src/EventListener/LocaleListener.php line 18

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