vendor/ibexa/admin-ui/src/lib/EventListener/InContextTranslationListener.php line 54

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\AdminUi\EventListener;
  8. use Ibexa\AdminUi\Specification\SiteAccess\IsAdmin;
  9. use Ibexa\AdminUi\UserSetting\InContextTranslation;
  10. use Ibexa\User\UserSetting\UserSettingService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. final class InContextTranslationListener implements EventSubscriberInterface
  18. {
  19.     private const ACHOLI_LANG 'ach-UG';
  20.     private UserSettingService $userSettingService;
  21.     /** @var string[] */
  22.     private array $siteAccessGroups;
  23.     private TranslatorInterface $translator;
  24.     public function __construct(
  25.         array $siteAccessGroups,
  26.         UserSettingService $userSettingService,
  27.         TranslatorInterface $translator
  28.     ) {
  29.         $this->siteAccessGroups $siteAccessGroups;
  30.         $this->userSettingService $userSettingService;
  31.         $this->translator $translator;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::REQUEST => [
  40.                 ['setInContextTranslation'5],
  41.             ],
  42.         ];
  43.     }
  44.     public function setInContextTranslation(RequestEvent $event): void
  45.     {
  46.         $request $event->getRequest();
  47.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType() || !$this->isAdminSiteAccess($request)) {
  48.             return;
  49.         }
  50.         $inContextSetting $this->userSettingService->getUserSetting('in_context_translation')->getValue();
  51.         if ($inContextSetting !== InContextTranslation::ENABLED_OPTION) {
  52.             return;
  53.         }
  54.         $request->setLocale(self::ACHOLI_LANG);
  55.         $request->attributes->set('_locale'self::ACHOLI_LANG);
  56.         $this->translator->setLocale(self::ACHOLI_LANG);
  57.     }
  58.     private function isAdminSiteAccess(Request $request): bool
  59.     {
  60.         return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($request->attributes->get('siteaccess'));
  61.     }
  62. }