src/AppBundle/EventSubscriber/LocaleSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class LocaleSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var string
  10.      */
  11.     private $defaultLocale;
  12.     /**
  13.      * @param string|null $defaultLocale
  14.      */
  15.     public function __construct(?string $defaultLocale 'en')
  16.     {
  17.         $this->defaultLocale $defaultLocale;
  18.     }
  19.     /**
  20.      * @param GetResponseEvent $event
  21.      *
  22.      * @return void
  23.      */
  24.     public function onKernelRequest(GetResponseEvent $event): void
  25.     {
  26.         $request $event->getRequest();
  27.         if (preg_match('/^\/admin\//'$request->getPathInfo())) {
  28.             $request->attributes->set('_locale'$this->defaultLocale);
  29.         }
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             // Must be registered before (i.e. with a higher priority than) the default Locale listener.
  38.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  39.         ];
  40.     }
  41. }