src/EventListener/TerminologiesListener.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Services\Admin\ContextService;
  4. use App\Twig\TranslationExtension;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Class TerminologiesListener.
  11.  */
  12. class TerminologiesListener implements EventSubscriberInterface
  13. {
  14.     // TODO : Récupérer valeur de la config
  15.     public const DEFAULT_LOCALE 'fr';
  16.     /**
  17.      * @var TranslationExtension
  18.      */
  19.     protected TranslationExtension $translationExtension;
  20.     /**
  21.      * @var ContextService
  22.      */
  23.     protected ContextService $contextService;
  24.     public function __construct(
  25.         TranslationExtension $translationExtension,
  26.         ContextService $contextService
  27.     ) {
  28.         $this->translationExtension $translationExtension;
  29.         $this->contextService $contextService;
  30.     }
  31.     /**
  32.      * @param RequestEvent $event
  33.      */
  34.     public function onKernelController(ControllerEvent $event): void
  35.     {
  36.         $request $event->getRequest();
  37.         $local $request->getLocale();
  38.         $dataCollectorTranslator $this->translationExtension->getInner()->getTranslator();
  39.         $customer $this->contextService->getCustomer();
  40.         if (false === $customer) {
  41.             return;
  42.         }
  43.         $customizations $customer->getCustomizations();
  44.         foreach (['fr''en'] as $local) {
  45.             $currentCatalogue $dataCollectorTranslator->getCatalogue($local);
  46.             $messages $currentCatalogue->all()['messages'];
  47.             $currentCatalogue->add($messages'original-content');
  48.             if (!empty($customizations['terminologies'])) {
  49.                 $terminologiesToAdd = [];
  50.                 foreach ($customizations['terminologies'] as $key => $data) {
  51.                     $key str_replace('_''.'$key);
  52.                     $key str_replace('-''_'$key);
  53.                     foreach ($data[$local] as $domination => $value) {
  54.                         if (null !== $currentTerminologie array_shift($value)) {
  55.                             $translationKey $key '.' $domination;
  56.                             $terminologiesToAdd[$translationKey] = $currentTerminologie;
  57.                         }
  58.                     }
  59.                 }
  60.                 $currentCatalogue->add($terminologiesToAdd'messages+intl-icu');
  61.             }
  62.         }
  63.     }
  64.     /**
  65.      * @return array
  66.      */
  67.     public static function getSubscribedEvents(): array
  68.     {
  69.         return [
  70.             KernelEvents::CONTROLLER => [
  71.                 ['onKernelController'15],
  72.             ],
  73.         ];
  74.     }
  75. }