change locale symfony 2.3

后端 未结 3 1217
梦如初夏
梦如初夏 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:17

    I however added this to make it more dynamic

    services.yml

     services:
            acme.language.kernel_request_listener:
                class: Acme\UserBundle\EventListener\LanguageListener
                tags:
                    - { name: kernel.event_listener, event: kernel.request, method: setLocale }
                arguments: [ @router, @service_container ]
    

    LanguageListener.php:

    router= $router;
            $this->container = $container;
        }
    
        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($this->container->parameters['jms_i18n_routing.locales']));
    
        }
    }
    

    Just to be able to get the parameters and values from config.yml.

    Regards, Wick

提交回复
热议问题