Symfony2 wrong locale detection?

后端 未结 4 1689
鱼传尺愫
鱼传尺愫 2020-12-15 10:20

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

4条回答
  •  星月不相逢
    2020-12-15 11:00

    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.

提交回复
热议问题