2 namespace TYPO3\CMS\Fluid\ViewHelpers
;
5 * This script is part of the TYPO3 project - inspiring people to share! *
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. *
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. *
17 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface
;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException
;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface
;
23 * Translate a key from locallang. The files are loaded from the folder
24 * "Resources/Private/Language/".
28 * <code title="Translate key">
29 * <f:translate key="key1" />
32 * value of key "key1" in the current website language
35 * <code title="Keep HTML tags">
36 * <f:translate key="htmlKey" htmlEscape="false" />
39 * value of key "htmlKey" in the current website language, no htmlspecialchars applied
42 * <code title="Translate key from custom locallang file">
43 * <f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xlf:key1" />
46 * value of key "key1" in the current website language
49 * <code title="Inline notation with arguments and default value">
50 * {f:translate(key: 'argumentsKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
53 * value of key "argumentsKey" in the current website language
54 * with "%1" and "%2" are replaced by "dog" and "fox" (printf)
55 * if the key is not found, the output is "default value"
58 * <code title="Inline notation with extension name">
59 * {f:translate(key: 'someKey', extensionName: 'SomeExtensionName')}
62 * value of key "someKey" in the current website language
63 * the locallang file of extension "some_extension_name" will be used
66 * <code title="Translate id as in TYPO3 Flow">
67 * <f:translate id="key1" />
70 * value of id "key1" in the current website language
73 class TranslateViewHelper
extends AbstractViewHelper
implements CompilableInterface
{
78 * @param string $key Translation Key
79 * @param string $id Translation Key compatible to TYPO3 Flow
80 * @param string $default 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
81 * @param bool $htmlEscape TRUE if the result should be htmlescaped. This won't have an effect for the default value
82 * @param array $arguments Arguments to be replaced in the resulting string
83 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
84 * @return string The translated key or tag body if key doesn't exist
86 public function render($key = NULL, $id = NULL, $default = NULL, $htmlEscape = NULL, array $arguments = NULL, $extensionName = NULL) {
87 return self
::renderStatic(
91 'default' => $default,
92 'htmlEscape' => $htmlEscape,
93 'arguments' => $arguments,
94 'extensionName' => $extensionName,
96 $this->buildRenderChildrenClosure(),
97 $this->renderingContext
102 * Return array element by key.
104 * @param array $arguments
105 * @param \Closure $renderChildrenClosure
106 * @param RenderingContextInterface $renderingContext
107 * @throws InvalidVariableException
110 static public function renderStatic(array $arguments, \Closure
$renderChildrenClosure, RenderingContextInterface
$renderingContext) {
111 $key = $arguments['key'];
112 $id = $arguments['id'];
113 $default = $arguments['default'];
114 $htmlEscape = $arguments['htmlEscape'];
115 $extensionName = $arguments['extensionName'];
116 $arguments = $arguments['arguments'];
118 // Wrapper including a compatibility layer for TYPO3 Flow Translation
123 if ((string)$id === '') {
124 throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
127 $request = $renderingContext->getControllerContext()->getRequest();
128 $extensionName = $extensionName === NULL ?
$request->getControllerExtensionName() : $extensionName;
129 $value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility
::translate($id, $extensionName, $arguments);
130 if ($value === NULL) {
131 $value = $default !== NULL ?
$default : $renderChildrenClosure();
132 if (!empty($arguments)) {
133 $value = vsprintf($value, $arguments);
135 } elseif ($htmlEscape) {
136 $value = htmlspecialchars($value);