vendor/contao/core-bundle/src/EventListener/LocaleSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\CoreBundle\Intl\Locales;
  12. use Contao\CoreBundle\Routing\ScopeMatcher;
  13. use Contao\CoreBundle\Util\LocaleUtil;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Contracts\Translation\LocaleAwareInterface;
  19. /**
  20.  * @internal
  21.  */
  22. class LocaleSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var LocaleAwareInterface
  26.      */
  27.     private $translator;
  28.     /**
  29.      * @var ScopeMatcher
  30.      */
  31.     private $scopeMatcher;
  32.     /**
  33.      * @var array
  34.      */
  35.     private $availableLocales;
  36.     public function __construct(LocaleAwareInterface $translatorScopeMatcher $scopeMatcherLocales $locales)
  37.     {
  38.         $this->translator $translator;
  39.         $this->scopeMatcher $scopeMatcher;
  40.         $this->availableLocales $locales->getEnabledLocaleIds();
  41.     }
  42.     /**
  43.      * Adds the default locale as request attribute.
  44.      */
  45.     public function onKernelRequest(RequestEvent $event): void
  46.     {
  47.         if (!$this->scopeMatcher->isContaoRequest($event->getRequest())) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         $request->attributes->set('_locale'$this->getLocale($request));
  52.     }
  53.     /**
  54.      * Sets the translator locale to the preferred browser language.
  55.      */
  56.     public function setTranslatorLocale(RequestEvent $event): void
  57.     {
  58.         $this->translator->setLocale($event->getRequest()->getPreferredLanguage($this->availableLocales));
  59.     }
  60.     public static function getSubscribedEvents(): array
  61.     {
  62.         return [
  63.             KernelEvents::REQUEST => [
  64.                 // The priority must be lower than the one of the Symfony route listener (defaults to 32)
  65.                 // and higher than the Symfony locale listener (defaults to 16)
  66.                 ['onKernelRequest'20],
  67.                 ['setTranslatorLocale'100],
  68.             ],
  69.         ];
  70.     }
  71.     /**
  72.      * Returns the locale from the request or the HTTP header.
  73.      */
  74.     private function getLocale(Request $request): string
  75.     {
  76.         if (null !== $request->attributes->get('_locale')) {
  77.             return LocaleUtil::formatAsLocale($request->attributes->get('_locale'));
  78.         }
  79.         return $request->getPreferredLanguage($this->availableLocales);
  80.     }
  81. }