Skip to content

Commit 2b8c084

Browse files
committed
[TASK] Refactor JavaScript implementation
The issue on TYPO3 13 having the button label writing outside the borders made a refactoring and regeneration of the localization.ts necessary. The labels were rewritten as CDATA to allow line breaks, getting rid of the too long labels. Together with TYPO3 Core standardized TSconfig for localizations the overridden JavaScript is more lightweight and doesn't need the AjaxRoute anymore. Therefore the AjaxRoutes.php and AjaxController.php are removed. In addition to Core functionality, the DeepL allow translateion now can be enabled/disabled by the following setting in Page TSConfig: ``` # Enabled (Default) mod.web_layout.localization.enableDeeplTranslate = 1 # Disabled mod.web_layout.localization.enableDeeplTranslate = 0 ``` This could be overridden in User TSconfig, too: ``` # Enabled (Default) page.mod.web_layout.localization.enableDeeplTranslate = 1 # Disabled page.mod.web_layout.localization.enableDeeplTranslate = 0 ```
1 parent 913acb6 commit 2b8c084

File tree

9 files changed

+59
-78
lines changed

9 files changed

+59
-78
lines changed

Classes/Configuration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WebVision\Deepltranslate\Core;
66

7+
use TYPO3\CMS\Backend\Utility\BackendUtility;
78
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
89
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
910
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
@@ -30,4 +31,20 @@ public function getApiKey(): string
3031
{
3132
return $this->apiKey;
3233
}
34+
35+
/**
36+
* Checks translation allowed against Page TSconfig from settings
37+
*
38+
* @param int $pageId
39+
* @return bool
40+
* @throws \JsonException
41+
*/
42+
public function isDeeplTranslationAllowedOnPage(int $pageId): bool
43+
{
44+
$localizationConfiguration = BackendUtility::getPagesTSconfig($pageId)['mod.']['web_layout.']['localization.'] ?? [];
45+
if ((bool)($localizationConfiguration['enableDeeplTranslate'] ?? true) === false) {
46+
return false;
47+
}
48+
return true;
49+
}
3350
}

Classes/ConfigurationInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
interface ConfigurationInterface
1414
{
1515
public function getApiKey(): string;
16+
17+
public function isDeeplTranslationAllowedOnPage(int $pageId): bool;
1618
}

Classes/Controller/Backend/AjaxController.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

Classes/Event/Listener/RenderLocalizationSelect.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77
use Psr\EventDispatcher\EventDispatcherInterface;
88
use TYPO3\CMS\Backend\Controller\Event\RenderAdditionalContentToRecordListEvent;
99
use TYPO3\CMS\Core\Site\Entity\Site;
10+
use WebVision\Deepltranslate\Core\ConfigurationInterface;
1011
use WebVision\Deepltranslate\Core\Event\RenderLocalizationSelectAllowed;
1112
use WebVision\Deepltranslate\Core\Form\TranslationDropdownGenerator;
1213

1314
final class RenderLocalizationSelect
1415
{
1516
public function __construct(
1617
private readonly TranslationDropdownGenerator $generator,
17-
private readonly EventDispatcherInterface $eventDispatcher
18+
private readonly EventDispatcherInterface $eventDispatcher,
19+
private readonly ConfigurationInterface $configuration
1820
) {
1921
}
2022

2123
public function __invoke(RenderAdditionalContentToRecordListEvent $event): void
2224
{
2325
$request = $event->getRequest();
26+
$currentPageId = (int)($request->getQueryParams()['id'] ?? 0);
27+
if (!$this->configuration->isDeeplTranslationAllowedOnPage($currentPageId)) {
28+
return;
29+
}
2430
// Check, if some event listener doesn't allow rendering here.
2531
// For use cases see Event
2632
$renderingAllowedEvent = $this->eventDispatcher->dispatch(new RenderLocalizationSelectAllowed($request));

Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
namespace WebVision\Deepltranslate\Core\ViewHelpers\Be\Access;
66

77
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
8+
use TYPO3\CMS\Core\Utility\GeneralUtility;
9+
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
810
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
911
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
1012
use WebVision\Deepltranslate\Core\Access\AllowedTranslateAccess;
13+
use WebVision\Deepltranslate\Core\ConfigurationInterface;
1114

15+
/**
16+
* @internal This ViewHelper is marked internal and only to be used within
17+
* the DeepL translate packages and therefore no public API.
18+
*/
1219
final class DeeplTranslateAllowedViewHelper extends AbstractConditionViewHelper
1320
{
14-
public function initializeArguments(): void
15-
{
16-
parent::initializeArguments();
17-
}
18-
1921
public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool
2022
{
23+
$deeplConfiguration = GeneralUtility::makeInstance(ConfigurationInterface::class);
24+
if ($deeplConfiguration->getApiKey() === '') {
25+
return false;
26+
}
27+
/** @var RenderingContext $renderingContext */
28+
$currentPageId = (int)($renderingContext->getRequest()?->getQueryParams()['id'] ?? 0);
29+
// set default to true avoiding breaking behaviour issues
30+
if (!$deeplConfiguration->isDeeplTranslationAllowedOnPage($currentPageId)) {
31+
return false;
32+
}
2133
if (self::getBackendUserAuthentication()->check('custom_options', AllowedTranslateAccess::ALLOWED_TRANSLATE_OPTION_VALUE)) {
2234
return true;
2335
}

Configuration/Backend/AjaxRoutes.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

Resources/Private/Language/de.locallang.xlf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
<target>Mit DeepL übersetzen</target>
1313
</trans-unit>
1414
<trans-unit id="localize.educate.deepltranslateHeader">
15-
<source>Translate with DeepL</source>
16-
<target>Mit DeepL übersetzen</target>
15+
<source>DeepL Translate</source>
16+
<target>DeepL Übersetzung</target>
1717
</trans-unit>
1818
<trans-unit id="localize.educate.deepltranslateHeaderAutodetect">
19-
<source>Translate with DeepL (autodetect)</source>
20-
<target>Mit DeepL übersetzen (autodetect)</target>
19+
<source><![CDATA[DeepL Translate<br>(autodetect)]]></source>
20+
<target><![CDATA[DeepL Übersetzung<br>(autodetect)]]></target>
2121
</trans-unit>
2222
<trans-unit id="localize.educate.deepltranslate" xml:space="preserve">
23-
<source><![CDATA[Translating content using DeepL Translate will create a translation from the source language to the language translation you selected. The translated content will be fetched from DeepL translation service (DeepL claims 99 percentage accuracy). The DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
24-
<target><![CDATA[Wenn Sie Inhalte mit DeepL Translate übersetzen, wird eine Übersetzung von der Ausgangssprache in die von Ihnen ausgewählte Sprache erstellt. Der übersetzte Inhalt wird vom DeepL Übersetzungsdienst abgerufen (DeepL behauptet eine 99-prozentige Genauigkeit). Der Dienst DeepL unterstützt die Übersetzung in und aus den Sprachen Englisch, Französisch, Deutsch, Spanisch, Italienisch, Niederländisch und Polnisch. Andere Sprachen werden ignoriert und die normale Übersetzungsfunktion von TYPO3 wird verwendet.]]></target>
23+
<source><![CDATA[Translating content via DeepL translate will create a translation from source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">different languages</a>. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
24+
<target><![CDATA[Die Übersetzung von Inhalten über DeepL translate erstellt eine Übersetzung von der Ausgangssprache in die Sprache, in die Sie übersetzen. Der übersetzte Inhalt wird vom DeepL Übersetzungsdienst stammen, der zu 99% genau ist. Der Dienst DeepL unterstützt die Übersetzung in und aus <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">verschiedenen Sprachen</a>. Andere Sprachen werden ignoriert und es wird die normale Übersetzungsfunktion von TYPO3 verwendet.]]></target>
2525
</trans-unit>
2626
<trans-unit id="localize.educate.deepltranslateAuto" xml:space="preserve">
2727
<source><![CDATA[Translating content using DeepL Translate (autodetect) will create a translation from the auto detected source language to the language translation you selected. The translated content will be fetched from DeepL translation service (DeepL claims 99 percentage accuracy). The DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>

Resources/Private/Language/fr.locallang.xlf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
<header/>
55
<body>
66
<trans-unit id="localize.educate.deepltranslateHeader">
7-
<source>Translate with DeepL</source>
8-
<target>Traduire avec DeepL</target>
7+
<source>DeepL translate</source>
8+
<target>DeepL traduire</target>
99
</trans-unit>
1010
<trans-unit id="localize.educate.deepltranslateHeaderAutodetect">
11-
<source>Translate (DeepL) (autodetect)</source>
12-
<target>Traduire (DeepL) (détection automatique)</target>
11+
<source><![CDATA[DeepL Translate<br>(autodetect)]]></source>
12+
<target><![CDATA[DeepL traduire<br>(auto détection)]]></target>
1313
</trans-unit>
1414

1515
<trans-unit id="localize.educate.deepltranslate" xml:space="preserve">
16-
<source><![CDATA[Translating content via DeepL translate will create a translation from source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
17-
<target><![CDATA[La traduction de contenu via DeepL translate créera une traduction de la langue source vers la langue vers laquelle vous traduisez. Le contenu traduit proviendra du service de traduction DeepL qui est précis à 99 %. Le service DeepL prend en charge la traduction vers et depuis l'anglais, le français, l'allemand, l'espagnol et l'italien. , néerlandais et polonais. Les autres langues seront ignorées et utiliseront par défaut l'opération de traduction normale de TYPO3.]]></target>
16+
<source><![CDATA[Translating content via DeepL translate will create a translation from source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">different languages</a>. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
17+
<target><![CDATA[La traduction d'un contenu via DeepL translate créera une traduction de la langue source vers la langue vers laquelle vous traduisez. Le contenu traduit proviendra du service de traduction DeepL qui est précis à 99%. Le service DeepL prend en charge la traduction vers et depuis <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">différentes langues</a>. Les autres langues seront ignorées et la traduction normale de TYPO3 sera appliquée par défaut.]]></target>
1818
</trans-unit>
1919
<trans-unit id="localize.educate.deepltranslateAuto" xml:space="preserve">
2020
<source><![CDATA[Translating content via DeepL translate will create a translation from <b>auto detected</b> source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>

Resources/Private/Language/locallang.xlf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
<source>Translate with DeepL</source>
1111
</trans-unit>
1212
<trans-unit id="localize.educate.deepltranslateHeader">
13-
<source>Translate with DeepL</source>
13+
<source>DeepL Translate</source>
1414
</trans-unit>
1515
<trans-unit id="localize.educate.deepltranslateHeaderAutodetect">
16-
<source>Translate with DeepL (autodetect)</source>
16+
<source><![CDATA[DeepL Translate<br>(autodetect)]]></source>
1717
</trans-unit>
1818

1919
<trans-unit id="localize.educate.deepltranslate" xml:space="preserve">
20-
<source>Translating content via DeepL translate will create a translation from source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.</source>
20+
<source><![CDATA[Translating content via DeepL translate will create a translation from source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">different languages</a>. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
2121
</trans-unit>
2222
<trans-unit id="localize.educate.deepltranslateAuto" xml:space="preserve">
23-
<source><![CDATA[Translating content via DeepL translate will create a translation from <b>auto detected</b> source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from English, French, German, Spanish, Italian, Dutch, and Polish languages. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
23+
<source><![CDATA[Translating content via DeepL translate will create a translation from <b>auto detected</b> source language to the language you translate to. The translated content will be from DeepL translation service which is 99% accurate. DeepL service supports translation to and from <a href="https://developers.deepl.com/docs/getting-started/supported-languages" target="_blank">different languages</a>. Other languages will be ignored and will default to the normal translate operation of TYPO3.]]></source>
2424
</trans-unit>
2525

2626
<trans-unit id="localize.educate.deeplSettingsFailure">

0 commit comments

Comments
 (0)