vendor/contao/core-bundle/src/EventListener/BackendLocaleListener.php line 45

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\BackendUser;
  12. use Contao\CoreBundle\Util\LocaleUtil;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\LocaleAwareInterface;
  16. /**
  17.  * @internal
  18.  */
  19. class BackendLocaleListener
  20. {
  21.     /**
  22.      * @var Security
  23.      */
  24.     private $security;
  25.     /**
  26.      * @var LocaleAwareInterface
  27.      */
  28.     private $translator;
  29.     public function __construct(Security $securityLocaleAwareInterface $translator)
  30.     {
  31.         $this->security $security;
  32.         $this->translator $translator;
  33.     }
  34.     /**
  35.      * Sets the default locale based on the user language.
  36.      */
  37.     public function __invoke(RequestEvent $event): void
  38.     {
  39.         $user $this->security->getUser();
  40.         if (!$user instanceof BackendUser || !$user->language) {
  41.             return;
  42.         }
  43.         $request $event->getRequest();
  44.         $request->setLocale($user->language);
  45.         $this->translator->setLocale($user->language);
  46.         // Deprecated since Contao 4.0, to be removed in Contao 5.0
  47.         $GLOBALS['TL_LANGUAGE'] = LocaleUtil::formatAsLanguageTag($user->language);
  48.     }
  49. }