src/EventSubscriber/LocaleSubscriber.php line 25

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.     private $defaultLocale;
  8.     public function __construct($defaultLocale='fr'){
  9.         $this->defaultLocale=$defaultLocale;
  10.     }
  11.     public function onKernelRequest(RequestEvent $event){
  12.         $request=$event->getRequest();
  13.         if(!$request->hasPreviousSession()){
  14.             return;
  15.         }
  16.         if($locale=$request->query->get('_locale')){
  17.             //$request->setLocale($locale);
  18.              $request->getSession()->set('_locale'$locale);
  19.         }else{
  20.             $request->setLocale($request->getSession()->get('_locale',
  21.             $this->defaultLocale));
  22.         }
  23.     }
  24.     public static function getSubscribedEvents(){
  25.         return[
  26.             KernelEvents::REQUEST =>[['onKernelRequest',20]],
  27.         ];
  28.     }
  29. }