src/Core/EventSubscriber/UserLocaleSubscriber.php line 28

  1. <?php
  2. namespace App\Core\EventSubscriber;
  3. use JetBrains\PhpStorm\ArrayShape;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. class UserLocaleSubscriber implements EventSubscriberInterface
  9. {
  10.     private RequestStack $requestStack;
  11.     public function __construct(RequestStack $requestStack)
  12.     {
  13.         $this->requestStack $requestStack;
  14.     }
  15.     #[ArrayShape([SecurityEvents::INTERACTIVE_LOGIN => 'string'])]
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  20.         ];
  21.     }
  22.     public function onInteractiveLogin(InteractiveLoginEvent $loginEvent)
  23.     {
  24.         $user $loginEvent->getAuthenticationToken()->getUser();
  25.         if (null !== $user->getLocale()) {
  26.             $this->requestStack->getSession()->set('_locale'$user->getLocale());
  27.         }
  28.     }
  29. }