为什么Symfony会忽略浏览器的区域设置(HTTP请求接受语言标头)? [英] Why does Symfony ignore the browsers locale-setting (HTTP-Request Accept-Language Header)?

查看:117
本文介绍了为什么Symfony会忽略浏览器的区域设置(HTTP请求接受语言标头)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在Symfony 2.0中启用翻译器. Symfony忽略了Accept-Language Header变量,并使用default_locale(并且在未定义后备时).

i am currently trying to enable the translator in Symfony 2.0. Symfony is ignoring the Accept-Language Header variable and is using default_locale (and when that is not defined the fallback).

我的请求如下:

Accept-Language de-DE,de;q=0.8,en-us;q=0.5,en;q=0.3

但是$this->getRequest()->getLocale();用相同的请求得到了我en.

but $this->getRequest()->getLocale(); gets me en with that same request.

有人可以告诉我什么地方可能出问题吗?

Can somebody tell me what may be wrong?

是的,我试图清除缓存并删除我的cookie(omnomnom):)

Yes, I have tried to clear the cache and deleting my cookies (omnomnom) :)

推荐答案

这是预期的行为.默认情况下,Symfony不使用接受语言"标头,而是依赖于symfony配置进行区域设置.实际上,建议不要对不同语言环境的内容使用相同的URL,请参阅此文档:

This is the expected behaviour. Symfony does not by default use the Accept Language header and instead relies on the symfony configuration for locale settings. In fact, it is advised not to use the same URL for content in different locales, see this document:

Symfony 2本书-翻译-区域设置和URL

但是,如果您想忽略此建议并使用接受"语言标头,则可以在控制器中使用以下代码来做到这一点:

But if you want to ignore this advice and use the Accept language header, you can do it with this code in your controller:

$request = $this->getRequest();
$session = $this->get('session');

$session->setLocale($request->getPreferredLanguage(array('de', 'en')));

这篇关于为什么Symfony会忽略浏览器的区域设置(HTTP请求接受语言标头)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆