diff --git a/Classes/Domain/Dto/CurrentPage.php b/Classes/Domain/Dto/CurrentPage.php new file mode 100644 index 00000000..2470feb9 --- /dev/null +++ b/Classes/Domain/Dto/CurrentPage.php @@ -0,0 +1,26 @@ +cache = $cache; $this->client = $client; - $this->glossaryRepository = $glossaryRepository; $this->processingInstruction = $processingInstruction; } @@ -61,22 +59,20 @@ public function translateRequest( public function translateContent(TranslateContext $translateContext): string { if ($this->processingInstruction->isDeeplMode() === false) { - // @todo Can be replaced with `$this->logger?->` when TYPO3 v11 and therefore PHP 7.4/8.0 support is dropped. - if ($this->logger !== null) { - $this->logger->warning('DeepL mode not set. Exit.'); - } + $this->logger?->warning('DeepL mode not set. Exit.'); return $translateContext->getContent(); } // 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( + $glossaryEvent = $this->eventDispatcher->dispatch(new DeepLGlossaryIdEvent( $translateContext->getSourceLanguageCode(), $translateContext->getTargetLanguageCode(), DeeplBackendUtility::detectCurrentPage($this->processingInstruction) - ); - - $translateContext->setGlossaryId($glossary['glossary_id']); + )); + $glossaryId = $glossaryEvent->glossaryId; + if ($glossaryId !== '') { + $translateContext->setGlossaryId($glossaryId); + } } try { @@ -93,10 +89,7 @@ public function translateContent(TranslateContext $translateContext): string } if ($response === null) { - // @todo Can be replaced with `$this->logger?->` when TYPO3 v11 and therefore PHP 7.4/8.0 support is dropped. - if ($this->logger !== null) { - $this->logger->warning('Translation not successful'); - } + $this->logger?->warning('Translation not successful'); return ''; } diff --git a/Classes/Utility/DeeplBackendUtility.php b/Classes/Utility/DeeplBackendUtility.php index 89de1e21..741508db 100644 --- a/Classes/Utility/DeeplBackendUtility.php +++ b/Classes/Utility/DeeplBackendUtility.php @@ -20,23 +20,25 @@ 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; -// @todo Make class final. Overriding a static utility class does not make much sense, but better to enforce it. -class DeeplBackendUtility +/** + * Utility helper methods for DeepL-translate + * + * Main entry point for detecting API key and current working page + */ +final class DeeplBackendUtility { private static string $apiKey = ''; private static bool $configurationLoaded = false; - /** - * @var array{uid: int, title: string}|array - */ - protected static array $currentPage; + protected static ?CurrentPage $currentPage = null; /** * @return string @@ -127,16 +129,6 @@ public static function buildBackendRoute(string $route, array $parameters): stri return (string)$uriBuilder->buildUriFromRoute($route, $parameters); } - /** - * @deprecated This function will no longer be used and will be removed in a later version please use it \WebVision\Deepltranslate\Core\Service\IconOverlayGenerator - * @see IconOverlayGenerator::get() - */ - public static function getIcon(string $iconFlag): Icon - { - $iconOverlayGenerator = GeneralUtility::makeInstance(IconOverlayGenerator::class); - return $iconOverlayGenerator->get($iconFlag); - } - /** * ToDo: Migrated function to own class object "WebVision\Deepltranslate\Core\Form\TranslationDropdownGenerator" */ @@ -233,20 +225,11 @@ public static function checkCanBeTranslated(int $pageId, int $languageId): bool return true; } - private static function getBackendUserAuthentication(): BackendUserAuthentication - { - 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 = []; - + $pageId = null; if ($processingInstruction->getProcessingTable() === 'pages') { - self::$currentPage = self::getPageRecord((int)$processingInstruction->getProcessingId()); + $pageId = (int)$processingInstruction->getProcessingId(); } elseif ( $processingInstruction->getProcessingTable() !== null && strlen($processingInstruction->getProcessingTable()) > 0 @@ -256,23 +239,34 @@ public static function detectCurrentPage(ProcessingInstruction $processingInstru (string)$processingInstruction->getProcessingTable(), (int)$processingInstruction->getProcessingId() ); - self::$currentPage = self::getPageRecord($pageId); + } + if ($pageId !== null && $pageId > 0) { + $pageRecord = self::getPageRecord($pageId); + if ($pageRecord !== null) { + self::$currentPage = new CurrentPage((int)$pageRecord['uid'], (string)$pageRecord['title']); + } } return self::$currentPage; } + private static function getBackendUserAuthentication(): BackendUserAuthentication + { + return $GLOBALS['BE_USER']; + } + /** - * @return array{uid: int, title: string}|array + * @return array{uid: int, title: string}|null */ - private static function getPageRecord(int $id): array + private static function getPageRecord(int $id): ?array { + /** @var array{uid: int, title: string}|null $page */ $page = BackendUtility::getRecord( 'pages', $id, 'uid, title' ); - return $page ?? []; + return $page; } private static function getPageIdFromRecord(string $table, int $id): int