<?php
namespace App\EventSubscriber;
use App\Controller\Admin\AdminController;
use App\Translation\AdminTranslator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class TranslatableSubscriber.
*
* The TranslatableSubscriber listens on the `kernel.controller` event to set the locale of the admin translator for
* correct translations in the admin.
*/
class TranslatableSubscriber implements EventSubscriberInterface
{
private AdminTranslator $adminTranslator;
public function __construct(AdminTranslator $adminTranslator)
{
$this->adminTranslator = $adminTranslator;
}
/**
* Sets the locale of the request from an admin controller to the `admin_locale`.
*/
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
// when a controller class defines multiple action methods, the controller
// is returned as [$controllerInstance, 'methodName']
if (\is_array($controller)) {
$controller = $controller[0];
}
if ($controller instanceof AdminController) {
$this->adminTranslator->parseLocale($event->getRequest());
}
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}