Skip to content

Commit

Permalink
Merge pull request #143 from web-vision/bugfix/hide-translation-butto…
Browse files Browse the repository at this point in the history
…ns-30

[BUGFIX] Hide DeepL controls for not supported languages
  • Loading branch information
NarkNiro authored Feb 23, 2023
2 parents 89ce1fd + 5f0de63 commit a133e23
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Classes/Hooks/AllowLanguageSynchronizationHook.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace WebVision\WvDeepltranslate\Hooks;

Expand Down
1 change: 1 addition & 0 deletions Classes/Override/DatabaseRecordList.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function makeLocalizationPanel($table, $row, array $translations): string
&& !$this->isRecordDeletePlaceholder($row)
&& !isset($translations[$lUid_OnPage])
&& $this->getBackendUserAuthentication()->checkLanguageAccess($lUid_OnPage)
&& DeeplBackendUtility::checkCanBeTranslated($pageId, $lUid_OnPage)
) {
$out .= DeeplBackendUtility::buildTranslateButton(
$table,
Expand Down
19 changes: 5 additions & 14 deletions Classes/Override/DeeplRecordListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,20 @@ protected function languageSelector($requestUri): string
$this->id,
$requestUri
);

if ($options == '') {
return '';
return $originalOutput;
}

$deeplSelectLabel = LocalizationUtility::translate(
'backend.label',
'wv_deepltranslate'
);

return str_replace(
'<div class="col-auto">',
'<div class="col-auto row"><div class="col-sm-6">',
$originalOutput
)
. '<div class="col-sm-6">'
. '<div class="row">'
. '<label class="col-sm-4" style="font-weight: bold;">' . $deeplSelectLabel . ':</label>'
. '<div class="col-sm-8">'
. '<select class="form-select" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">'
. $options
. '</select>'
. '</div>'
. '</div>'
. '<select class="form-select" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">'
. $options
. '</select>'
. '</div>'
. '</div>';
}
Expand Down
4 changes: 4 additions & 0 deletions Classes/Override/v10/DatabaseRecordList.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function makeLocalizationPanel($table, $row): array
&& $this->isEditable($table)
&& !isset($translations['translations'][$lUid_OnPage])
&& $this->getBackendUserAuthentication()->checkLanguageAccess($lUid_OnPage)
&& DeeplBackendUtility::checkCanBeTranslated(
($table === 'pages') ? $row['uid'] : $row['pid'],
$lUid_OnPage
)
) {
$language = BackendUtility::getRecord('sys_language', $lUid_OnPage, 'title');
$lNew .= DeeplBackendUtility::buildTranslateButton(
Expand Down
10 changes: 2 additions & 8 deletions Classes/Override/v10/DeeplPageLayoutView.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ public function languageSelector($id)
$this->id,
GeneralUtility::getIndpEnv('REQUEST_URI')
);

if ($options == '') {
return '';
return $originalOutput;
}

$originalOutput = str_ireplace('</div></div>', '</div>', $originalOutput);
return $originalOutput
. '<div class="form-group">'
. sprintf(
'<label>%s</label>',
LocalizationUtility::translate(
'backend.label',
'wv_deepltranslate'
)
)
. '<select class="form-control input-sm" onchange="window.location.href=this.options[this.selectedIndex].value">'
. $options
. '</select></div></div>';
Expand Down
9 changes: 1 addition & 8 deletions Classes/Override/v10/DeeplRecordListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ protected function languageSelector($id): string
);

if ($options == '') {
return '';
return $originalOutput;
}

return str_ireplace('</div></div>', '</div>', $originalOutput)
. '<div class="form-group">'
. sprintf(
'<label>%s</label>',
LocalizationUtility::translate(
'backend.label',
'wv_deepltranslate'
)
)
. '<select class="form-control input-sm" name="createNewLanguage" onchange="window.location.href=this.options[this.selectedIndex].value">'
. $options
. '</select>'
Expand Down
42 changes: 37 additions & 5 deletions Classes/Utility/DeeplBackendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use WebVision\WvDeepltranslate\Exception\LanguageIsoCodeNotFoundException;
use WebVision\WvDeepltranslate\Exception\LanguageRecordNotFoundException;
use WebVision\WvDeepltranslate\Service\LanguageService;

class DeeplBackendUtility
{
Expand Down Expand Up @@ -187,7 +190,7 @@ private static function getIcon(string $iconFlag): Icon
$deeplIcon = GeneralUtility::makeInstance(
IconFactory::class
)->getIcon(
'actions-localize-deepl',
'deepl-grey-logo',
Icon::SIZE_OVERLAY
);
GeneralUtility::makeInstance(IconRegistry::class)
Expand Down Expand Up @@ -248,11 +251,13 @@ public static function buildTranslateDropdown(
}
// If any languages are left, make selector:
if (!empty($availableTranslations)) {
$output = sprintf(
'<option value="">%s</option>',
htmlspecialchars(LocalizationUtility::translate('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:new_language'))
);
$output = '';
foreach ($availableTranslations as $languageUid => $languageTitle) {
// check if language can be translated with DeepL
// otherwise continue to next
if (!DeeplBackendUtility::checkCanBeTranslated($id, $languageUid)) {
continue;
}
// Build localize command URL to DataHandler (tce_db)
// which redirects to FormEngine (record_edit)
// which, when finished editing should return back to the current page (returnUrl)
Expand All @@ -268,12 +273,39 @@ public static function buildTranslateDropdown(
$targetUrl = self::buildBackendRoute('tce_db', $params);
$output .= '<option value="' . htmlspecialchars($targetUrl) . '">' . htmlspecialchars($languageTitle) . '</option>';
}
if ($output !== '') {
$output = sprintf(
'<option value="">%s</option>%s',
htmlspecialchars(LocalizationUtility::translate('backend.label', 'wv_deepltranslate')),
$output
);
}

return $output;
}
return '';
}

public static function checkCanBeTranslated(int $pageId, int $languageId): bool
{
$languageService = GeneralUtility::makeInstance(LanguageService::class);
$site = $languageService->getCurrentSite('pages', $pageId);
if ($site === null) {
return false;
}
try {
$languageService->getSourceLanguage($site['site']);
} catch (LanguageIsoCodeNotFoundException $e) {
return false;
}
try {
$languageService->getTargetLanguage($site['site'], $languageId);
} catch (LanguageIsoCodeNotFoundException|LanguageRecordNotFoundException $e) {
return false;
}
return true;
}

private static function getBackendUserAuthentication(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
Expand Down
11 changes: 10 additions & 1 deletion Classes/ViewHelpers/DeeplTranslateViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WebVision\WvDeepltranslate\ViewHelpers;

use TYPO3\CMS\Backend\View\PageLayoutContext;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use WebVision\WvDeepltranslate\Utility\DeeplBackendUtility;

Expand Down Expand Up @@ -33,13 +34,21 @@ public function render(): array
$siteLanguage->getLanguageId() != -1
&& $siteLanguage->getLanguageId() != 0
) {
if (!DeeplBackendUtility::checkCanBeTranslated($context->getPageId(), $siteLanguage->getLanguageId())) {
continue;
}
$languageMatch[$siteLanguage->getTitle()] = $siteLanguage->getLanguageId();
}
}

if (count($languageMatch) === 0) {
return $options;
}
foreach ($context->getNewLanguageOptions() as $key => $possibleLanguage) {
if ($key === 0) {
$options[] = $possibleLanguage;
continue;
}
if (!array_key_exists($possibleLanguage, $languageMatch)) {
continue;
}
$parameters = [
Expand Down
29 changes: 17 additions & 12 deletions Resources/Private/Backend/Partials/PageLayout/LanguageColumns.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
xmlns:deepl="http://typo3.org/ns/WebVision/WvDeepltranslate/ViewHelpers"
xmlns:f="http://typo3.org/ns/TYPO3Fluid/Fluid/ViewHelpers"
>
<f:comment>
<f:comment><!--
Use in TYPO3 v11 default
</f:comment>
--></f:comment>

<f:if condition="{context.newLanguageOptions}">
<div class="row row-cols-auto align-items-end g-3 mb-3">
Expand All @@ -16,16 +16,21 @@
</f:for>
</select>
</div>
<div class="col row">
<label class="col-sm-4 col">{f:translate(key: 'backend.label', extensionName: 'wv_deepltranslate')}</label>
<div class="col col-sm-8">
<select class="form-select" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">
<f:for each="{deepl:deeplTranslate(context: '{context}')}" as="languageName" key="url">
<option value="{url}">{languageName}</option>
</f:for>
</select>
</div>
</div>
<f:alias map="{deeplLanguages: '{deepl:deeplTranslate(context: \'{context}\')}'}">
<f:if condition="{deeplLanguages -> f:count()} > 0">
<div class="col row">
<label class="col-sm-4 col">{f:translate(key: 'backend.label', extensionName: 'wv_deepltranslate')}</label>
<div class="col col-sm-8">
<select class="form-select" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">
<option><f:translate key="backend.label" extensionName="wv_deepltranslate" /></option>
<f:for each="{deeplLanguages}" as="languageName" key="url">
<option value="{url}">{languageName}</option>
</f:for>
</select>
</div>
</div>
</f:if>
</f:alias>
</div>
</f:if>
<f:comment><!-- Identical to backend/Resources/Partials/PageLayout/LanguageColumns only copied --></f:comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
xmlns:deepl="http://typo3.org/ns/WebVision/WvDeepltranslate/ViewHelpers"
xmlns:f="http://typo3.org/ns/TYPO3Fluid/Fluid/ViewHelpers"
>
<f:comment>
<f:comment><!--
Use in TYPO3 v10 when feature toggle enabled
</f:comment>
--></f:comment>
<f:if condition="{context.newLanguageOptions}">
<div class="form-inline form-inline-spaced">
<div class="form-group">
Expand All @@ -15,14 +15,19 @@
</f:for>
</select>
</div>
<div class="form-group">
<label >{f:translate(key: 'backend.label', extensionName: 'wv_deepltranslate')}</label>
<select class="form-control input-sm" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">
<f:for each="{deepl:deeplTranslate(context: '{context}')}" as="languageName" key="url">
<option value="{url}">{languageName}</option>
</f:for>
</select>
</div>
<f:alias map="{deeplLanguages: '{deepl:deeplTranslate(context: \'{context}\')}'}">
<f:if condition="{deeplLanguages -> f:count()} > 0">
<div class="form-group">
<label >{f:translate(key: 'backend.label', extensionName: 'wv_deepltranslate')}</label>
<select class="form-control input-sm" name="createNewLanguage" data-global-event="change" data-action-navigate="$value">
<option><f:translate key="backend.label" extensionName="wv_deepltranslate" /></option>
<f:for each="{deeplLanguages}" as="languageName" key="url">
<option value="{url}">{languageName}</option>
</f:for>
</select>
</div>
</f:if>
</f:alias>
</div>
</f:if>
<f:comment><!-- Identical to backend/Resources/Partials/PageLayout/LanguageColumns only copied --></f:comment>
Expand Down
4 changes: 2 additions & 2 deletions ext_typoscript_setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ module.tx_backend.view {
}
}

[compatVersion("10.4")]
module.tx_backend.view.partialRootPaths.10 = EXT:wv_deepltranslate/Resources/Private/Backend/Partials/v10
[typo3.version < "11.5"]
module.tx_backend.view.partialRootPaths.10 = EXT:wv_deepltranslate/Resources/Private/Backend/v10/Partials
[END]

0 comments on commit a133e23

Please sign in to comment.