Skip to content

Commit 0ba1c7f

Browse files
committed
[BUGFIX] Correctly resolve source language on translation context
1 parent 7d06946 commit 0ba1c7f

File tree

3 files changed

+67
-39
lines changed

3 files changed

+67
-39
lines changed

Classes/Hooks/AbstractTranslateHook.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
namespace WebVision\Deepltranslate\Core\Hooks;
66

7-
use TYPO3\CMS\Backend\Utility\BackendUtility;
87
use TYPO3\CMS\Core\Core\Environment;
98
use TYPO3\CMS\Core\DataHandling\DataHandler;
109
use TYPO3\CMS\Core\Messaging\FlashMessage;
1110
use TYPO3\CMS\Core\Messaging\FlashMessageService;
12-
use TYPO3\CMS\Core\Site\Entity\Site;
1311
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
1412
use TYPO3\CMS\Core\Utility\GeneralUtility;
1513
use WebVision\Deepltranslate\Core\Domain\Dto\TranslateContext;
1614
use WebVision\Deepltranslate\Core\Domain\Repository\PageRepository;
17-
use WebVision\Deepltranslate\Core\Exception\InvalidArgumentException;
1815
use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException;
1916
use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException;
2017
use WebVision\Deepltranslate\Core\Service\DeeplService;
@@ -62,31 +59,18 @@ public function translateContent(
6259
/**
6360
* @internal
6461
*
62+
* @param array{uid: int, title: string, language_isocode: string, languageCode: string} $sourceLanguageRecord
63+
* @param array{uid: int, title: string, language_isocode: string, languageCode: string, formality: string} $targetLanguageRecord
64+
*
6565
* @throws LanguageRecordNotFoundException
6666
* @throws LanguageIsoCodeNotFoundException
6767
*/
68-
protected function createTranslateContext(string $content, int $targetLanguageUid, Site $site): TranslateContext
68+
protected function createTranslateContext(string $content, array $sourceLanguageRecord, array $targetLanguageRecord): TranslateContext
6969
{
7070
$context = new TranslateContext($content);
71-
72-
$sourceLanguageRecord = $this->languageService->getSourceLanguage($site);
73-
7471
$context->setSourceLanguageCode($sourceLanguageRecord['languageCode']);
75-
76-
try {
77-
$targetLanguageRecord = $this->languageService->getTargetLanguage($site, $targetLanguageUid);
78-
} catch (\Throwable $e) {
79-
throw new InvalidArgumentException(
80-
sprintf(
81-
'The target language is not DeepL supported. Possibly wrong Site configuration. Message: %s',
82-
$e->getMessage(),
83-
),
84-
1746962367,
85-
$e,
86-
);
87-
}
88-
8972
$context->setTargetLanguageCode($targetLanguageRecord['languageCode']);
73+
9074
if (
9175
$targetLanguageRecord['formality'] !== ''
9276
&& $this->deeplService->hasLanguageFormalitySupport($targetLanguageRecord['languageCode'])
@@ -97,14 +81,15 @@ protected function createTranslateContext(string $content, int $targetLanguageUi
9781
return $context;
9882
}
9983

100-
protected function findCurrentParentPage(string $tableName, int $currentRecordId): int
84+
/**
85+
* @param array<string, mixed> $currentRecord
86+
*/
87+
protected function findCurrentParentPage(string $tableName, array $currentRecord): int
10188
{
10289
if ($tableName === 'pages') {
103-
$pageId = $currentRecordId;
90+
$pageId = $currentRecord['uid'];
10491
} else {
105-
/** @var array{pid: int|string} $currentPageRecord */
106-
$currentPageRecord = BackendUtility::getRecord($tableName, $currentRecordId);
107-
$pageId = (int)$currentPageRecord['pid'];
92+
$pageId = (int)$currentRecord['pid'];
10893
}
10994

11095
return $pageId;

Classes/Hooks/TranslateHook.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
namespace WebVision\Deepltranslate\Core\Hooks;
66

77
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
8+
use TYPO3\CMS\Backend\Utility\BackendUtility;
89
use TYPO3\CMS\Core\DataHandling\DataHandler;
910
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
1011
use TYPO3\CMS\Core\Site\SiteFinder;
1112
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
1213
use TYPO3\CMS\Core\Utility\GeneralUtility;
14+
use TYPO3\CMS\Core\Utility\MathUtility;
15+
use WebVision\Deepltranslate\Core\Exception\InvalidArgumentException;
1316
use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException;
1417
use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException;
1518

@@ -27,33 +30,46 @@ public function processTranslateTo_copyAction(
2730
array $languageRecord,
2831
DataHandler $dataHandler
2932
): void {
30-
// Table Information are importen to find deepl configuration for site
33+
if (MathUtility::canBeInterpretedAsInteger($content)) {
34+
return;
35+
}
36+
37+
// Translation mode not set to DeepL translate skip the translation
38+
if ($this->processingInstruction->isDeeplMode() === false) {
39+
return;
40+
}
41+
42+
// Table Information are important to find deepl configuration for site
3143
$tableName = $this->processingInstruction->getProcessingTable();
3244
if ($tableName === null) {
3345
return;
3446
}
3547

36-
// Record Information are importen to find deepl configuration for site
48+
// Record Information are important to find deepl configuration for site
3749
$currentRecordId = $this->processingInstruction->getProcessingId();
3850
if ($currentRecordId === null) {
3951
return;
4052
}
4153

42-
// Wenn you will translate file metadata use the extension "web-vision/deepltranslate-assets"
54+
// When you will translate file metadata use the extension "web-vision/deepltranslate-assets"
4355
if ($tableName === 'sys_file_metadata') {
4456
return;
4557
}
4658

47-
// Translation mode not set to DeepL translate skip the translation
48-
if ($this->processingInstruction->isDeeplMode() === false) {
59+
$translatedContent = '';
60+
61+
$currentRecord = BackendUtility::getRecord($tableName, $currentRecordId);
62+
if ($currentRecord === null) {
4963
return;
5064
}
5165

52-
$translatedContent = '';
53-
54-
$pageId = $this->findCurrentParentPage($tableName, (int)$currentRecordId);
66+
$currentRecordLanguage = 0;
67+
$pageId = $this->findCurrentParentPage($tableName, $currentRecord);
5568
try {
5669
$siteInformation = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId);
70+
if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) {
71+
$currentRecordLanguage = $currentRecord[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']];
72+
}
5773
} catch (SiteNotFoundException $e) {
5874
$siteInformation = null;
5975
}
@@ -63,7 +79,21 @@ public function processTranslateTo_copyAction(
6379
}
6480

6581
try {
66-
$translatedContext = $this->createTranslateContext($content, (int)$languageRecord['uid'], $siteInformation);
82+
$sourceLanguageRecord = $this->languageService->getSourceLanguage($siteInformation, (int)$currentRecordLanguage);
83+
$targetLanguageRecord = $this->languageService->getTargetLanguage($siteInformation, (int)$languageRecord['uid']);
84+
} catch (\Throwable $e) {
85+
throw new InvalidArgumentException(
86+
sprintf(
87+
'The target language is not DeepL supported. Possibly wrong Site configuration. Message: %s',
88+
$e->getMessage(),
89+
),
90+
1764160649,
91+
$e,
92+
);
93+
}
94+
95+
try {
96+
$translatedContext = $this->createTranslateContext($content, $sourceLanguageRecord, $targetLanguageRecord);
6797

6898
$translatedContent = $this->deeplService->translateContent($translatedContext);
6999

Classes/Service/LanguageService.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@ public function __construct(
2828
/**
2929
* @return array{uid: int, title: string, language_isocode: string, languageCode: string}
3030
*/
31-
public function getSourceLanguage(Site $currentSite): array
31+
public function getSourceLanguage(Site $currentSite, int $languageId = 0): array
3232
{
33-
$languageIsoCode = $currentSite->getDefaultLanguage()->getLocale()->getLanguageCode();
33+
try {
34+
$language = $currentSite->getLanguageById($languageId);
35+
} catch (\Exception $e) {
36+
if ($e->getCode() === 1522960188) {
37+
throw new LanguageRecordNotFoundException(
38+
sprintf('Language "%d" in site "%s" not found.', $languageId, $currentSite->getIdentifier()),
39+
1764155930,
40+
$e,
41+
);
42+
}
43+
throw $e;
44+
}
45+
46+
$languageIsoCode = $language->getLocale()->getLanguageCode();
3447
$sourceLanguageRecord = [
35-
'uid' => $currentSite->getDefaultLanguage()->getLanguageId(),
36-
'title' => $currentSite->getDefaultLanguage()->getTitle(),
48+
'uid' => $language->getLanguageId(),
49+
'title' => $language->getTitle(),
3750
'language_isocode' => strtoupper($languageIsoCode),
3851
'languageCode' => strtoupper($languageIsoCode),
3952
];

0 commit comments

Comments
 (0)