src/Event/Listener/BGLocaleSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: PMDiagne
  5.  * Date: 08/04/2020
  6.  * Time: 15:24
  7.  */
  8. namespace App\Event\Listener;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use App\Utils\Constantes;
  13. class BGLocaleSubscriber implements EventSubscriberInterface
  14. {
  15.     private $defaultLocale;
  16.     public function __construct(string $defaultLocale 'fr')
  17.     {
  18.         $this->defaultLocale $defaultLocale;
  19.     }
  20.     public function onKernelRequest(RequestEvent $event)
  21.     {
  22.         $request $event->getRequest();
  23.         if ($request->headers->has(Constantes::LOCALE_LANGUAGE)) {
  24.             $request->setLocale($request->headers->get(Constantes::LOCALE_LANGUAGE));
  25.         }
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  31.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  32.         ];
  33.     }
  34. }