src/EventSubscriber/TranslatableSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\Admin\AdminController;
  4. use App\Translation\AdminTranslator;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. /**
  9.  * Class TranslatableSubscriber.
  10.  *
  11.  * The TranslatableSubscriber listens on the `kernel.controller` event to set the locale of the admin translator for
  12.  * correct translations in the admin.
  13.  */
  14. class TranslatableSubscriber implements EventSubscriberInterface
  15. {
  16.     private AdminTranslator $adminTranslator;
  17.     public function __construct(AdminTranslator $adminTranslator)
  18.     {
  19.         $this->adminTranslator $adminTranslator;
  20.     }
  21.     /**
  22.      * Sets the locale of the request from an admin controller to the `admin_locale`.
  23.      */
  24.     public function onKernelController(ControllerEvent $event)
  25.     {
  26.         $controller $event->getController();
  27.         // when a controller class defines multiple action methods, the controller
  28.         // is returned as [$controllerInstance, 'methodName']
  29.         if (\is_array($controller)) {
  30.             $controller $controller[0];
  31.         }
  32.         if ($controller instanceof AdminController) {
  33.             $this->adminTranslator->parseLocale($event->getRequest());
  34.         }
  35.     }
  36.     /**
  37.      * {@inheritDoc}
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             KernelEvents::CONTROLLER => 'onKernelController',
  43.         ];
  44.     }
  45. }