src/EventSubscriber/LocaleSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class LocaleSubscriber implements EventSubscriberInterface
  16. {
  17.     public function onKernelRequest(RequestEvent $event): void
  18.     {
  19.         $request $event->getRequest();
  20.         if (!$request->hasPreviousSession()) {
  21.             return;
  22.         }
  23.         // try to see if the locale has been set as a _locale routing parameter
  24.         if ($locale $request->attributes->get('_locale')) {
  25.             $request->getSession()->set('_locale'$locale);
  26.         } else {
  27.             // if no explicit locale has been set on this request, use one from the session
  28.             $request->setLocale($request->getSession()->get('_locale''fr'));
  29.         }
  30.     }
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  38.             KernelEvents::REQUEST => [['onKernelRequest'20]]
  39.         ];
  40.     }
  41. }