From 902816e1f283bd51224a6a357cc8e068d8a2d942 Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Sat, 25 Jun 2022 00:21:17 +0200 Subject: [PATCH] [TASK] Improve translation handling --- Classes/Controller/ActionController.php | 67 ++++++++++--- Classes/Utility/TranslationUtility.php | 94 +++++++++++++++++++ Classes/ViewHelpers/TranslationViewHelper.php | 25 ++++- 3 files changed, 173 insertions(+), 13 deletions(-) create mode 100644 Classes/Utility/TranslationUtility.php diff --git a/Classes/Controller/ActionController.php b/Classes/Controller/ActionController.php index ff4b7c1..05884f3 100644 --- a/Classes/Controller/ActionController.php +++ b/Classes/Controller/ActionController.php @@ -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; } } } diff --git a/Classes/Utility/TranslationUtility.php b/Classes/Utility/TranslationUtility.php new file mode 100644 index 0000000..29d9a1c --- /dev/null +++ b/Classes/Utility/TranslationUtility.php @@ -0,0 +1,94 @@ + + * + ***/ + +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; + } + } +} diff --git a/Classes/ViewHelpers/TranslationViewHelper.php b/Classes/ViewHelpers/TranslationViewHelper.php index 6ec2b9b..03aa02c 100644 --- a/Classes/ViewHelpers/TranslationViewHelper.php +++ b/Classes/ViewHelpers/TranslationViewHelper.php @@ -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']; } }