[TASK] Improve translation handling
This commit is contained in:
parent
2dc7630390
commit
902816e1f2
@ -488,18 +488,8 @@ class ActionController extends BaseController
|
|||||||
if ($error->keyword() == 'additionalProperties') {
|
if ($error->keyword() == 'additionalProperties') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($error->keyword() != 'required') {
|
switch ($error->keyword()) {
|
||||||
$errorLabel = $this->getTranslation(
|
case 'required':
|
||||||
'error.' . $field . '.' . $error->keyword()
|
|
||||||
);
|
|
||||||
if ($errorLabel == null) {
|
|
||||||
$errorLabel = 'error.'
|
|
||||||
. $field
|
|
||||||
. '.'
|
|
||||||
. $error->keyword();
|
|
||||||
}
|
|
||||||
$this->errorLabels[$field] = $errorLabel;
|
|
||||||
} else {
|
|
||||||
$errorLabel = $this->getTranslation(
|
$errorLabel = $this->getTranslation(
|
||||||
'error.' . $field . '.required'
|
'error.' . $field . '.required'
|
||||||
);
|
);
|
||||||
@ -518,6 +508,59 @@ class ActionController extends BaseController
|
|||||||
. $error->keyword();
|
. $error->keyword();
|
||||||
}
|
}
|
||||||
$this->errorLabels[$field] = $errorLabel;
|
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
94
Classes/Utility/TranslationUtility.php
Normal file
94
Classes/Utility/TranslationUtility.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ namespace Cjel\TemplatesAide\ViewHelpers;
|
|||||||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
|
||||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
|
||||||
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
|
||||||
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
||||||
|
|
||||||
class TranslationViewHelper extends AbstractViewHelper
|
class TranslationViewHelper extends AbstractViewHelper
|
||||||
{
|
{
|
||||||
@ -33,6 +34,20 @@ class TranslationViewHelper extends AbstractViewHelper
|
|||||||
'The translation key to render',
|
'The translation key to render',
|
||||||
true
|
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,
|
\Closure $renderChildrenClosure,
|
||||||
RenderingContextInterface $renderingContext
|
RenderingContextInterface $renderingContext
|
||||||
) {
|
) {
|
||||||
return 'this.extension.translation';
|
$translation = LocalizationUtility::translate(
|
||||||
|
$arguments['key'],
|
||||||
|
$arguments['extensionKey'],
|
||||||
|
$arguments['arguments']
|
||||||
|
);
|
||||||
|
if ($translation) {
|
||||||
|
return $translation;
|
||||||
|
}
|
||||||
|
return $arguments['extensionKey'] . ': ' . $arguments['key'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user