src/Service/CustomTranslation/EventListener/SetCustomTranslationListener.php line 54

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\CustomTranslation\EventListener;
  3. use Exception;
  4. use Harmonizely\Service\CustomTranslation\Contract\IGetCustomTranslationCatalogService;
  5. use Harmonizely\Service\CustomTranslation\Event\SetCustomTranslationEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Translation\MessageCatalogue;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. class SetCustomTranslationListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var TranslatorInterface
  13.      */
  14.     private TranslatorInterface $translator;
  15.     /**
  16.      * @var IGetCustomTranslationCatalogService
  17.      */
  18.     private IGetCustomTranslationCatalogService $getCustomTranslationCatalogService;
  19.     /**
  20.      * @param TranslatorInterface $translator
  21.      * @param IGetCustomTranslationCatalogService $getCustomTranslationCatalogService
  22.      */
  23.     public function __construct(
  24.         TranslatorInterface $translator,
  25.         IGetCustomTranslationCatalogService $getCustomTranslationCatalogService
  26.     )
  27.     {
  28.         $this->translator $translator;
  29.         $this->getCustomTranslationCatalogService $getCustomTranslationCatalogService;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             SetCustomTranslationEvent::NAME => 'onSetCustomTranslation',
  38.         ];
  39.     }
  40.     /**
  41.      * @param SetCustomTranslationEvent $event
  42.      * @throws Exception
  43.      */
  44.     public function onSetCustomTranslation(SetCustomTranslationEvent $event): void
  45.     {
  46.         $customTranslationCatalog $this->getCustomTranslationCatalogService->perform(
  47.             $event->getUserId(),
  48.             $event->getOrganizationId()
  49.         );
  50.         /** @var MessageCatalogue $catalogue */
  51.         $catalogue $this->translator->getCatalogue();
  52.         $domains $catalogue->getDomains();
  53.         foreach ($customTranslationCatalog->getEntries() as $entry) {
  54.             foreach ($domains as $domain) {
  55.                 $catalogue->set($entry->getMsgId(), $entry->getMsgStr(), $domain);
  56.             }
  57.         }
  58.     }
  59. }