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

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) {
  62. if ($request->getLanguages() && $preferredLanguage = $request->getPreferredLanguage($this->enabledLocales)) {
  63. $request->setLocale($preferredLanguage);
  64. }
  65. $request->attributes->set('_vary_by_language', true);
  66. }
  67. }
  68. private function setRouterContext(Request $request)
  69. {
  70. if (null !== $this->router) {
  71. $this->router->getContext()->setParameter('_locale', $request->getLocale());
  72. }
  73. }
  74. public static function getSubscribedEvents(): array
  75. {
  76. return [
  77. KernelEvents::REQUEST => [
  78. ['setDefaultLocale', 100],
  79. // must be registered after the Router to have access to the _locale
  80. ['onKernelRequest', 16],
  81. ],
  82. KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', 0]],
  83. ];
  84. }
  85. }