src/EventSubscriber/RedirectToLocaleSubscriber.php line 68

  1. <?php
  2. /*
  3.  * This file is part of the AdminLTE-Bundle demo.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace App\EventSubscriber;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. /**
  15.  * When visiting the homepage, this listener redirects the user to the most
  16.  * appropriate localized version according to the browser settings.
  17.  *
  18.  * See http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-request-event
  19.  *
  20.  * @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
  21.  */
  22. class RedirectToLocaleSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * List of supported locales.
  26.      *
  27.      * @var string[]
  28.      */
  29.     private $locales = [];
  30.     private string $defaultLocale '';
  31.     /**
  32.      * @param string      $locales       Supported locales separated by '|'
  33.      * @param string|null $defaultLocale
  34.      */
  35.     public function __construct(private UrlGeneratorInterface $urlGenerator$locales$defaultLocale null)
  36.     {
  37.         $this->locales explode('|'trim($locales));
  38.         if (empty($this->locales)) {
  39.             throw new \UnexpectedValueException('The list of supported locales must not be empty.');
  40.         }
  41.         $this->defaultLocale $defaultLocale ?: $this->locales[0];
  42.         if (!in_array($this->defaultLocale$this->locales)) {
  43.             throw new \UnexpectedValueException(sprintf('The default locale ("%s") must be one of "%s".'$this->defaultLocale$locales));
  44.         }
  45.         // Add the default locale at the first position of the array,
  46.         // because Symfony\HttpFoundation\Request::getPreferredLanguage
  47.         // returns the first element when no an appropriate language is found
  48.         array_unshift($this->locales$this->defaultLocale);
  49.         $this->locales array_unique($this->locales);
  50.     }
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             KernelEvents::REQUEST => ['onKernelRequest'],
  55.         ];
  56.     }
  57.     public function onKernelRequest(RequestEvent $event)
  58.     {
  59.         $request $event->getRequest();
  60.         // Ignore sub-requests and all URLs but the homepage
  61.         if ('/admin' !== $request->getPathInfo()) {
  62.             return;
  63.         }
  64.         // Ignore requests from referrers with the same HTTP host in order to prevent
  65.         // changing language for users who possibly already selected it for this application.
  66.         if (=== stripos($request->headers->get('referer'), $request->getSchemeAndHttpHost())) {
  67.             return;
  68.         }
  69.         $preferredLanguage $request->getPreferredLanguage($this->locales);
  70.         $response = new RedirectResponse($this->urlGenerator->generate('app_admin_index', ['_locale' => $preferredLanguage]));
  71.         $event->setResponse($response);
  72.     }
  73. }