eZ\Bundle\EzPublishCoreBundle\EventListener\LocaleListener::onKernelRequest PHP Method

onKernelRequest() public method

public onKernelRequest ( GetResponseEvent $event )
$event Symfony\Component\HttpKernel\Event\GetResponseEvent
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->attributes->has('_locale')) {
            foreach ($this->configResolver->getParameter('languages') as $locale) {
                $convertedLocale = $this->localeConverter->convertToPOSIX($locale);
                if ($convertedLocale !== null) {
                    // Setting the converted locale to the _locale request attribute, so that it can be properly processed by parent listener.
                    $request->attributes->set('_locale', $convertedLocale);
                    break;
                }
            }
        }
        parent::onKernelRequest($event);
    }

Usage Example

 /**
  * @dataProvider onKernelRequestProvider
  */
 public function testOnKernelRequest(array $configuredLanguages, array $convertedLocalesValueMap, $expectedLocale)
 {
     $this->configResolver->expects($this->once())->method('getParameter')->with('languages')->will($this->returnValue($configuredLanguages));
     $this->localeConverter->expects($this->atLeastOnce())->method('convertToPOSIX')->will($this->returnValueMap($convertedLocalesValueMap));
     $defaultLocale = 'en';
     $localeListener = new LocaleListener($defaultLocale);
     $localeListener->setConfigResolver($this->configResolver);
     $localeListener->setLocaleConverter($this->localeConverter);
     $request = new Request();
     $localeListener->onKernelRequest(new GetResponseEvent($this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST));
     $this->assertSame($expectedLocale, $request->attributes->get('_locale'));
 }