[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
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 parent::initializeArguments();
93 $this->registerArgument('key', 'string', 'Translation Key');
94 $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
95 $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');
96 $this->registerArgument('htmlEscape', 'bool', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
97 $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
98 $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
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 $htmlEscape = $arguments['htmlEscape'];
116 $extensionName = $arguments['extensionName'];
117 $arguments = $arguments['arguments'];
118
119 if ($htmlEscape !== null) {
120 GeneralUtility::logDeprecatedViewHelperAttribute(
121 'htmlEscape',
122 $renderingContext,
123 'Please wrap the view helper in <f:format.raw> if you want to disable HTML escaping, which is enabled by default now.'
124 );
125 }
126
127 // Wrapper including a compatibility layer for TYPO3 Flow Translation
128 if ($id === null) {
129 $id = $key;
130 }
131
132 if ((string)$id === '') {
133 throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
134 }
135
136 $request = $renderingContext->getControllerContext()->getRequest();
137 $extensionName = $extensionName === null ? $request->getControllerExtensionName() : $extensionName;
138 try {
139 $value = static::translate($id, $extensionName, $arguments);
140 } catch (\InvalidArgumentException $e) {
141 $value = null;
142 }
143 if ($value === null) {
144 $value = $default !== null ? $default : $renderChildrenClosure();
145 if (!empty($arguments)) {
146 $value = vsprintf($value, $arguments);
147 }
148 }
149 return $value;
150 }
151
152 /**
153 * Wrapper call to static LocalizationUtility
154 *
155 * @param string $id Translation Key compatible to TYPO3 Flow
156 * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
157 * @param array $arguments Arguments to be replaced in the resulting string
158 *
159 * @return null|string
160 */
161 protected static function translate($id, $extensionName, $arguments)
162 {
163 return LocalizationUtility::translate($id, $extensionName, $arguments);
164 }
165 }