src/EventSubscriber/LocaleSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Enums\General\Locale;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. class LocaleSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents(): array
  10.     {
  11.         return [
  12.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  13.         ];
  14.     }
  15.     public function onKernelRequest(RequestEvent $event): void
  16.     {
  17.         if (!$event->isMainRequest()) {
  18.             return;
  19.         }
  20.         $request $event->getRequest();
  21.         if (!$request->hasPreviousSession()) {
  22.             return;
  23.         }
  24.         $locale $request->getSession()->get('_locale'Locale::ES->value);
  25.         $request->setLocale($locale);
  26.     }
  27. }