src/EventSubscriber/RuntimeHtmlTranslationSubscriber.php line 25
<?phpnamespace App\EventSubscriber;use App\Service\RuntimeHtmlTranslationService;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;final class RuntimeHtmlTranslationSubscriber implements EventSubscriberInterface{public function __construct(private readonly RuntimeHtmlTranslationService $runtimeHtmlTranslationService,) {}public static function getSubscribedEvents(): array{return [KernelEvents::RESPONSE => ['onKernelResponse', -32],];}public function onKernelResponse(ResponseEvent $event): void{if (!$event->isMainRequest()) {return;}$request = $event->getRequest();$locale = strtolower(substr((string) $request->getLocale(), 0, 2));if ($locale !== 'en') {return;}$path = (string) $request->getPathInfo();if (str_starts_with($path, '/_profiler') || str_starts_with($path, '/_wdt')) {return;}$response = $event->getResponse();if (!$response instanceof Response || $response->isRedirection()) {return;}$contentType = strtolower((string) $response->headers->get('Content-Type', ''));if ($contentType !== '' && !str_contains($contentType, 'text/html')) {return;}$content = $response->getContent();if (!is_string($content) || $content === '') {return;}$translated = $this->runtimeHtmlTranslationService->translateHtml($content, $locale);if ($translated !== $content) {$response->setContent($translated);}}}