c12c5849db7b12d0df29c2d03c2d1f9808ea4a7a
[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 '\\%1$s::translate(%2$s[\'key\'] ?? %2$s[\'id\'], %2$s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %2$s[\'arguments\'], %2$s[\'languageKey\'], %2$s[\'alternativeLanguageKeys\']) ?? %2$s[\'default\'] ?? %3$s()',
111 LocalizationUtility::class,
112 $argumentsName,
113 $closureName
114 );
115 }
116
117 /**
118 * @return string|null
119 */
120 public function render()
121 {
122 $key = $this->arguments['key'];
123 $id = $this->arguments['id'];
124 $default = $this->arguments['default'];
125 $extensionName = $this->arguments['extensionName'];
126 $arguments = $this->arguments['arguments'];
127 $languageKey = $this->arguments['languageKey'];
128 $alternativeLanguageKeys = $this->arguments['alternativeLanguageKeys'];
129
130 if (empty($id) && empty($key)) {
131 throw new InvalidVariableException('Either "key" or "id" must be provided for f:translate', 1351584844);
132 }
133
134 // Wrapper including a compatibility layer for TYPO3 Flow Translation
135 if ($id === null) {
136 $id = $key;
137 }
138
139 $request = $this->renderingContext->getControllerContext()->getRequest();
140 try {
141 $value = $this->translate(
142 $key ?? $id,
143 $extensionName ?? $request->getControllerExtensionName(),
144 $arguments,
145 $languageKey,
146 $alternativeLanguageKeys
147 );
148 } catch (\InvalidArgumentException $e) {
149 $value = null;
150 }
151 if ($value === null) {
152 $value = $default !== null ? $default : $this->renderChildren();
153 }
154 return $value;
155 }
156
157 /**
158 * Wrapper call to static LocalizationUtility
159 *
160 * @param string $id Translation Key compatible to TYPO3 Flow
161 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
162 * @param array $arguments Arguments to be replaced in the resulting string
163 * @param string $languageKey Language key to use for this translation
164 * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
165 *
166 * @return string|null
167 */
168 protected function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
169 {
170 return LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
171 }
172 }