diff --git a/Classes/Hooks/AbstractTranslateHook.php b/Classes/Hooks/AbstractTranslateHook.php index 0a86fb9..dae10d3 100644 --- a/Classes/Hooks/AbstractTranslateHook.php +++ b/Classes/Hooks/AbstractTranslateHook.php @@ -4,17 +4,14 @@ namespace WebVision\Deepltranslate\Core\Hooks; -use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; -use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; use TYPO3\CMS\Core\Utility\GeneralUtility; use WebVision\Deepltranslate\Core\Domain\Dto\TranslateContext; use WebVision\Deepltranslate\Core\Domain\Repository\PageRepository; -use WebVision\Deepltranslate\Core\Exception\InvalidArgumentException; use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException; use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException; use WebVision\Deepltranslate\Core\Service\DeeplService; @@ -62,31 +59,18 @@ public function translateContent( /** * @internal * + * @param array{uid: int, title: string, language_isocode: string, languageCode: string} $sourceLanguageRecord + * @param array{uid: int, title: string, language_isocode: string, languageCode: string, formality: string} $targetLanguageRecord + * * @throws LanguageRecordNotFoundException * @throws LanguageIsoCodeNotFoundException */ - protected function createTranslateContext(string $content, int $targetLanguageUid, Site $site): TranslateContext + protected function createTranslateContext(string $content, array $sourceLanguageRecord, array $targetLanguageRecord): TranslateContext { $context = new TranslateContext($content); - - $sourceLanguageRecord = $this->languageService->getSourceLanguage($site); - $context->setSourceLanguageCode($sourceLanguageRecord['languageCode']); - - try { - $targetLanguageRecord = $this->languageService->getTargetLanguage($site, $targetLanguageUid); - } catch (\Throwable $e) { - throw new InvalidArgumentException( - sprintf( - 'The target language is not DeepL supported. Possibly wrong Site configuration. Message: %s', - $e->getMessage(), - ), - 1746962367, - $e, - ); - } - $context->setTargetLanguageCode($targetLanguageRecord['languageCode']); + if ( $targetLanguageRecord['formality'] !== '' && $this->deeplService->hasLanguageFormalitySupport($targetLanguageRecord['languageCode']) @@ -97,14 +81,15 @@ protected function createTranslateContext(string $content, int $targetLanguageUi return $context; } - protected function findCurrentParentPage(string $tableName, int $currentRecordId): int + /** + * @param array $currentRecord + */ + protected function findCurrentParentPage(string $tableName, array $currentRecord): int { if ($tableName === 'pages') { - $pageId = $currentRecordId; + $pageId = $currentRecord['uid']; } else { - /** @var array{pid: int|string} $currentPageRecord */ - $currentPageRecord = BackendUtility::getRecord($tableName, $currentRecordId); - $pageId = (int)$currentPageRecord['pid']; + $pageId = (int)$currentRecord['pid']; } return $pageId; diff --git a/Classes/Hooks/TranslateHook.php b/Classes/Hooks/TranslateHook.php index 59fabbd..2485964 100644 --- a/Classes/Hooks/TranslateHook.php +++ b/Classes/Hooks/TranslateHook.php @@ -5,11 +5,14 @@ namespace WebVision\Deepltranslate\Core\Hooks; use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; +use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Exception\SiteNotFoundException; use TYPO3\CMS\Core\Site\SiteFinder; use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\MathUtility; +use WebVision\Deepltranslate\Core\Exception\InvalidArgumentException; use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException; use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException; @@ -27,33 +30,46 @@ public function processTranslateTo_copyAction( array $languageRecord, DataHandler $dataHandler ): void { - // Table Information are importen to find deepl configuration for site + if (MathUtility::canBeInterpretedAsInteger($content)) { + return; + } + + // Translation mode not set to DeepL translate skip the translation + if ($this->processingInstruction->isDeeplMode() === false) { + return; + } + + // Table Information are important to find deepl configuration for site $tableName = $this->processingInstruction->getProcessingTable(); if ($tableName === null) { return; } - // Record Information are importen to find deepl configuration for site + // Record Information are important to find deepl configuration for site $currentRecordId = $this->processingInstruction->getProcessingId(); if ($currentRecordId === null) { return; } - // Wenn you will translate file metadata use the extension "web-vision/deepltranslate-assets" + // When you will translate file metadata use the extension "web-vision/deepltranslate-assets" if ($tableName === 'sys_file_metadata') { return; } - // Translation mode not set to DeepL translate skip the translation - if ($this->processingInstruction->isDeeplMode() === false) { + $translatedContent = ''; + + $currentRecord = BackendUtility::getRecord($tableName, $currentRecordId); + if ($currentRecord === null) { return; } - $translatedContent = ''; - - $pageId = $this->findCurrentParentPage($tableName, (int)$currentRecordId); + $currentRecordLanguage = 0; + $pageId = $this->findCurrentParentPage($tableName, $currentRecord); try { $siteInformation = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId); + if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) { + $currentRecordLanguage = $currentRecord[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']]; + } } catch (SiteNotFoundException $e) { $siteInformation = null; } @@ -63,7 +79,21 @@ public function processTranslateTo_copyAction( } try { - $translatedContext = $this->createTranslateContext($content, (int)$languageRecord['uid'], $siteInformation); + $sourceLanguageRecord = $this->languageService->getSourceLanguage($siteInformation, (int)$currentRecordLanguage); + $targetLanguageRecord = $this->languageService->getTargetLanguage($siteInformation, (int)$languageRecord['uid']); + } catch (\Throwable $e) { + throw new InvalidArgumentException( + sprintf( + 'The target language is not DeepL supported. Possibly wrong Site configuration. Message: %s', + $e->getMessage(), + ), + 1764160649, + $e, + ); + } + + try { + $translatedContext = $this->createTranslateContext($content, $sourceLanguageRecord, $targetLanguageRecord); $translatedContent = $this->deeplService->translateContent($translatedContext); diff --git a/Classes/Service/LanguageService.php b/Classes/Service/LanguageService.php index 352a559..19bc707 100644 --- a/Classes/Service/LanguageService.php +++ b/Classes/Service/LanguageService.php @@ -28,12 +28,25 @@ public function __construct( /** * @return array{uid: int, title: string, language_isocode: string, languageCode: string} */ - public function getSourceLanguage(Site $currentSite): array + public function getSourceLanguage(Site $currentSite, int $languageId = 0): array { - $languageIsoCode = $currentSite->getDefaultLanguage()->getLocale()->getLanguageCode(); + try { + $language = $currentSite->getLanguageById($languageId); + } catch (\Exception $e) { + if ($e->getCode() === 1522960188) { + throw new LanguageRecordNotFoundException( + sprintf('Language "%d" in site "%s" not found.', $languageId, $currentSite->getIdentifier()), + 1764155930, + $e, + ); + } + throw $e; + } + + $languageIsoCode = $language->getLocale()->getLanguageCode(); $sourceLanguageRecord = [ - 'uid' => $currentSite->getDefaultLanguage()->getLanguageId(), - 'title' => $currentSite->getDefaultLanguage()->getTitle(), + 'uid' => $language->getLanguageId(), + 'title' => $language->getTitle(), 'language_isocode' => strtoupper($languageIsoCode), 'languageCode' => strtoupper($languageIsoCode), ];