Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions Classes/Hooks/AbstractTranslateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'])
Expand All @@ -97,14 +81,15 @@ protected function createTranslateContext(string $content, int $targetLanguageUi
return $context;
}

protected function findCurrentParentPage(string $tableName, int $currentRecordId): int
/**
* @param array<string, mixed> $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;
Expand Down
48 changes: 39 additions & 9 deletions Classes/Hooks/TranslateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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);

Expand Down
21 changes: 17 additions & 4 deletions Classes/Service/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
Expand Down