‪TYPO3CMS  9.5
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 use TYPO3Fluid\Fluid\Core\Exception;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22 
91 class ‪TranslateViewHelper extends AbstractViewHelper
92 {
93  use CompileWithRenderStatic;
94 
100  protected ‪$escapeChildren = false;
101 
107  public function ‪initializeArguments()
108  {
109  $this->registerArgument('key', 'string', 'Translation Key');
110  $this->registerArgument('id', 'string', 'Translation ID. Same as key.');
111  $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');
112  $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
113  $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
114  $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');
115  $this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist');
116  }
117 
127  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
128  {
129  $key = $arguments['key'];
130  $id = $arguments['id'];
131  $default = $arguments['default'];
132  $extensionName = $arguments['extensionName'];
133  $translateArguments = $arguments['arguments'];
134 
135  // Use key if id is empty.
136  if ($id === null) {
137  $id = $key;
138  }
139 
140  if ((string)$id === '') {
141  throw new Exception('An argument "key" or "id" has to be provided', 1351584844);
142  }
143 
144  $request = $renderingContext->getControllerContext()->getRequest();
145  $extensionName = $extensionName ?? $request->getControllerExtensionName();
146  try {
147  $value = static::translate($id, $extensionName, $translateArguments, $arguments['languageKey'], $arguments['alternativeLanguageKeys']);
148  } catch (\InvalidArgumentException $e) {
149  $value = null;
150  }
151  if ($value === null) {
152  $value = $default ?? $renderChildrenClosure();
153  if (!empty($translateArguments)) {
154  $value = vsprintf($value, $translateArguments);
155  }
156  }
157  return $value;
158  }
159 
171  protected static function ‪translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
172  {
173  return ‪LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
174  }
175 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:29
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateViewHelper.php:125
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\translate
‪static string null translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
Definition: TranslateViewHelper.php:169
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateViewHelper.php:105
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: TranslateViewHelper.php:98
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper
Definition: TranslateViewHelper.php:92
‪TYPO3\CMS\Fluid\ViewHelpers
Definition: BaseViewHelper.php:2