TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
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  */
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 
74 {
80  protected $escapeChildren = false;
81 
87  public function initializeArguments()
88  {
89  parent::initializeArguments();
90  $this->registerArgument('key', 'string', 'Translation Key');
91  $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
92  $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');
93  $this->registerArgument('htmlEscape', 'bool', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
94  $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
95  $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
96  }
97 
103  public function render()
104  {
105  return static::renderStatic(
106  $this->arguments,
107  $this->buildRenderChildrenClosure(),
108  $this->renderingContext
109  );
110  }
111 
121  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
122  {
123  $key = $arguments['key'];
124  $id = $arguments['id'];
125  $default = $arguments['default'];
126  $htmlEscape = $arguments['htmlEscape'];
127  $extensionName = $arguments['extensionName'];
128  $arguments = $arguments['arguments'];
129 
130  if ($htmlEscape !== null) {
131  GeneralUtility::deprecationLog('htmlEscape argument has been deprecated and has no functionality any more. Please wrap the view helper in <f:format.raw> if you want to disable HTML escaping, which is enabled by default now.');
132  }
133 
134  // Wrapper including a compatibility layer for TYPO3 Flow Translation
135  if ($id === null) {
136  $id = $key;
137  }
138 
139  if ((string)$id === '') {
140  throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
141  }
142 
143  $request = $renderingContext->getControllerContext()->getRequest();
144  $extensionName = $extensionName === null ? $request->getControllerExtensionName() : $extensionName;
145  $value = static::translate($id, $extensionName, $arguments);
146  if ($value === null) {
147  $value = $default !== null ? $default : $renderChildrenClosure();
148  if (!empty($arguments)) {
149  $value = vsprintf($value, $arguments);
150  }
151  }
152  return $value;
153  }
154 
164  protected static function translate($id, $extensionName, $arguments)
165  {
166  return LocalizationUtility::translate($id, $extensionName, $arguments);
167  }
168 }
static renderStatic(array $arguments,\Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static translate($id, $extensionName, $arguments)
static translate($key, $extensionName, $arguments=null)