<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class TranslationSubscriber implements EventSubscriberInterface
{
private $translator;
private $params;
public function __construct(TranslatorInterface $translator, ParameterBagInterface $params)
{
$this->translator = $translator;
$this->params = $params;
}
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event)
{
if ($this->params->get('allow.debug.translation') == 'y') {
$request = $event->getRequest();
$session = $request->getSession();
if ($request->get('debug_translation') == 'y') {
$session->set('debug_translation', 'y');
} elseif ($request->get('debug_translation') == 'n') {
$session->set('debug_translation', 'n');
}
if ($session->get('debug_translation') == 'y') {
$messages = [];
foreach ($this->translator->getCatalogue()->all('messages') as $id => $val) {
$messages[$id] = '{' . str_replace('.', ': ', $id) . ':} ' . $val;
}
$this->translator->getCatalogue()->replace($messages, 'messages');
}
}
}
}