src/Listener/LocaleApiListener.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class LocaleApiListener implements EventSubscriberInterface
  7. {
  8.     public function onKernelRequest(RequestEvent $event)
  9.     {
  10.         $request $event->getRequest();
  11.         if ($request->headers->has('X-Accept-Language') && strpos($request->getPathInfo(), '/api') === 0) {
  12.             $locale $request->headers->get('X-Accept-Language');
  13.             $request->setLocale($locale);
  14.         }
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  20.         ];
  21.     }
  22. }