Skip to content

Commit

Permalink
EZP-29902: Admin UI - Locale is not correctly set (#756)
Browse files Browse the repository at this point in the history
* EZP-29902: Admin UI - Locale is not correctly set

* fix for behat tests
  • Loading branch information
mikadamczyk authored and Łukasz Serwatka committed Dec 17, 2018
1 parent 0f4cfa0 commit e57e53d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib/Behat/PageElement/Fields/Date.php
Expand Up @@ -52,8 +52,8 @@ public function getValue(): array

public function verifyValueInItemView(array $values): void
{
$expectedDateTime = date_format(date_create($values['value']), self::VIEW_DATE_FORMAT);
$actualDateTime = $this->context->findElement($this->fields['fieldContainer'])->getText();
$expectedDateTime = date_create($values['value']);
$actualDateTime = date_create($this->context->findElement($this->fields['fieldContainer'])->getText());
Assert::assertEquals(
$expectedDateTime,
$actualDateTime,
Expand Down
24 changes: 16 additions & 8 deletions src/lib/EventListener/RequestLocaleListener.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Translation\TranslatorInterface;

class RequestLocaleListener implements EventSubscriberInterface
{
Expand All @@ -23,16 +24,22 @@ class RequestLocaleListener implements EventSubscriberInterface
/** @var array */
private $availableTranslations;

/** @var \Symfony\Component\Translation\TranslatorInterface */
private $translator;

/**
* @param array $siteAccessGroups
* @param array $availableTranslations
* @param \Symfony\Component\Translation\TranslatorInterface $translator
*/
public function __construct(
array $siteAccessGroups,
array $availableTranslations
array $availableTranslations,
TranslatorInterface $translator
) {
$this->siteAccessGroups = $siteAccessGroups;
$this->availableTranslations = $availableTranslations;
$this->translator = $translator;
}

/**
Expand All @@ -59,19 +66,20 @@ public function onKernelRequest(GetResponseEvent $event): void
}

$preferableLocales = $this->getPreferredLocales($request);
$locale = null;

$locale = false;
foreach ($preferableLocales as $preferableLocale) {
if (in_array($preferableLocale, $this->availableTranslations)) {
if (\in_array($preferableLocale, $this->availableTranslations)) {
$locale = $preferableLocale;
break;
}
}

if (false !== $locale) {
$request->setLocale($locale);
$request->attributes->set('_locale', $locale);
}
$locale = $locale ?? reset($preferableLocales);
$request->setLocale($locale);
$request->attributes->set('_locale', $locale);
// Set of the current locale on the translator service is needed because RequestLocaleListener has lower
// priority than LocaleListener and messages are not translated on login, forgot and reset password pages.
$this->translator->setLocale($locale);
}

/**
Expand Down

0 comments on commit e57e53d

Please sign in to comment.