[TASK] Use null coalescing operator where possible
[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\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
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 use CompileWithRenderStatic;
77
78 /**
79 * Output is escaped already. We must not escape children, to avoid double encoding.
80 *
81 * @var bool
82 */
83 protected $escapeChildren = false;
84
85 /**
86 * Initialize arguments.
87 *
88 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
89 */
90 public function initializeArguments()
91 {
92 $this->registerArgument('key', 'string', 'Translation Key');
93 $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
94 $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');
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 $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');
98 $this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist');
99 }
100
101 /**
102 * Return array element by key.
103 *
104 * @param array $arguments
105 * @param \Closure $renderChildrenClosure
106 * @param RenderingContextInterface $renderingContext
107 * @throws InvalidVariableException
108 * @return string
109 */
110 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
111 {
112 $key = $arguments['key'];
113 $id = $arguments['id'];
114 $default = $arguments['default'];
115 $extensionName = $arguments['extensionName'];
116 $translateArguments = $arguments['arguments'];
117
118 // Wrapper including a compatibility layer for TYPO3 Flow Translation
119 if ($id === null) {
120 $id = $key;
121 }
122
123 if ((string)$id === '') {
124 throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
125 }
126
127 $request = $renderingContext->getControllerContext()->getRequest();
128 $extensionName = $extensionName ?? $request->getControllerExtensionName();
129 try {
130 $value = static::translate($id, $extensionName, $translateArguments, $arguments['languageKey'], $arguments['alternativeLanguageKeys']);
131 } catch (\InvalidArgumentException $e) {
132 $value = null;
133 }
134 if ($value === null) {
135 $value = $default ?? $renderChildrenClosure();
136 if (!empty($translateArguments)) {
137 $value = vsprintf($value, $translateArguments);
138 }
139 }
140 return $value;
141 }
142
143 /**
144 * Wrapper call to static LocalizationUtility
145 *
146 * @param string $id Translation Key compatible to TYPO3 Flow
147 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
148 * @param array $arguments Arguments to be replaced in the resulting string
149 * @param string $languageKey Language key to use for this translation
150 * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
151 *
152 * @return string|null
153 */
154 protected static function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
155 {
156 return LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
157 }
158 }