Skip to content

Commit 440f9ae

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 440f9ae

File tree

6 files changed

+43
-62
lines changed

6 files changed

+43
-62
lines changed

Classes/Configuration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WebVision\Deepltranslate\Core;
66

77
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
8+
use TYPO3\CMS\Backend\Utility\BackendUtility;
89
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
910
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
1011
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
@@ -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.

0 commit comments

Comments
 (0)