[FEATURE] Add native browser lazy loading for images
[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 TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23 /**
24 * Translate a key from locallang. The files are loaded from the folder
25 * :file:`Resources/Private/Language/`.
26 *
27 * Examples
28 * ========
29 *
30 * Translate key
31 * -------------
32 *
33 * ::
34 *
35 * <f:translate key="key1" />
36 *
37 * Value of key ``key1`` in the current website language. Alternatively id can
38 * be used instead of key::
39 *
40 * <f:translate id="key1" />
41 *
42 * This will output the same as above. If both id and key are set, id will take precedence.
43 *
44 * Keep HTML tags
45 * --------------
46 *
47 * ::
48 *
49 * <f:format.raw><f:translate key="htmlKey" /></f:format.raw>
50 *
51 * Value of key ``htmlKey`` in the current website language, no :php:`htmlspecialchars()` applied.
52 *
53 * Translate key from custom locallang file
54 * ----------------------------------------
55 *
56 * ::
57 *
58 * <f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xlf:key1" />
59 *
60 * Value of key ``key1`` in the current website language.
61 *
62 * Inline notation with arguments and default value
63 * ------------------------------------------------
64 *
65 * ::
66 *
67 * {f:translate(key: 'argumentsKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
68 *
69 * Value of key ``argumentsKey`` in the current website language
70 * with ``%1`` and ``%2`` are replaced by "dog" and "fox" (:php:`printf()`).
71 * If the key is not found, the output is "default value".
72 *
73 * Inline notation with extension name
74 * -----------------------------------
75 *
76 * ::
77 *
78 * {f:translate(key: 'someKey', extensionName: 'SomeExtensionName')}
79 *
80 * Value of key ``someKey`` in the current website language.
81 * The locallang file of extension "some_extension_name" will be used.
82 */
83 class TranslateViewHelper extends AbstractViewHelper
84 {
85 use CompileWithRenderStatic;
86
87 /**
88 * Output is escaped already. We must not escape children, to avoid double encoding.
89 *
90 * @var bool
91 */
92 protected $escapeChildren = false;
93
94 /**
95 * Initialize arguments.
96 *
97 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
98 */
99 public function initializeArguments()
100 {
101 $this->registerArgument('key', 'string', 'Translation Key');
102 $this->registerArgument('id', 'string', 'Translation ID. Same as key.');
103 $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');
104 $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
105 $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
106 $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');
107 $this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist');
108 }
109
110 /**
111 * Return array element by key.
112 *
113 * @param array $arguments
114 * @param \Closure $renderChildrenClosure
115 * @param RenderingContextInterface $renderingContext
116 * @throws Exception
117 * @return string
118 */
119 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
120 {
121 $key = $arguments['key'];
122 $id = $arguments['id'];
123 $default = $arguments['default'];
124 $extensionName = $arguments['extensionName'];
125 $translateArguments = $arguments['arguments'];
126
127 // Use key if id is empty.
128 if ($id === null) {
129 $id = $key;
130 }
131
132 if ((string)$id === '') {
133 throw new Exception('An argument "key" or "id" has to be provided', 1351584844);
134 }
135
136 $request = $renderingContext->getControllerContext()->getRequest();
137 $extensionName = $extensionName ?? $request->getControllerExtensionName();
138 try {
139 $value = static::translate($id, $extensionName, $translateArguments, $arguments['languageKey'], $arguments['alternativeLanguageKeys']);
140 } catch (\InvalidArgumentException $e) {
141 $value = null;
142 }
143 if ($value === null) {
144 $value = $default ?? $renderChildrenClosure();
145 if (!empty($translateArguments)) {
146 $value = vsprintf($value, $translateArguments);
147 }
148 }
149 return $value;
150 }
151
152 /**
153 * Wrapper call to static LocalizationUtility
154 *
155 * @param string $id Translation Key
156 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
157 * @param array $arguments Arguments to be replaced in the resulting string
158 * @param string $languageKey Language key to use for this translation
159 * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
160 *
161 * @return string|null
162 */
163 protected static function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
164 {
165 return LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
166 }
167 }