src/EventSubscriber/UserLocaleSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  8. use Symfony\Component\Security\Http\SecurityEvents;
  9. class UserLocaleSubscriber implements EventSubscriberInterface
  10. {
  11.     private $requestStack;
  12.     public function __construct(RequestStack $requestStack)
  13.     {
  14.         $this->requestStack $requestStack;
  15.     }
  16.     public function onLoginSuccess(LoginSuccessEvent $event)
  17.     {
  18.         $user $event->getAuthenticatedToken()->getUser();
  19.         if ($user->getLanguage() != null && $user->getLanguage()->getKey() !== null) {
  20.             $this->requestStack->getSession()->set('_locale'$user->getLanguage()->getKey());
  21.         }
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             LoginSuccessEvent::class => 'onLoginSuccess',
  27.         ];
  28.     }
  29. }