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).
我目前正在尝试在Symfony 2.0中启用翻译器。 Symfony忽略了Accept-Language Header变量,并使用default_locale(当没有定义回退时)。
My request looks like:
我的请求如下:
Accept-Language de-DE,de;q=0.8,en-us;q=0.5,en;q=0.3
but $this->getRequest()->getLocale();
gets me en
with that same request.
但$ this-> getRequest() - > getLocale();得到我同样的要求。
Can somebody tell me what may be wrong?
有人能告诉我可能有什么问题吗?
Yes, I have tried to clear the cache and deleting my cookies (omnomnom) :)
是的,我试图清除缓存并删除我的cookie(omnomnom):)
1 个解决方案
#1
20
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不使用Accept Language标头,而是依赖于区域设置的symfony配置。实际上,建议不要对不同语言环境中的内容使用相同的URL,请参阅此文档:
Symfony 2 The Book - Translations - The Locale and the URL
Symfony 2 The Book - Translations - Locale和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:
但是,如果您想忽略此建议并使用Accept语言标头,则可以在控制器中使用此代码执行此操作:
$request = $this->getRequest();
$session = $this->get('session');
$session->setLocale($request->getPreferredLanguage(array('de', 'en')));
#1
20
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不使用Accept Language标头,而是依赖于区域设置的symfony配置。实际上,建议不要对不同语言环境中的内容使用相同的URL,请参阅此文档:
Symfony 2 The Book - Translations - The Locale and the URL
Symfony 2 The Book - Translations - Locale和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:
但是,如果您想忽略此建议并使用Accept语言标头,则可以在控制器中使用此代码执行此操作:
$request = $this->getRequest();
$session = $this->get('session');
$session->setLocale($request->getPreferredLanguage(array('de', 'en')));