src/EventSubscribers/LocaleSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscribers;
  3. use JetBrains\PhpStorm\ArrayShape;
  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.     #[ArrayShape([KernelEvents::REQUEST => 'array[]'])]
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             KernelEvents::REQUEST => [['onKernelRequest'15]],
  14.         ];
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $request $event->getRequest();
  19.         if ($locale $request->attributes->get('_locale')) {
  20.             $request->getSession()->set('_locale'$locale);
  21.         } else {
  22.             $request->setLocale($request->getSession()->get('_locale'$request->getLocale()));
  23.         }
  24.     }
  25. }