[TASK] Improve TranslateViewHelper compiling
[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
17 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
19 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
20 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
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:format.raw><f:translate key="htmlKey" /></f:format.raw>
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
75 {
76 /**
77 * Output is escaped already. We must not escape children, to avoid double encoding.
78 *
79 * @var bool
80 */
81 protected $escapeChildren = false;
82
83 /**
84 * Initialize arguments.
85 *
86 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
87 */
88 public function initializeArguments()
89 {
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('arguments', 'array', 'Arguments to be replaced in the resulting string');
94 $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
95 $this->registerArgument('languageKey', 'string', 'Language key ("dk" for example) or "default" to use for this translation. If this argument is empty, we use the current language');
96 $this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist');
97 }
98
99 /**
100 * @param string $argumentsName
101 * @param string $closureName
102 * @param string $initializationPhpCode
103 * @param ViewHelperNode $node
104 * @param TemplateCompiler $compiler
105 * @return string
106 */
107 public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
108 {
109 return sprintf(
110 '\\%s::translate(%s[\'key\'] ?? %s[\'id\'], %s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %s[\'arguments\'], %s[\'languageKey\'], %s[\'alternativeLanguageKeys\']) ?? %s[\'default\'] ?? %s()',
111 LocalizationUtility::class,
112 $argumentsName,
113 $argumentsName,
114 $argumentsName,
115 $argumentsName,
116 $argumentsName,
117 $argumentsName,
118 $argumentsName,
119 $closureName
120 );
121 }
122
123 /**
124 * @return string|null
125 */
126 public function render()
127 {
128 $key = $this->arguments['key'];
129 $id = $this->arguments['id'];
130 $default = $this->arguments['default'];
131 $extensionName = $this->arguments['extensionName'];
132 $arguments = $this->arguments['arguments'];
133 $languageKey = $this->arguments['languageKey'];
134 $alternativeLanguageKeys = $this->arguments['alternativeLanguageKeys'];
135
136 if (empty($id) && empty($key)) {
137 throw new InvalidVariableException('Either "key" or "id" must be provided for f:translate', 1351584844);
138 }
139
140 // Wrapper including a compatibility layer for TYPO3 Flow Translation
141 if ($id === null) {
142 $id = $key;
143 }
144
145 $request = $this->renderingContext->getControllerContext()->getRequest();
146 try {
147 $value = $this->translate(
148 $key ?? $id,
149 $extensionName ?? $request->getControllerExtensionName(),
150 $arguments,
151 $languageKey,
152 $alternativeLanguageKeys
153 );
154 } catch (\InvalidArgumentException $e) {
155 $value = null;
156 }
157 if ($value === null) {
158 $value = $default !== null ? $default : $this->renderChildren();
159 }
160 return $value;
161 }
162
163 /**
164 * Wrapper call to static LocalizationUtility
165 *
166 * @param string $id Translation Key compatible to TYPO3 Flow
167 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
168 * @param array $arguments Arguments to be replaced in the resulting string
169 * @param string $languageKey Language key to use for this translation
170 * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
171 *
172 * @return string|null
173 */
174 protected function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
175 {
176 return LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
177 }
178 }