Symfony2 wrong locale detection?

后端 未结 4 1691
鱼传尺愫
鱼传尺愫 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 10:44

    You can register listener like follow:

    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    
    class LocaleListener
    {
        private $container;
        private $defaultLocale;
    
        public function __construct(ContainerInterface $container, $defaultLocale = 'nl')
        {
            $this->container = $container;
            $this->defaultLocale = $defaultLocale;
        }
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
                return;
            }
    
            if (!$this->container->has('session')) {
                return;
            }
    
            $session = $this->container->get('session');
            $session->setLocale($this->defaultLocale);
        }
    }
    

    (gist)

    Just after framework session setup stage:

    
        
        
    
    

    (gist)

提交回复
热议问题