src/EventSubscriber/RedirectToLocaleSubscriber.php line 73

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