src/EventSubscriber/CustomTranslationSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Translation\RuntimeTranslator;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. class CustomTranslationSubscriber implements EventSubscriberInterface
  7. {
  8.     private RuntimeTranslator $translator;
  9.     public function __construct(RuntimeTranslator $translator)
  10.     {
  11.         $this->translator $translator;
  12.     }
  13.     public function onKernelRequest(RequestEvent $event)
  14.     {
  15.         if (false === $event->isMasterRequest() || str_starts_with($event->getRequest()->getPathInfo(), '/admin')) {
  16.             return;
  17.         }
  18.         $viewId 1;
  19.         $locale $event->getRequest()->getLocale(); // not needed, can extract from view
  20.         $this->translator->addRuntimeResource('db'$viewId$locale);
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             'kernel.request' => 'onKernelRequest',
  26.         ];
  27.     }
  28. }