change locale symfony 2.3

后端 未结 3 1231
梦如初夏
梦如初夏 2020-12-29 17:40

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 thi

3条回答
  •  余生分开走
    2020-12-29 18:13

    Based on this and this answers.

    LanguageListener.php:

    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

提交回复
热议问题