app/Plugin/ChangeLocale4/Event.php line 21

Open in your IDE?
  1. <?php
  2. namespace Plugin\ChangeLocale4;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class Event implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @return array
  10.      */
  11.     public static function getSubscribedEvents()
  12.     {
  13.         return [
  14.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  15.         ];
  16.     }
  17.     public function onKernelRequest(GetResponseEvent $event)
  18.     {
  19.         $request $event->getRequest();
  20.         if (!$request->hasPreviousSession()) {
  21.             return;
  22.         }
  23.         // try to see if the locale has been set as a _locale routing parameter
  24.         if ($locale $request->attributes->get('_locale')) {
  25.             $request->getSession()->set('_locale'$locale);
  26.         } else {
  27.             $request->setLocale($request->getSession()->get('_locale',  'en'));
  28.         }
  29.     }
  30. }