TYPO3  7.6
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
16 
20 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
22 
75 {
87  public function render($key = null, $id = null, $default = null, $htmlEscape = null, array $arguments = null, $extensionName = null)
88  {
89  return static::renderStatic(
90  array(
91  'key' => $key,
92  'id' => $id,
93  'default' => $default,
94  'htmlEscape' => $htmlEscape,
95  'arguments' => $arguments,
96  'extensionName' => $extensionName,
97  ),
99  $this->renderingContext
100  );
101  }
102 
113  {
114  $key = $arguments['key'];
115  $id = $arguments['id'];
116  $default = $arguments['default'];
117  $htmlEscape = $arguments['htmlEscape'];
118  $extensionName = $arguments['extensionName'];
119  $arguments = $arguments['arguments'];
120 
121  // Wrapper including a compatibility layer for TYPO3 Flow Translation
122  if ($id === null) {
123  $id = $key;
124  }
125 
126  if ((string)$id === '') {
127  throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
128  }
129 
130  $request = $renderingContext->getControllerContext()->getRequest();
131  $extensionName = $extensionName === null ? $request->getControllerExtensionName() : $extensionName;
132  $value = static::translate($id, $extensionName, $arguments);
133  if ($value === null) {
134  $value = $default !== null ? $default : $renderChildrenClosure();
135  if (!empty($arguments)) {
136  $value = vsprintf($value, $arguments);
137  }
138  } elseif ($htmlEscape) {
139  $value = htmlspecialchars($value);
140  }
141  return $value;
142  }
143 
153  protected static function translate($id, $extensionName, $arguments)
154  {
155  return LocalizationUtility::translate($id, $extensionName, $arguments);
156  }
157 }