From ec04cb046314b5df485b44d124f513eb128475e2 Mon Sep 17 00:00:00 2001 From: mhofmann Date: Tue, 17 Dec 2024 13:26:29 +0100 Subject: [PATCH] [FEATURE] Add Event for Glossary detection --- Classes/Domain/Dto/CurrentPage.php | 41 +++++++++++++++++ Classes/Event/GlossaryKeyEvent.php | 44 +++++++++++++++++++ .../Exception/MissingParameterException.php | 9 ++++ Classes/Service/DeeplService.php | 22 +++++----- Classes/Utility/DeeplBackendUtility.php | 18 +++----- 5 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 Classes/Domain/Dto/CurrentPage.php create mode 100644 Classes/Event/GlossaryKeyEvent.php create mode 100644 Classes/Exception/MissingParameterException.php diff --git a/Classes/Domain/Dto/CurrentPage.php b/Classes/Domain/Dto/CurrentPage.php new file mode 100644 index 00000000..8be0a4cd --- /dev/null +++ b/Classes/Domain/Dto/CurrentPage.php @@ -0,0 +1,41 @@ +glossaryId = $glossaryId; + } + + public function getGlossaryId(): ?string + { + return $this->glossaryId; + } + + public function getSourceLanguage(): string + { + return $this->sourceLanguage; + } + + public function getTargetLanguage(): string + { + return $this->targetLanguage; + } + + public function getCurrentPage(): ?CurrentPage + { + return $this->currentPage; + } +} diff --git a/Classes/Exception/MissingParameterException.php b/Classes/Exception/MissingParameterException.php new file mode 100644 index 00000000..c05ab55d --- /dev/null +++ b/Classes/Exception/MissingParameterException.php @@ -0,0 +1,9 @@ +cache = $cache; $this->client = $client; - $this->glossaryRepository = $glossaryRepository; $this->processingInstruction = $processingInstruction; } @@ -69,14 +67,16 @@ public function translateContent(TranslateContext $translateContext): string } // If the source language is set to Autodetect, no glossary can be detected. if ($translateContext->getSourceLanguageCode() !== null) { - // @todo Make glossary findable by current site. - $glossary = $this->glossaryRepository->getGlossaryBySourceAndTarget( + /** @var GlossaryKeyEvent $glossaryEvent */ + $glossaryEvent = $this->eventDispatcher->dispatch(new GlossaryKeyEvent( $translateContext->getSourceLanguageCode(), $translateContext->getTargetLanguageCode(), DeeplBackendUtility::detectCurrentPage($this->processingInstruction) - ); - - $translateContext->setGlossaryId($glossary['glossary_id']); + )); + $glossaryId = $glossaryEvent->getGlossaryId(); + if ($glossaryId !== null) { + $translateContext->setGlossaryId($glossaryId); + } } try { diff --git a/Classes/Utility/DeeplBackendUtility.php b/Classes/Utility/DeeplBackendUtility.php index 89de1e21..caca3fa0 100644 --- a/Classes/Utility/DeeplBackendUtility.php +++ b/Classes/Utility/DeeplBackendUtility.php @@ -20,11 +20,13 @@ use TYPO3\CMS\Core\Utility\MathUtility; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; use WebVision\Deepltranslate\Core\Configuration; +use WebVision\Deepltranslate\Core\Domain\Dto\CurrentPage; use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException; use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException; use WebVision\Deepltranslate\Core\Service\IconOverlayGenerator; use WebVision\Deepltranslate\Core\Service\LanguageService; use WebVision\Deepltranslate\Core\Service\ProcessingInstruction; +use WebVision\Deepltranslate\Core\Domain\Dto\CurrentPage; // @todo Make class final. Overriding a static utility class does not make much sense, but better to enforce it. class DeeplBackendUtility @@ -33,10 +35,7 @@ class DeeplBackendUtility private static bool $configurationLoaded = false; - /** - * @var array{uid: int, title: string}|array - */ - protected static array $currentPage; + protected static ?CurrentPage $currentPage = null; /** * @return string @@ -238,15 +237,10 @@ private static function getBackendUserAuthentication(): BackendUserAuthenticatio return $GLOBALS['BE_USER']; } - /** - * @return array{uid: int, title: string}|array - */ - public static function detectCurrentPage(ProcessingInstruction $processingInstruction): array + public static function detectCurrentPage(ProcessingInstruction $processingInstruction): ?CurrentPage { - self::$currentPage = []; - if ($processingInstruction->getProcessingTable() === 'pages') { - self::$currentPage = self::getPageRecord((int)$processingInstruction->getProcessingId()); + self::$currentPage = CurrentPage::fromArray(self::getPageRecord((int)$processingInstruction->getProcessingId())); } elseif ( $processingInstruction->getProcessingTable() !== null && strlen($processingInstruction->getProcessingTable()) > 0 @@ -256,7 +250,7 @@ public static function detectCurrentPage(ProcessingInstruction $processingInstru (string)$processingInstruction->getProcessingTable(), (int)$processingInstruction->getProcessingId() ); - self::$currentPage = self::getPageRecord($pageId); + self::$currentPage = CurrentPage::fromArray(self::getPageRecord($pageId)); } return self::$currentPage;