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 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢