src/mywebsolutions/intl-bundle/EventSubscriber/LocaleSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Class LocaleSubscriber
  4.  * @package MWS\IntlBundle\EventSubscriber
  5.  * @author Martin Walther <martin@myweb.solutions>
  6.  *
  7.  * (c) MyWebSolutions
  8.  */
  9. namespace MWS\IntlBundle\EventSubscriber;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Validator\Constraints\Locale;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. class LocaleSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var string
  21.      */
  22.     private string $defaultLocale;
  23.     /**
  24.      * @var array
  25.      */
  26.     private array $supportedLocales;
  27.     /**
  28.      * @var ValidatorInterface
  29.      */
  30.     private ValidatorInterface $validator;
  31.     /**
  32.      * @var string
  33.      */
  34.     protected string $currentLocale;
  35.     /**
  36.      * LocaleSubscriber constructor.
  37.      * @param string $defaultLocale
  38.      * @param array $supportedLocales
  39.      * @param ValidatorInterface $validator
  40.      */
  41.     public function __construct(ValidatorInterface $validator, array $supportedLocalesstring $defaultLocale 'en_US')
  42.     {
  43.         $this->defaultLocale $defaultLocale;
  44.         $this->supportedLocales $supportedLocales;
  45.         $this->validator $validator;
  46.     }
  47.     /**
  48.      * @return array
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return array(
  53.             // must be registered after the default Locale listener
  54.             KernelEvents::REQUEST => [['onKernelRequest'17]],
  55.             KernelEvents::RESPONSE =>['setContentLanguage']
  56.         );
  57.     }
  58.     /**
  59.      * @param RequestEvent $event
  60.      */
  61.     public function onKernelRequest(RequestEvent $event)
  62.     {
  63.         $request $event->getRequest();
  64.         // try to see if the locale has been set as a accept-language routing parameter
  65.         if ($locale $request->headers->get('accept-language')) {
  66.             if ($this->isLocaleValid($locale) && in_array($locale$this->supportedLocales)) {
  67.                 $request->setLocale($locale);
  68.             } else {
  69.                 // if locale is not valid, use the default one
  70.                 $request->setLocale$this->defaultLocale);
  71.             }
  72.         } else {
  73.             // if no explicit locale has been set on this request, use the default one
  74.             $request->setLocale$this->defaultLocale);
  75.         }
  76.         $this->currentLocale $request->getLocale();
  77.     }
  78.     /**
  79.      * @param ResponseEvent $event
  80.      * @return Response
  81.      */
  82.     public function setContentLanguage(ResponseEvent $event): Response
  83.     {
  84.         if (!isset($this->currentLocale)) {
  85.             $this->currentLocale $this->defaultLocale;
  86.         }
  87.         $response $event->getResponse();
  88.         $response->headers->add(array('Content-Language' => $this->currentLocale));
  89.         return $response;
  90.     }
  91.     /**
  92.      * @param $locale
  93.      * @return bool
  94.      */
  95.     private function isLocaleValid($locale): bool
  96.     {
  97.         $localeConstraint = new Locale(['canonicalize' => true]);
  98.         $errors $this->validator->validate($locale$localeConstraint);
  99.         return == count($errors);
  100.     }
  101. }