<?php
namespace App\EventListener;
use App\Services\Admin\ContextService;
use App\Twig\TranslationExtension;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class TerminologiesListener.
*/
class TerminologiesListener implements EventSubscriberInterface
{
// TODO : Récupérer valeur de la config
public const DEFAULT_LOCALE = 'fr';
/**
* @var TranslationExtension
*/
protected TranslationExtension $translationExtension;
/**
* @var ContextService
*/
protected ContextService $contextService;
public function __construct(
TranslationExtension $translationExtension,
ContextService $contextService
) {
$this->translationExtension = $translationExtension;
$this->contextService = $contextService;
}
/**
* @param RequestEvent $event
*/
public function onKernelController(ControllerEvent $event): void
{
$request = $event->getRequest();
$local = $request->getLocale();
$dataCollectorTranslator = $this->translationExtension->getInner()->getTranslator();
$customer = $this->contextService->getCustomer();
if (false === $customer) {
return;
}
$customizations = $customer->getCustomizations();
foreach (['fr', 'en'] as $local) {
$currentCatalogue = $dataCollectorTranslator->getCatalogue($local);
$messages = $currentCatalogue->all()['messages'];
$currentCatalogue->add($messages, 'original-content');
if (!empty($customizations['terminologies'])) {
$terminologiesToAdd = [];
foreach ($customizations['terminologies'] as $key => $data) {
$key = str_replace('_', '.', $key);
$key = str_replace('-', '_', $key);
foreach ($data[$local] as $domination => $value) {
if (null !== $currentTerminologie = array_shift($value)) {
$translationKey = $key . '.' . $domination;
$terminologiesToAdd[$translationKey] = $currentTerminologie;
}
}
}
$currentCatalogue->add($terminologiesToAdd, 'messages+intl-icu');
}
}
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => [
['onKernelController', 15],
],
];
}
}