src/EventListener/User/LocaleSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\User;
  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.     private $defaultLocale;
  9.     
  10.     public function __construct($defaultLocale 'en')
  11.     {
  12.         $this->defaultLocale $defaultLocale;
  13.     }
  14.     
  15.     public function onKernelRequest(RequestEvent $event)
  16.     {
  17.         $request $event->getRequest();
  18.         $locale $request->getSession()->get('_locale');
  19.         if(!empty($locale))        
  20.         {
  21.             $request->setLocale($locale);
  22.             $request->attributes->set('_locale'$locale);
  23.         }
  24.     }
  25.     
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::REQUEST => [['onKernelRequest']],
  30.         ];
  31.     }
  32. }