src/EventListener/LocaleListener.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\EventListener;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  10. use Symfony\Component\Routing\RouterInterface;
  11. class LocaleListener implements EventSubscriberInterface
  12. {
  13.     const COOKIE 'pref_language';
  14.     const ALLOWED_LOCALE_FRONTEND = [
  15.         'de''en'
  16.     ];
  17.     const DEFAULT_LOCALE_FRONTEND 'de';
  18.     const ALLOWED_LOCALE_BACKEND = [
  19.         'de'
  20.     ];
  21.     const DEFAULT_LOCALE_BACKEND 'de';
  22.     private $defaultLocale;
  23.     private $allowedLocale;
  24.     private function isAllowed($locale)
  25.     {
  26.         return in_array($locale$this->allowedLocale);
  27.     }
  28.     private function setDefault($request)
  29.     {
  30.         try {
  31.             $route $this->router->matchRequest($request)['_route'];
  32.         } catch (ResourceNotFoundException $e) {
  33.             $route null;
  34.         }
  35.         if ($route == 'easyadmin') {
  36.             $this->allowedLocale self::ALLOWED_LOCALE_BACKEND;
  37.             $this->defaultLocale self::DEFAULT_LOCALE_BACKEND;
  38.         } else {
  39.             $this->allowedLocale self::ALLOWED_LOCALE_FRONTEND;
  40.             $this->defaultLocale self::DEFAULT_LOCALE_FRONTEND;
  41.         }
  42.     }
  43.     public function __construct(RouterInterface $router$defaultLocale 'de')
  44.     {
  45.         $this->router $router;
  46.         $this->defaultLocale $defaultLocale;
  47.     }
  48.     public function onKernelRequest(RequestEvent $event)
  49.     {
  50.         $request $event->getRequest();
  51.         if (!$request->hasPreviousSession()) {
  52.             return;
  53.         }
  54.         $this->setDefault($request);
  55.         // try to see if the locale has been set as a _locale routing parameter
  56.         $locale $request->attributes->get('_locale');
  57.         if (!$locale && $request->isMethod('GET')) {
  58.             // try to see if the locale has been set as a locale get parameter
  59.             $locale $request->query->get('locale');
  60.         }
  61.         // try to see if the locale has been set as a cookie
  62.         if (!$locale || !$this->isAllowed($locale)) {
  63.             $locale $request->cookies->get(self::COOKIE);
  64.         }
  65.         if ($locale && $this->isAllowed($locale)) {
  66.             $request->getSession()->set('_locale'$locale);
  67.             $request->setLocale($locale);
  68.         } else {
  69.             // if no explicit locale has been set on this request, use one from the session
  70.             $locale $request->getSession()->get('_locale'$this->defaultLocale);
  71.             $request->setLocale($locale);
  72.         }
  73.         $request->cookies->set(self::COOKIE$locale);
  74.     }
  75.     public static function getSubscribedEvents()
  76.     {
  77.         return [
  78.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  79.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  80.         ];
  81.     }
  82. }