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