55namespace WebVision \Deepltranslate \Core \Hooks ;
66
77use Symfony \Component \DependencyInjection \Attribute \Autoconfigure ;
8+ use TYPO3 \CMS \Backend \Utility \BackendUtility ;
89use TYPO3 \CMS \Core \DataHandling \DataHandler ;
910use TYPO3 \CMS \Core \Exception \SiteNotFoundException ;
1011use TYPO3 \CMS \Core \Site \SiteFinder ;
1112use TYPO3 \CMS \Core \Type \ContextualFeedbackSeverity ;
1213use TYPO3 \CMS \Core \Utility \GeneralUtility ;
14+ use TYPO3 \CMS \Core \Utility \MathUtility ;
15+ use WebVision \Deepltranslate \Core \Exception \InvalidArgumentException ;
1316use WebVision \Deepltranslate \Core \Exception \LanguageIsoCodeNotFoundException ;
1417use 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
0 commit comments