<?php
namespace App\EventSubscriber;
use App\Translation\RuntimeTranslator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class CustomTranslationSubscriber implements EventSubscriberInterface
{
private RuntimeTranslator $translator;
public function __construct(RuntimeTranslator $translator)
{
$this->translator = $translator;
}
public function onKernelRequest(RequestEvent $event)
{
if (false === $event->isMasterRequest() || str_starts_with($event->getRequest()->getPathInfo(), '/admin')) {
return;
}
$viewId = 1;
$locale = $event->getRequest()->getLocale(); // not needed, can extract from view
$this->translator->addRuntimeResource('db', $viewId, $locale);
}
public static function getSubscribedEvents(): array
{
return [
'kernel.request' => 'onKernelRequest',
];
}
}