[TASK] Improve translation handling

This commit is contained in:
Philipp Dieter 2022-06-25 00:21:17 +02:00
parent 2dc7630390
commit 902816e1f2
3 changed files with 173 additions and 13 deletions

View File

@ -488,18 +488,8 @@ class ActionController extends BaseController
if ($error->keyword() == 'additionalProperties') {
continue;
}
if ($error->keyword() != 'required') {
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
} else {
switch ($error->keyword()) {
case 'required':
$errorLabel = $this->getTranslation(
'error.' . $field . '.required'
);
@ -518,6 +508,59 @@ class ActionController extends BaseController
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'pattern':
$errorLabel = $this->getTranslation(
'error.' . $field . '.pattern'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.pattern', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'format':
$errorLabel = $this->getTranslation(
'error.' . $field . '.format'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.format', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
default:
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
}
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace Cjel\TemplatesAide\Utility;
/***
*
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 Philipp Dieter <philipp.dieter@attic-media.net>
*
***/
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
*
*/
class TranslationUtility
{
/**
* Get all interface constants per prefix
*/
public static function buildSelectOptionsFromOptions(
$options,
$column,
$element,
$extensionKey = null
) {
$items = [];
if ($addEmpty) {
$items[] = ['-', ''];
}
foreach ($options as $option) {
$translationKey = "option.$element.$column.$option";
$translation = self::getTranslation(
$translationKey,
$extensionKey
);
if ($translation) {
$items[] = [
'code' => $option,
'label' => $translation,
];
} else {
$items[] = [
'code' => $option,
'label' => $translationKey,
];
}
}
return $items;
}
/**
* shortcut to get translation
*
* @return void
*/
public static function getTranslation($key, $extensionKey)
{
if (version_compare(TYPO3_branch, '10.0', '>=')) {
if (!$extensionKey) {
$extensionKey = 'site_templates';
}
return implode([
'LLL:EXT:',
$extensionKey,
'/Resources/Private/Language/locallang_db.xlf:',
$key
]);
} else {
if ($extensionKey) {
$translation = LocalizationUtility::translate(
$key,
$extensionKey
);
if ($translation) {
return $translation;
}
}
$translation = LocalizationUtility::translate(
$key,
'site_templates'
);
if ($translation) {
return $translation;
}
return null;
}
}
}

View File

@ -15,6 +15,7 @@ namespace Cjel\TemplatesAide\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
class TranslationViewHelper extends AbstractViewHelper
{
@ -33,6 +34,20 @@ class TranslationViewHelper extends AbstractViewHelper
'The translation key to render',
true
);
$this->registerArgument(
'extensionKey',
'string',
'The extension key to search in',
false,
'site_templates'
);
$this->registerArgument(
'arguments',
'array',
'The arguments',
false,
false
);
}
/**
@ -48,6 +63,14 @@ class TranslationViewHelper extends AbstractViewHelper
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
return 'this.extension.translation';
$translation = LocalizationUtility::translate(
$arguments['key'],
$arguments['extensionKey'],
$arguments['arguments']
);
if ($translation) {
return $translation;
}
return $arguments['extensionKey'] . ': ' . $arguments['key'];
}
}