[TASK] Move arguments to initializeArguments() in TranslateVH in ext:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / TranslateViewHelper.php
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 */
16 use TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21
22 /**
23 * Translate a key from locallang. The files are loaded from the folder
24 * "Resources/Private/Language/".
25 *
26 * == Examples ==
27 *
28 * <code title="Translate key">
29 * <f:translate key="key1" />
30 * </code>
31 * <output>
32 * value of key "key1" in the current website language
33 * </output>
34 *
35 * <code title="Keep HTML tags">
36 * <f:translate key="htmlKey" htmlEscape="false" />
37 * </code>
38 * <output>
39 * value of key "htmlKey" in the current website language, no htmlspecialchars applied
40 * </output>
41 *
42 * <code title="Translate key from custom locallang file">
43 * <f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xlf:key1" />
44 * </code>
45 * <output>
46 * value of key "key1" in the current website language
47 * </output>
48 *
49 * <code title="Inline notation with arguments and default value">
50 * {f:translate(key: 'argumentsKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
51 * </code>
52 * <output>
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"
56 * </output>
57 *
58 * <code title="Inline notation with extension name">
59 * {f:translate(key: 'someKey', extensionName: 'SomeExtensionName')}
60 * </code>
61 * <output>
62 * value of key "someKey" in the current website language
63 * the locallang file of extension "some_extension_name" will be used
64 * </output>
65 *
66 * <code title="Translate id as in TYPO3 Flow">
67 * <f:translate id="key1" />
68 * </code>
69 * <output>
70 * value of id "key1" in the current website language
71 * </output>
72 */
73 class TranslateViewHelper extends AbstractViewHelper
74 {
75 /**
76 * Output is escaped already. We must not escape children, to avoid double encoding.
77 *
78 * @var bool
79 */
80 protected $escapeChildren = false;
81
82 /**
83 * Initialize arguments.
84 *
85 * @api
86 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
87 */
88 public function initializeArguments()
89 {
90 parent::initializeArguments();
91 $this->registerArgument('key', 'string', 'Translation Key');
92 $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
93 $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');
94 $this->registerArgument('htmlEscape', 'bool', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
95 $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
96 $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
97 }
98
99 /**
100 * Render translation
101 *
102 * @return string The translated key or tag body if key doesn't exist
103 */
104 public function render()
105 {
106 $key = $this->arguments['key'];
107 $id = $this->arguments['id'];
108 $default = $this->arguments['default'];
109 $htmlEscape = $this->arguments['htmlEscape'];
110 $arguments = $this->arguments['arguments'];
111 $extensionName = $this->arguments['extensionName'];
112
113 return static::renderStatic(
114 array(
115 'key' => $key,
116 'id' => $id,
117 'default' => $default,
118 'htmlEscape' => $htmlEscape,
119 'arguments' => $arguments,
120 'extensionName' => $extensionName,
121 ),
122 $this->buildRenderChildrenClosure(),
123 $this->renderingContext
124 );
125 }
126
127 /**
128 * Return array element by key.
129 *
130 * @param array $arguments
131 * @param \Closure $renderChildrenClosure
132 * @param RenderingContextInterface $renderingContext
133 * @throws InvalidVariableException
134 * @return string
135 */
136 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
137 {
138 $key = $arguments['key'];
139 $id = $arguments['id'];
140 $default = $arguments['default'];
141 $htmlEscape = $arguments['htmlEscape'];
142 $extensionName = $arguments['extensionName'];
143 $arguments = $arguments['arguments'];
144
145 if ($htmlEscape !== null) {
146 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.');
147 }
148
149 // Wrapper including a compatibility layer for TYPO3 Flow Translation
150 if ($id === null) {
151 $id = $key;
152 }
153
154 if ((string)$id === '') {
155 throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
156 }
157
158 $request = $renderingContext->getControllerContext()->getRequest();
159 $extensionName = $extensionName === null ? $request->getControllerExtensionName() : $extensionName;
160 $value = static::translate($id, $extensionName, $arguments);
161 if ($value === null) {
162 $value = $default !== null ? $default : $renderChildrenClosure();
163 if (!empty($arguments)) {
164 $value = vsprintf($value, $arguments);
165 }
166 }
167 return $value;
168 }
169
170 /**
171 * Wrapper call to static LocalizationUtility
172 *
173 * @param string $id Translation Key compatible to TYPO3 Flow
174 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
175 * @param array $arguments Arguments to be replaced in the resulting string
176 *
177 * @return NULL|string
178 */
179 protected static function translate($id, $extensionName, $arguments)
180 {
181 return LocalizationUtility::translate($id, $extensionName, $arguments);
182 }
183 }