src/EventSubscriber/LocaleSubscriber.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class LocaleSubscriber implements EventSubscriberInterface
  7. {
  8.     public function onKernelRequest(RequestEvent $event)
  9.     {
  10.         $request $event->getRequest();
  11.         if (!$request->hasPreviousSession()) {
  12.             return;
  13.         }
  14.         $defaultLocale $request->attributes->get('_locale');
  15.         if( $locale $request->getSession()->get('_locale'$defaultLocale) )
  16.             $request->setLocale($locale);
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  22.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  23.         ];
  24.     }
  25. }