src/EventSubscriber/LocaleSubscriber.php line 33

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