vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/EventListener/TranslatorListener.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\WebsiteBundle\EventListener;
  11. use Sulu\Component\Localization\Localization;
  12. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Contracts\Translation\LocaleAwareInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class TranslatorListener implements EventSubscriberInterface
  19. {
  20. /**
  21. * @var TranslatorInterface|LocaleAwareInterface
  22. */
  23. private $translator;
  24. /**
  25. * @param TranslatorInterface|LocaleAwareInterface $translator
  26. */
  27. public function __construct($translator)
  28. {
  29. $this->translator = $translator;
  30. }
  31. public function onKernelRequest(RequestEvent $event)
  32. {
  33. $attributes = $event->getRequest()->attributes->get('_sulu');
  34. if (!$attributes instanceof RequestAttributes) {
  35. return;
  36. }
  37. $localization = $attributes->getAttribute('localization');
  38. if (!$localization instanceof Localization) {
  39. return;
  40. }
  41. if (!\method_exists($this->translator, 'setLocale')) {
  42. return;
  43. }
  44. $this->translator->setLocale($localization->getLocale(Localization::LCID));
  45. }
  46. public static function getSubscribedEvents()
  47. {
  48. return [
  49. // Set the translator locale in `de_AT` format instead of `de-at`
  50. KernelEvents::REQUEST => [['onKernelRequest', 14]],
  51. ];
  52. }
  53. }