dongmibeng5885 2013-06-21 20:04
浏览 23
已采纳

改变语言环境symfony 2.3

I just began with symfony I'm trying to build a multilang website but I have a problem to change the locale

I read some posts and I read the documentation about this but the locale don't change, I try:

public function indexAction()
{    
    $this->get('session')->set('_locale', 'fr');

    $request = $this->getRequest();
    $locale = $request->getLocale();
    return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}

but the value in $locale is always 'en' (my default locale)

I also try

public function indexAction()
{    
    $this->get('session')->set('_locale', 'fr');

    $request = $this->getRequest();
    $request->setLocale('fr');
    $locale = $request->getLocale();

    return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}

In this case $locale is fr but the translations are always from messages.en.yml

I'd like in a first time to detect the user locale using $_SERVER['HTTP_ACCEPT_LANGUAGE'], maybe using a listner on each page actualisation ?

and after I will create a route to change the locale

But I 'd like to find a way to change the locale.

Thanks for your help

  • 写回答

3条回答 默认 最新

  • douwei8295 2013-06-22 07:51
    关注

    Based on this and this answers.

    LanguageListener.php:

    <?php
    
    namespace Acme\UserBundle\EventListener;
    use Symfony\Component\HttpFoundation\Session\Session;
    use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    
    class LanguageListener
    {
        private $session;
    
        public function setSession(Session $session)
        {
            $this->session = $session;
        }
    
        public function setLocale(GetResponseEvent $event)
        {
            if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
                return;
            }
    
            $request = $event->getRequest();
            $request->setLocale($request->getPreferredLanguage(array('en', 'de')));
    
        }
    }
    

    services.yml:

    acme.language.kernel_request_listener:
        class: Acme\UserBundle\EventListener\LanguageListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: setLocale }
    

    About wrong locale detection in twig, there could be a lot of different causes. Search through the SO, you'll definitely find the answer. Make sure that your '_local' var is defined right, make sure that you put your languages files in the right place, etc. FInally, read again the last version of the documentation: http://symfony.com/doc/current/book/translation.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料