src/EventSubscriber/UserLocaleSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  11. use Symfony\Component\Security\Http\SecurityEvents;
  12. use Symfony\Component\Translation\Translator;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. /**
  15.  * This class manage the locale with the user->getLocale() value
  16.  * It manage it for session with classic login or jwt token.
  17.  */
  18. class UserLocaleSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private RequestStack $requestStack,
  22.         private string $defaultLocale,
  23.         private TranslatorInterface $translator
  24.     ) {
  25.     }
  26.     /**
  27.      * Used to avoid warning.
  28.      *
  29.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         // used to be done just after the default locale set
  34.         return [
  35.             KernelEvents::REQUEST => ['onKernelRequest'20],
  36.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  37.             Events::JWT_AUTHENTICATED => 'onJwtAuthenticated',
  38.         ];
  39.     }
  40.     /**
  41.      * On Login we store in session the locale of the user.
  42.      */
  43.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  44.     {
  45.         /** @var User $user */
  46.         $user $event->getAuthenticationToken()->getUser();
  47.         if (null !== $user->getLocale()) {
  48.             $this->requestStack->getSession()->set('_locale'$user->getLocale());
  49.         }
  50.     }
  51.     /**
  52.      * On kernel request, we set the locale from the session that have been previously stored.
  53.      */
  54.     public function onKernelRequest(RequestEvent $event)
  55.     {
  56.         $request $event->getRequest();
  57.         if (!$request->hasPreviousSession()) {
  58.             return;
  59.         }
  60.         // try to see if the locale has been set as a _locale routing parameter
  61.         if ($locale $request->attributes->get('_locale')) {
  62.             $request->getSession()->set('_locale'$locale);
  63.         } else {
  64.             // if no explicit locale has been set on this request, use one from the session
  65.             $request->setLocale($request->getSession()->get('_locale'$this->defaultLocale));
  66.         }
  67.         // adding the accept-language from header management
  68.         /* if ($request->headers->has("Accept-Language")) {
  69.             $locale = $request->getPreferredLanguage();
  70.             $request->setLocale($locale);
  71.         }*/
  72.     }
  73.     /**
  74.      * after a jwt token is set, we need to keep the same logic...
  75.      * But as the event is done after the translation set... we need to do it.
  76.      */
  77.     public function onJwtAuthenticated(JWTAuthenticatedEvent $event)
  78.     {
  79.         /** @var User $user */
  80.         $user $event->getToken()->getUser();
  81.         $request $this->requestStack->getCurrentRequest();
  82.         if ($locale $request->attributes->get('_locale')) {
  83.             $request->getSession()->set('_locale'$locale);
  84.         } else {
  85.             $locale $user->getLocale();
  86.             // adding the accept-language from header management
  87.             /*if ($request->headers->has("Accept-Language")) {
  88.                 $locale = $request->getPreferredLanguage();
  89.             }*/
  90.             $request->setLocale($locale);
  91.             // force the translator to the calculated locale
  92.             /** @var Translator $translator */
  93.             $translator $this->translator;
  94.             $translator->setLocale($locale);
  95.         }
  96.     }
  97. }