‪TYPO3CMS  ‪main
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
24 use ‪TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
27 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
30 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
31 
106 final class ‪TranslateViewHelper extends AbstractViewHelper
107 {
108  use CompileWithRenderStatic;
109 
115  protected ‪$escapeChildren = false;
116 
117  public function ‪initializeArguments(): void
118  {
119  $this->registerArgument('key', 'string', 'Translation Key');
120  $this->registerArgument('id', 'string', 'Translation ID. Same as key.');
121  $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');
122  $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
123  $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
124  $this->registerArgument('languageKey', 'string', 'Language key ("da" for example) or "default" to use. Also a Locale object is possible. If empty, use current locale from the request.');
125  }
126 
133  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
134  {
135  $key = $arguments['key'];
136  $id = $arguments['id'];
137  $default = (string)($arguments['default'] ?? $renderChildrenClosure() ?? '');
138  $extensionName = $arguments['extensionName'];
139  $translateArguments = $arguments['arguments'];
140 
141  // Use key if id is empty.
142  if ($id === null) {
143  $id = $key;
144  }
145 
146  $id = (string)$id;
147  if ($id === '') {
148  throw new Exception('An argument "key" or "id" has to be provided', 1351584844);
149  }
150 
151  $request = null;
152  if ($renderingContext instanceof RenderingContext) {
153  $request = $renderingContext->getRequest();
154  }
155 
156  if (empty($extensionName)) {
157  if ($request instanceof ExtbaseRequestInterface) {
158  $extensionName = $request->getControllerExtensionName();
159  } elseif (str_starts_with($id, 'LLL:EXT:')) {
160  $extensionName = substr($id, 8, strpos($id, '/', 8) - 8);
161  } elseif ($default) {
162  return ‪self::handleDefaultValue($default, $translateArguments);
163  } else {
164  // Throw exception in case neither an extension key nor a extbase request
165  // are given, since the "short key" shouldn't be considered as a label.
166  throw new \RuntimeException(
167  'ViewHelper f:translate in non-extbase context needs attribute "extensionName" to resolve'
168  . ' key="' . $id . '" without path. Either set attribute "extensionName" together with the short'
169  . ' key "yourKey" to result in a lookup "LLL:EXT:your_extension/Resources/Private/Language/locallang.xlf:yourKey",'
170  . ' or (better) use a full LLL reference like key="LLL:EXT:your_extension/Resources/Private/Language/yourFile.xlf:yourKey".'
171  . ' Alternatively, you can also define a default value.',
172  1639828178
173  );
174  }
175  }
176  try {
177  $locale = ‪self::getUsedLocale($arguments['languageKey'], $request);
178  $value = ‪LocalizationUtility::translate($id, $extensionName, $translateArguments, $locale);
179  } catch (\InvalidArgumentException) {
180  // @todo: Switch to more specific Exceptions here - for instance those thrown when a package was not found, see #95957
181  $value = null;
182  }
183  if ($value === null) {
184  return ‪self::handleDefaultValue($default, $translateArguments);
185  }
186  return $value;
187  }
188 
192  protected static function ‪handleDefaultValue(string $default, ?array $translateArguments): string
193  {
194  if (!empty($translateArguments)) {
195  return vsprintf($default, $translateArguments);
196  }
197  return $default;
198  }
199 
200  protected static function ‪getUsedLocale(‪Locale|string|null $languageKey, ?ServerRequestInterface $request): ‪Locale|string|null
201  {
202  if ($languageKey !== null && $languageKey !== '') {
203  return $languageKey;
204  }
205  if ($request) {
206  return GeneralUtility::makeInstance(Locales::class)->createLocaleFromRequest($request);
207  }
208  return null;
209  }
210 }
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateViewHelper.php:131
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateViewHelper.php:115
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:36
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: TranslateViewHelper.php:113
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\getUsedLocale
‪static getUsedLocale(Locale|string|null $languageKey, ?ServerRequestInterface $request)
Definition: TranslateViewHelper.php:198
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper
Definition: TranslateViewHelper.php:107
‪TYPO3\CMS\Fluid\ViewHelpers
‪TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper\handleDefaultValue
‪static handleDefaultValue(string $default, ?array $translateArguments)
Definition: TranslateViewHelper.php:190
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Core\Localization\Locale
Definition: Locale.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35