diff --git a/Classes/Configuration.php b/Classes/Configuration.php index 087c5423..5f8fed3f 100644 --- a/Classes/Configuration.php +++ b/Classes/Configuration.php @@ -5,10 +5,12 @@ namespace WebVision\Deepltranslate\Core; use Symfony\Component\DependencyInjection\Attribute\AsAlias; +use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\MathUtility; #[AsAlias(id: ConfigurationInterface::class, public: true)] final class Configuration implements ConfigurationInterface @@ -30,4 +32,44 @@ public function getApiKey(): string { return $this->apiKey; } + + public function isDeeplTranslateAllowed(int $pageId): bool + { + $pageTsConfig = BackendUtility::getPagesTSconfig($pageId); + if (!is_array($pageTsConfig['mod.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.']['localization.'] ?? null) + || !( + is_bool($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + || is_int($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + || ( + is_string($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + && MathUtility::canBeInterpretedAsInteger($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate']) + ) + ) + ) { + return true; + } + return (bool)$pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate']; + } + + public function isCoreTranslationDisabled(int $pageId): bool + { + $pageTsConfig = BackendUtility::getPagesTSconfig($pageId); + if (!is_array($pageTsConfig['mod.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.']['localization.'] ?? null) + || !( + is_bool($pageTsConfig['mod.']['web_layout.']['localization.']['disableCoreTranslation'] ?? null) + || is_int($pageTsConfig['mod.']['web_layout.']['localization.']['disableCoreTranslation'] ?? null) + || ( + is_string($pageTsConfig['mod.']['web_layout.']['localization.']['disableCoreTranslation'] ?? null) + && MathUtility::canBeInterpretedAsInteger($pageTsConfig['mod.']['web_layout.']['localization.']['disableCoreTranslation']) + ) + ) + ) { + return false; + } + return (bool)$pageTsConfig['mod.']['web_layout.']['localization.']['disableCoreTranslation']; + } } diff --git a/Classes/ConfigurationInterface.php b/Classes/ConfigurationInterface.php index f08874de..8d3d03f1 100644 --- a/Classes/ConfigurationInterface.php +++ b/Classes/ConfigurationInterface.php @@ -13,4 +13,7 @@ interface ConfigurationInterface { public function getApiKey(): string; + public function isDeeplTranslateAllowed(int $pageId): bool; + + public function isCoreTranslationDisabled(int $pageId): bool; } diff --git a/Classes/Event/Listener/ApplyLocalizationModesEventListener.php b/Classes/Event/Listener/ApplyLocalizationModesEventListener.php index b7cded6f..276f118d 100644 --- a/Classes/Event/Listener/ApplyLocalizationModesEventListener.php +++ b/Classes/Event/Listener/ApplyLocalizationModesEventListener.php @@ -5,6 +5,7 @@ namespace WebVision\Deepltranslate\Core\Event\Listener; use TYPO3\CMS\Core\Information\Typo3Version; +use TYPO3\CMS\Core\Utility\MathUtility; use WebVision\Deepl\Base\Controller\Backend\LocalizationController; use WebVision\Deepl\Base\Event\GetLocalizationModesEvent; use WebVision\Deepl\Base\Localization\LocalizationMode; @@ -27,7 +28,7 @@ public function __invoke(GetLocalizationModesEvent $event): void description: $event->getLanguageService()->sL('LLL:EXT:deepltranslate_core/Resources/Private/Language/locallang.xlf:localize.educate.deepltranslate'), icon: ($majorVersion === 13 ? 'actions-localize-deepl-13' : 'actions-localize-deepl'), before: [], - after: ['translate', 'copy'], + after: [LocalizationController::ACTION_LOCALIZE, LocalizationController::ACTION_COPY], ); } if ($this->allowDeeplTranslateAuto($event)) { @@ -38,7 +39,7 @@ public function __invoke(GetLocalizationModesEvent $event): void description: $event->getLanguageService()->sL('LLL:EXT:deepltranslate_core/Resources/Private/Language/locallang.xlf:localize.educate.deepltranslateAuto'), icon: ($majorVersion === 13 ? 'actions-localize-deepl-13' : 'actions-localize-deepl'), before: [], - after: ['translate', 'copy', 'deepltranslate'], + after: [LocalizationController::ACTION_LOCALIZE, LocalizationController::ACTION_COPY, 'deepltranslate'], ); } if ($modes !== []) { @@ -48,8 +49,22 @@ public function __invoke(GetLocalizationModesEvent $event): void private function allowDeeplTranslate(GetLocalizationModesEvent $event): bool { - // @todo Prepared for PageTSConfig feature to toggle `deepltranslate`. - return true; + $pageTsConfig = $event->getPageTsConfig(); + if (!is_array($pageTsConfig['mod.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.'] ?? null) + || !is_array($pageTsConfig['mod.']['web_layout.']['localization.'] ?? null) + || !( + is_bool($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + || is_int($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + || ( + is_string($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate'] ?? null) + && MathUtility::canBeInterpretedAsInteger($pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate']) + ) + ) + ) { + return true; + } + return (bool)$pageTsConfig['mod.']['web_layout.']['localization.']['enableDeeplTranslate']; } private function allowDeeplTranslateAuto(GetLocalizationModesEvent $event): bool diff --git a/Classes/Event/Listener/ModifyRecordListLanguageDropdownEventListener.php b/Classes/Event/Listener/ModifyRecordListLanguageDropdownEventListener.php new file mode 100644 index 00000000..505d8114 --- /dev/null +++ b/Classes/Event/Listener/ModifyRecordListLanguageDropdownEventListener.php @@ -0,0 +1,80 @@ +getIdentifier() !== 'core-template-recordlist') { + return; + } + + $coreLanguageSelectorHtml = $event->getGlobalVariableProvider()->get('languageSelectorHtml'); + // means, no translations available, we can abort here. + if ($coreLanguageSelectorHtml === '') { + return; + } + $currentPageId = $event->getGlobalVariableProvider()->get('pageId'); + if (!$this->configuration->isDeeplTranslateAllowed($currentPageId)) { + return; + } + if ($this->configuration->isCoreTranslationDisabled($currentPageId)) { + $coreLanguageSelectorHtml = ''; + } else { + $coreLanguageSelectorHtml = str_replace('