src/App/Event/LocaleSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3. * Disclaimer: This source code is protected by copyright law and by
  4. * international conventions.
  5. *
  6. * Any reproduction or partial or total distribution of the source code, by any
  7. * means whatsoever, is strictly forbidden.
  8. *
  9. * Anyone not complying with these provisions will be guilty of the offense of
  10. * infringement and the penal sanctions provided for by law.
  11. *
  12. * © 2022 All rights reserved.
  13. */
  14. namespace App\Event;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. /**
  19.  * LocaleSubscriber.
  20.  *
  21.  * @author John Doe <john.doe@projetx.io>
  22.  * @package App\Event
  23.  */
  24. class LocaleSubscriber implements EventSubscriberInterface {
  25.     /** @var string */
  26.     private $defaultLocale;
  27.     /**
  28.      * LocaleSubscriber constructor.
  29.      *
  30.      * @param string $defaultLocale
  31.      */
  32.     public function __construct(string $defaultLocale 'fr') {
  33.         $this->defaultLocale $defaultLocale;
  34.     }
  35.     /**
  36.      * @return \array[][]
  37.      */
  38.     public static function getSubscribedEvents() {
  39.         return [
  40.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  41.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  42.         ];
  43.     }
  44.     /**
  45.      * @param RequestEvent $event
  46.      */
  47.     public function onKernelRequest(RequestEvent $event) {
  48.         $request $event->getRequest();
  49.         if (!$request->hasPreviousSession()) {
  50.             return;
  51.         }
  52.         if ($request->getSession()->get("_locale") !== null) {
  53.             $request->setLocale($request->getSession()->get("_locale"));
  54.         }
  55.     }
  56. }