Skip to content
Draft
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
42 changes: 42 additions & 0 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'];
}
}
3 changes: 3 additions & 0 deletions Classes/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
interface ConfigurationInterface
{
public function getApiKey(): string;
public function isDeeplTranslateAllowed(int $pageId): bool;

public function isCoreTranslationDisabled(int $pageId): bool;
}
23 changes: 19 additions & 4 deletions Classes/Event/Listener/ApplyLocalizationModesEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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 !== []) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Event\Listener;

use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WebVision\Deepl\Base\Event\ViewHelpers\ModifyInjectVariablesViewHelperEvent;
use WebVision\Deepltranslate\Core\ConfigurationInterface;
use WebVision\Deepltranslate\Core\Form\TranslationDropdownGenerator;

/**
* This listener helps to modify the Recordlist language dropdown
* and adds the possibility removing the default dropdown by page TS setting
*/
final class ModifyRecordListLanguageDropdownEventListener
{
public function __construct(
private readonly SiteFinder $siteFinder,
private readonly TranslationDropdownGenerator $generator,
private readonly ConfigurationInterface $configuration,
) {
}

public function __invoke(ModifyInjectVariablesViewHelperEvent $event): void
{
if ($event->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('<div class="form-row">', '', $coreLanguageSelectorHtml);
$coreLanguageSelectorHtml = substr($coreLanguageSelectorHtml, 0, (strlen($coreLanguageSelectorHtml) - strlen('</div>')));
}

$request = $GLOBALS['TYPO3_REQUEST'] ?? null;

$deeplLanguageSelectorHtml = '';
$site = $this->siteFinder->getSiteByPageId($currentPageId);
$siteLanguages = $site->getLanguages();
$options = $this->generator->buildTranslateDropdownOptions($siteLanguages, $currentPageId, $request->getUri());
if ($options !== '') {
$deeplLanguageSelectorHtml = '<div class="form-group">'
. '<div class="row">'
. '<label class="col-md-4 control-label" for="deeplLanguageTranslation">'
. $this->getLanguageService()->sL('LLL:EXT:deepltranslate_core/Resources/')
. '</label>'
. '<select class="form-select" id="deeplLanguageTranslation" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">'
. $options
. '</select>'
. '</div>'
. '</div>';
}

$event->getGlobalVariableProvider()->add(
'languageSelectorHtml',
sprintf('<div class="form-row">%s %s</div>', $coreLanguageSelectorHtml, $deeplLanguageSelectorHtml)
);
}

private function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'] ?? GeneralUtility::makeInstance(LanguageServiceFactory::class)
->createFromUserPreferences($GLOBALS['USER'] ?? null);
}
}
45 changes: 0 additions & 45 deletions Classes/Event/Listener/RenderLocalizationSelect.php

This file was deleted.

2 changes: 2 additions & 0 deletions Classes/Event/RenderLocalizationSelectAllowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/**
* Event deciding if the localization dropdown should be rendered.
* Could be used avoiding rendering for special cases, e.g., glossary or access denied.
*
* @deprecated Use page TS config options instead
*/
final class RenderLocalizationSelectAllowed
{
Expand Down
12 changes: 6 additions & 6 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ services:
WebVision\Deepltranslate\Core\ClientInterface:
class: WebVision\Deepltranslate\Core\Client

WebVision\Deepltranslate\Core\Event\Listener\RenderLocalizationSelect:
tags:
- name: 'event.listener'
identifier: 'deepltranslate/coreSelector'
event: TYPO3\CMS\Backend\Controller\Event\RenderAdditionalContentToRecordListEvent

WebVision\Deepltranslate\Core\Event\Listener\RenderTranslatedFlagInFrontendPreviewMode:
tags:
- name: 'event.listener'
Expand Down Expand Up @@ -74,3 +68,9 @@ services:
identifier: 'deepltranslate-core/translation-dropdown'
event: WebVision\Deepl\Base\Event\ViewHelpers\ModifyInjectVariablesViewHelperEvent
after: 'deepl-base/default-translation'

WebVision\Deepltranslate\Core\Event\Listener\ModifyRecordListLanguageDropdownEventListener:
tags:
- name: 'event.listener'
identifier: 'deepltranslate-core/translation-dropdown-recordlist'
event: WebVision\Deepl\Base\Event\ViewHelpers\ModifyInjectVariablesViewHelperEvent