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

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\Component\Translation\Translator;
  17. use Symfony\Contracts\Translation\LocaleAwareInterface;
  18. class TranslatorListener implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var Translator|LocaleAwareInterface
  22.      */
  23.     private $translator;
  24.     public function __construct($translator)
  25.     {
  26.         $this->translator $translator;
  27.     }
  28.     public function onKernelRequest(RequestEvent $event)
  29.     {
  30.         $attributes $event->getRequest()->attributes->get('_sulu');
  31.         if (!$attributes instanceof RequestAttributes) {
  32.             return;
  33.         }
  34.         $localization $attributes->getAttribute('localization');
  35.         if (!$localization instanceof Localization) {
  36.             return;
  37.         }
  38.         $this->translator->setLocale($localization->getLocale(Localization::LCID));
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             // Set the translator locale in `de_AT` format instead of `de-at`
  44.             KernelEvents::REQUEST => [['onKernelRequest'14]],
  45.         ];
  46.     }
  47. }