‪TYPO3CMS  10.4
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
98 class ‪TranslateViewHelper extends AbstractViewHelper
99 {
100  use CompileWithRenderStatic;
101 
107  protected ‪$escapeChildren = false;
108 
114  public function ‪initializeArguments()
115  {
116  $this->registerArgument('key', 'string', 'Translation Key');
117  $this->registerArgument('id', 'string', 'Translation ID. Same as key.');
118  $this->registerArgument('default', 'string', 'If the given locallang key could not be found, this value is used. If this argument is not set, child nodes will be used to render the default');
119  $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
120  $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
121  $this->registerArgument('languageKey', 'string', 'Language key ("dk" for example) or "default" to use for this translation. If this argument is empty, we use the current language');
122  $this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist');
123  }
124 
134  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
135  {
136  $key = $arguments['key'];
137  $id = $arguments['id'];
138  $default = $arguments['default'];
139  $extensionName = $arguments['extensionName'];
140  $translateArguments = $arguments['arguments'];
141 
142  // Use key if id is empty.
143  if ($id === null) {
144  $id = $key;
145  }
146 
147  if ((string)$id === '') {
148  throw new Exception('An argument "key" or "id" has to be provided', 1351584844);
149  }
150 
151  $request = $renderingContext->getControllerContext()->getRequest();
152  $extensionName = $extensionName ?? $request->getControllerExtensionName();
153  try {
154  $value = static::translate($id, $extensionName, $translateArguments, $arguments['languageKey'], $arguments['alternativeLanguageKeys']);
155  } catch (\InvalidArgumentException $e) {
156  $value = null;
157  }
158  if ($value === null) {
159  $value = $default ?? $renderChildrenClosure();
160  if (!empty($translateArguments)) {
161  $value = vsprintf($value, $translateArguments);
162  }
163  }
164  return $value;
165  }
166 
178  protected static function ‪translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
179  {
180  return ‪LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
181  }
182 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateViewHelper.php:132
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\translate
‪static string null translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
Definition: TranslateViewHelper.php:176
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateViewHelper.php:112
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: TranslateViewHelper.php:105
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper
Definition: TranslateViewHelper.php:99
‪TYPO3\CMS\Fluid\ViewHelpers