Symfony2错误的语言环境检测? [英] Symfony2 wrong locale detection?

查看:58
本文介绍了Symfony2错误的语言环境检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Symfony2的有关> 的指南,我发现可以从http标头(存储在$this->get('session')->getLocale()中)是错误的(发送,推断为 en ):

Following Symfony2 guide about translation i found that inferred locale from http headers (stored in $this->get('session')->getLocale()) is wrong (sent it, inferred en):

主机localhost用户代理Mozilla/5.0(Windows NT 6.1; WOW64; rv:7.0.1)Gecko/20100101 Firefox/7.0.1 接受text/html,application/xhtml + xml,application/xml; q = 0.9,/; q = 0.8 接受语言it-it,它; q = 0.8,en-us; q = 0.5,en; q = 0.3

Host localhost User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language it-it,it;q=0.8,en-us;q=0.5,en;q=0.3

这是正常行为吗?还是我应该设置一些东西才能使本地化立即可用?

Is this a normal behaviour? Or should i set something in order to get localization working out of the box?

推荐答案

我今天对代码进行了更彻底的研究,因为我遇到了与您相同的问题,并且该语言似乎来自Session::getLocale().但是Symfony2永远不会调用Session::setLocale(),并设置Session对象的locale成员.谷歌搜索"symfony2会话setlocale"会导致文档的§

I looked more thoroughly onto the code today, because I was experiencing the same problem as you, and it appears that the language comes from Session::getLocale(). But Symfony2 never calls Session::setLocale(), and sets the locale member of the Session object. A google search for "symfony2 session setlocale" leads to this § of the documentation

所以我最终将这一行放在我正在使用的控制器之上,并且有效:

So I ended up putting this line on top of the controller I was working on, and it worked :

$this->getRequest()->getSession()->setLocale(
    $this->getRequest()->getPreferredLanguage());

现在我猜想这是不可接受的,因为您不会将其添加到每个控制器的顶部.另外,不应在每个请求上都执行此操作,而应该在用户没有会话时才对第一个请求执行此操作.如果有人知道该怎么做,请随时编辑此答案.

Now I guess this is not acceptable, because you're not going to add this on top of each and every controller. Plus, this should not be done for every request, it should only be done for the first one, when the user has no session yet. If anyone knows how to do this feel free to edit this answer.

这篇关于Symfony2错误的语言环境检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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