Skip to content

Commit

Permalink
Fix unsetting locale
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jul 7, 2024
1 parent 50b5934 commit bdeb0e0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/EventListener/LocaleEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

final class LocaleEventListener implements EventSubscriberInterface
{
private const ATTR_LOCALE = '_locale';

private const ATTR_TIMEZONE = '_timezone';

private readonly LocaleAwareTranslator $translator;

public function __construct(LocaleAwareTranslator $translator)
Expand Down Expand Up @@ -78,9 +82,8 @@ public function onKernelRequest(RequestEvent $event): void

$session = $request->getSession();

if (null !== $locale = $session->get('_locale')) {
$this->translator->setLocale($locale);
$request->setLocale($locale);
if (null !== $locale = $session->get(self::ATTR_LOCALE)) {
$this->setLocale($request, $locale);
}
}

Expand Down Expand Up @@ -113,12 +116,14 @@ private function setLocale(Request $request, LocaleAwareUser $user): void
$locale = $user->getLocale();

if ('' === $locale || null === $locale) {
$session->remove(self::ATTR_LOCALE);

return;
}

$this->translator->setLocale($locale);
$request->setLocale($locale);
$session->set('_locale', $locale);
$session->set(self::ATTR_LOCALE, $locale);
}

private function setTimezone(Request $request, LocaleAwareUser $user): void
Expand All @@ -127,13 +132,16 @@ private function setTimezone(Request $request, LocaleAwareUser $user): void
return;
}

$session = $request->getSession();

$timezone = $user->getTimezone();

if ('' === $timezone || null === $timezone) {
$session->remove(self::ATTR_TIMEZONE);

return;
}

$session = $request->getSession();
$session->set('_timezone', $timezone);
$session->set(self::ATTR_TIMEZONE, $timezone);
}
}

0 comments on commit bdeb0e0

Please sign in to comment.