eZ Publish uses - locale format (mostly, some supported locales being out of this format, e.g. cro-HR).
Symfony uses the standard POSIX locale format (_), which is supported by Intl PHP extension.
Locale converters are meant to convert in those 2 formats back and forth.
/** * Returns a hash with 'locale' as key and locale string in POSIX format as value. * * Locale from request object will be used as locale if set, otherwise field language code * will be converted to locale string. * * @param Field $field * * @return array */ public function getViewParameters(Field $field) { $parameters = array(); if ($this->request && $this->request->attributes->has('_locale')) { $parameters['locale'] = $this->request->attributes->get('_locale'); } else { $parameters['locale'] = $this->localeConverter->convertToPOSIX($field->languageCode); } return $parameters; }
/** * @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')); }
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); }