atiweb/src/EventSubscriber/UserLocaleSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace Atiweb\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class UserLocaleSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * Locales autorisées
  10.      * @var array
  11.      */
  12.     protected $allowedLanguages = [
  13.         'fr',
  14.         'es',
  15.         'it',
  16.         'de',
  17.     ];
  18.     /**
  19.      * Locale par défaut
  20.      * @var string
  21.      */
  22.     protected $defaultLocale    'fr';
  23.     /**
  24.      * UserLocaleSubscriber constructor.
  25.      * @param string $defaultLocale
  26.      * @param string $allowedLanguagesPattern
  27.      */
  28.     public function __construct(string $defaultLocale, array $allowedLanguagesPattern)
  29.     {
  30.         $this->defaultLocale    $defaultLocale;
  31.         $this->allowedLanguages $allowedLanguagesPattern;
  32.     }
  33.     public function onKernelRequest(RequestEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         /**
  37.          * Lorsque le user demande le changement de langue
  38.          */
  39.         if ($locale $request->attributes->get('_locale')) {
  40.             $request->getSession()->set('_locale'$locale);
  41.             $request->setLocale($locale);
  42.             \Locale::setDefault($locale);
  43.         } else {
  44.             /**
  45.              * Si le user à une locale en session, sinon, on cherche via le navigateur
  46.              */
  47.             if($request->getSession()->has('_locale')) {
  48.                 $request->setLocale($request->getSession()->get('_locale'));
  49.                 \Locale::setDefault($request->getSession()->get('_locale'));
  50.             } else {
  51.                 if($locale $this->findLocaleFromAcceptLanguage($request->headers->get("Accept-Language"))) {
  52.                     $request->getSession()->set('_locale'$locale);
  53.                     $request->setLocale($locale);
  54.                     \Locale::setDefault($locale);
  55.                 } else {
  56.                     $request->getSession()->set('_locale'$this->defaultLocale);
  57.                     $request->setLocale($this->defaultLocale);
  58.                     \Locale::setDefault($this->defaultLocale);
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     protected function findLocaleFromAcceptLanguage($acceptLanguage)
  64.     {
  65.         $acceptLanguagesArray preg_split('/,/'$acceptLanguage);
  66.         foreach ($acceptLanguagesArray as $acceptLanguage) {
  67.             foreach ($this->allowedLanguages as $allowedLanguage) {
  68.                 if(preg_match('/^'.$allowedLanguage.'/'$acceptLanguage)) {
  69.                     return $allowedLanguage;
  70.                 }
  71.             }
  72.         }
  73.         return null;
  74.     }
  75.     public static function getSubscribedEvents()
  76.     {
  77.         return [
  78.             KernelEvents::REQUEST   => [
  79.                 [ 'onKernelRequest'18 ]
  80.             ]
  81.         ];
  82.     }
  83.     /**
  84.      * @return string
  85.      */
  86.     public function getDefaultLocale(): string
  87.     {
  88.         return $this->defaultLocale;
  89.     }
  90. }