src/EventSubscriber/LocaleSubscriber.php line 20

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 (!$event->isMainRequest()) {
  12.             return;
  13.         }
  14.         if (!$request->hasPreviousSession()) {
  15.             return;
  16.         }
  17.         if ($locale $request->attributes->get('_locale')) {
  18.             $request->getSession()->set('_locale'$locale);
  19.         } else {
  20.             $request->setLocale($request->getSession()->get('_locale''fr'));
  21.         }
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  27.             KernelEvents::REQUEST => [['onKernelRequest'33]],
  28.         ];
  29.     }
  30. }