src/EventSubscriber/TranslationSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class TranslationSubscriber implements EventSubscriberInterface
  9. {
  10.     private $translator;
  11.     private $params;
  12.     public function __construct(TranslatorInterface $translator, ParameterBagInterface $params)
  13.     {
  14.         $this->translator = $translator;
  15.         $this->params = $params;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         // return the subscribed events, their methods and priorities
  20.         return [
  21.             KernelEvents::CONTROLLER => 'onKernelController',
  22.         ];
  23.     }
  24.     public function onKernelController(ControllerEvent $event)
  25.     {
  26.         if ($this->params->get('allow.debug.translation') == 'y') {
  27.             $request = $event->getRequest();
  28.             $session = $request->getSession();
  29.             if ($request->get('debug_translation') == 'y') {
  30.                 $session->set('debug_translation', 'y');
  31.             } elseif ($request->get('debug_translation') == 'n') {
  32.                 $session->set('debug_translation', 'n');
  33.             }
  34.             if ($session->get('debug_translation') == 'y') {
  35.                 $messages = [];
  36.                 foreach ($this->translator->getCatalogue()->all('messages') as $id => $val) {
  37.                     $messages[$id] = '{' . str_replace('.', ': ', $id) . ':} ' . $val;
  38.                 }
  39.                 $this->translator->getCatalogue()->replace($messages, 'messages');    
  40.             }    
  41.         }
  42.     }
  43. }