src/EventSubscriber/KernelRequestSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class KernelRequestSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var ContainerInterface
  10.      */
  11.     protected $container;
  12.     public function __construct(
  13.         ContainerInterface $container
  14.     ) {
  15.         $this->container $container;
  16.     }
  17.     public function setLanguage($event)
  18.     {
  19.         if (!$event->isMasterRequest()) {
  20.             return;
  21.         }
  22.         if ($locale $event->getRequest()->headers->get('X-Language')) {
  23.             $locale strtolower($locale);
  24.             $event->getRequest()->setLocale($locale);
  25.         }
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::REQUEST => [
  31.                 ['setLanguage'16]
  32.             ],
  33.         ];
  34.     }
  35. }