TYPO3 CMS  TYPO3_8-7
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  */
22 
75 {
77 
83  protected $escapeChildren = false;
84 
90  public function initializeArguments()
91  {
92  parent::initializeArguments();
93  $this->registerArgument('key', 'string', 'Translation Key');
94  $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
95  $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');
96  $this->registerArgument('htmlEscape', 'bool', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
97  $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
98  $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
99  }
100 
110  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
111  {
112  $key = $arguments['key'];
113  $id = $arguments['id'];
114  $default = $arguments['default'];
115  $htmlEscape = $arguments['htmlEscape'];
116  $extensionName = $arguments['extensionName'];
117  $arguments = $arguments['arguments'];
118 
119  if ($htmlEscape !== null) {
121  'htmlEscape',
122  $renderingContext,
123  'Please wrap the view helper in <f:format.raw> if you want to disable HTML escaping, which is enabled by default now.'
124  );
125  }
126 
127  // Wrapper including a compatibility layer for TYPO3 Flow Translation
128  if ($id === null) {
129  $id = $key;
130  }
131 
132  if ((string)$id === '') {
133  throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
134  }
135 
136  $request = $renderingContext->getControllerContext()->getRequest();
137  $extensionName = $extensionName === null ? $request->getControllerExtensionName() : $extensionName;
138  try {
139  $value = static::translate($id, $extensionName, $arguments);
140  } catch (\InvalidArgumentException $e) {
141  $value = null;
142  }
143  if ($value === null) {
144  $value = $default !== null ? $default : $renderChildrenClosure();
145  if (!empty($arguments)) {
146  $value = vsprintf($value, $arguments);
147  }
148  }
149  return $value;
150  }
151 
161  protected static function translate($id, $extensionName, $arguments)
162  {
163  return LocalizationUtility::translate($id, $extensionName, $arguments);
164  }
165 }
static logDeprecatedViewHelperAttribute(string $property, RenderingContextInterface $renderingContext, string $additionalMessage='')
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static translate($key, $extensionName=null, $arguments=null)
static translate($id, $extensionName, $arguments)