Skip to content

[TASK] Streamline Localization handling #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -30,4 +31,20 @@ public function getApiKey(): string
{
return $this->apiKey;
}

/**
* Checks translation allowed against Page TSconfig from settings
*
* @param int $pageId
* @return bool
* @throws \JsonException
*/
public function isDeeplTranslationAllowedOnPage(int $pageId): bool
{
$localizationConfiguration = BackendUtility::getPagesTSconfig($pageId)['mod.']['web_layout.']['localization.'] ?? [];
if ((bool)($localizationConfiguration['enableDeeplTranslate'] ?? true) === false) {
return false;
}
return true;
}
}
2 changes: 2 additions & 0 deletions Classes/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
interface ConfigurationInterface
{
public function getApiKey(): string;

public function isDeeplTranslationAllowedOnPage(int $pageId): bool;
}
39 changes: 0 additions & 39 deletions Classes/Controller/Backend/AjaxController.php

This file was deleted.

8 changes: 7 additions & 1 deletion Classes/Event/Listener/RenderLocalizationSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Controller\Event\RenderAdditionalContentToRecordListEvent;
use TYPO3\CMS\Core\Site\Entity\Site;
use WebVision\Deepltranslate\Core\ConfigurationInterface;
use WebVision\Deepltranslate\Core\Event\RenderLocalizationSelectAllowed;
use WebVision\Deepltranslate\Core\Form\TranslationDropdownGenerator;

final class RenderLocalizationSelect
{
public function __construct(
private readonly TranslationDropdownGenerator $generator,
private readonly EventDispatcherInterface $eventDispatcher
private readonly EventDispatcherInterface $eventDispatcher,
private readonly ConfigurationInterface $configuration
) {
}

public function __invoke(RenderAdditionalContentToRecordListEvent $event): void
{
$request = $event->getRequest();
$currentPageId = (int)($request->getQueryParams()['id'] ?? 0);
if (!$this->configuration->isDeeplTranslationAllowedOnPage($currentPageId)) {
return;
}
// Check, if some event listener doesn't allow rendering here.
// For use cases see Event
$renderingAllowedEvent = $this->eventDispatcher->dispatch(new RenderLocalizationSelectAllowed($request));
Expand Down
22 changes: 17 additions & 5 deletions Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
namespace WebVision\Deepltranslate\Core\ViewHelpers\Be\Access;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
use WebVision\Deepltranslate\Core\Access\AllowedTranslateAccess;
use WebVision\Deepltranslate\Core\ConfigurationInterface;

/**
* @internal This ViewHelper is marked internal and only to be used within
* the DeepL translate packages and therefore no public API.
*/
final class DeeplTranslateAllowedViewHelper extends AbstractConditionViewHelper
{
public function initializeArguments(): void
{
parent::initializeArguments();
}

public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool
{
$deeplConfiguration = GeneralUtility::makeInstance(ConfigurationInterface::class);
if ($deeplConfiguration->getApiKey() === '') {
return false;
}
/** @var RenderingContext $renderingContext */
$currentPageId = (int)($renderingContext->getRequest()?->getQueryParams()['id'] ?? 0);
// set default to true avoiding breaking behaviour issues
if (!$deeplConfiguration->isDeeplTranslationAllowedOnPage($currentPageId)) {
return false;
}
if (self::getBackendUserAuthentication()->check('custom_options', AllowedTranslateAccess::ALLOWED_TRANSLATE_OPTION_VALUE)) {
return true;
}
Expand Down
17 changes: 0 additions & 17 deletions Configuration/Backend/AjaxRoutes.php

This file was deleted.