vendor/symfony/http-kernel/EventListener/LocaleListener.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  15. use Symfony\Component\HttpKernel\Event\KernelEvent;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\RequestContextAwareInterface;
  19. /**
  20. * Initializes the locale based on the current request.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @final
  25. */
  26. class LocaleListener implements EventSubscriberInterface
  27. {
  28. private $router;
  29. private $defaultLocale;
  30. private $requestStack;
  31. private $useAcceptLanguageHeader;
  32. private $enabledLocales;
  33. public function __construct(RequestStack $requestStack, string $defaultLocale = 'en', RequestContextAwareInterface $router = null, bool $useAcceptLanguageHeader = false, array $enabledLocales = [])
  34. {
  35. $this->defaultLocale = $defaultLocale;
  36. $this->requestStack = $requestStack;
  37. $this->router = $router;
  38. $this->useAcceptLanguageHeader = $useAcceptLanguageHeader;
  39. $this->enabledLocales = $enabledLocales;
  40. }
  41. public function setDefaultLocale(KernelEvent $event)
  42. {
  43. $event->getRequest()->setDefaultLocale($this->defaultLocale);
  44. }
  45. public function onKernelRequest(RequestEvent $event)
  46. {
  47. $request = $event->getRequest();
  48. $this->setLocale($request);
  49. $this->setRouterContext($request);
  50. }
  51. public function onKernelFinishRequest(FinishRequestEvent $event)
  52. {
  53. if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
  54. $this->setRouterContext($parentRequest);
  55. }
  56. }
  57. private function setLocale(Request $request)
  58. {
  59. if ($locale = $request->attributes->get('_locale')) {
  60. $request->setLocale($locale);
  61. } elseif ($this->useAcceptLanguageHeader && $this->enabledLocales && ($preferredLanguage = $request->getPreferredLanguage($this->enabledLocales))) {
  62. $request->setLocale($preferredLanguage);
  63. $request->attributes->set('_vary_by_language', true);
  64. }
  65. }
  66. private function setRouterContext(Request $request)
  67. {
  68. if (null !== $this->router) {
  69. $this->router->getContext()->setParameter('_locale', $request->getLocale());
  70. }
  71. }
  72. public static function getSubscribedEvents(): array
  73. {
  74. return [
  75. KernelEvents::REQUEST => [
  76. ['setDefaultLocale', 100],
  77. // must be registered after the Router to have access to the _locale
  78. ['onKernelRequest', 16],
  79. ],
  80. KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', 0]],
  81. ];
  82. }
  83. }