src/EventSubscriber/TranslatorLocaleSubscriber.php line 18

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. use Symfony\Contracts\Translation\TranslatorInterface;
  7. class TranslatorLocaleSubscriber implements EventSubscriberInterface
  8. {
  9.     private TranslatorInterface $translator;
  10.     public function __construct(TranslatorInterface $translator)
  11.     {
  12.         $this->translator $translator;
  13.     }
  14.     public function onKernelRequest(RequestEvent $event): void
  15.     {
  16.         $request $event->getRequest();
  17.         $this->translator->setLocale($request->getLocale());
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             KernelEvents::REQUEST => [['onKernelRequest', -10]], // moins prioritaire que LocaleSubscriber
  23.         ];
  24.     }
  25. }