[CLEANUP] Replace strlen() with === for zero length check
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / TranslateViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16 /**
17 * Translate a key from locallang. The files are loaded from the folder
18 * "Resources/Private/Language/".
19 *
20 * == Examples ==
21 *
22 * <code title="Translate key">
23 * <f:translate key="key1" />
24 * </code>
25 * <output>
26 * value of key "key1" in the current website language
27 * </output>
28 *
29 * <code title="Keep HTML tags">
30 * <f:translate key="htmlKey" htmlEscape="false" />
31 * </code>
32 * <output>
33 * value of key "htmlKey" in the current website language, no htmlspecialchars applied
34 * </output>
35 *
36 * <code title="Translate key from custom locallang file">
37 * <f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xlf:key1" />
38 * </code>
39 * <output>
40 * value of key "key1" in the current website language
41 * </output>
42 *
43 * <code title="Inline notation with arguments and default value">
44 * {f:translate(key: 'argumentsKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
45 * </code>
46 * <output>
47 * value of key "argumentsKey" in the current website language
48 * with "%1" and "%2" are replaced by "dog" and "fox" (printf)
49 * if the key is not found, the output is "default value"
50 * </output>
51 *
52 * <code title="Inline notation with extension name">
53 * {f:translate(key: 'someKey', extensionName: 'SomeExtensionName')}
54 * </code>
55 * <output>
56 * value of key "someKey" in the current website language
57 * the locallang file of extension "some_extension_name" will be used
58 * </output>
59 *
60 * <code title="Translate id as in TYPO3 Flow">
61 * <f:translate id="key1" />
62 * </code>
63 * <output>
64 * value of id "key1" in the current website language
65 * </output>
66 */
67 class TranslateViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
68
69 /**
70 * Initializes arguments for Translate ViewHelper
71 *
72 * @return void
73 */
74 public function initializeArguments() {
75 $this->registerArgument('key', 'string', 'Translation Key');
76 $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
77 $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');
78 $this->registerArgument('htmlEscape', 'boolean', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
79 $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
80 $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
81 }
82
83 /**
84 * Wrapper function including a compatibility layer for TYPO3 Flow Translation
85 *
86 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException
87 *
88 * @return string The translated key or tag body if key doesn't exist
89 */
90 public function render() {
91 $id = $this->hasArgument('id') ? $this->arguments['id'] : $this->arguments['key'];
92
93 if ((string)$id !== '') {
94 return $this->renderTranslation($id);
95 } else {
96 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
97 }
98 }
99
100 /**
101 * Translate a given key or use the tag body as default.
102 *
103 * @param string $id The locallang id
104 * @return string The translated key or tag body if key doesn't exist
105 */
106 protected function renderTranslation($id) {
107 $request = $this->controllerContext->getRequest();
108 $extensionName = $this->arguments['extensionName'] === NULL ? $request->getControllerExtensionName() : $this->arguments['extensionName'];
109 $value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($id, $extensionName, $this->arguments['arguments']);
110 if ($value === NULL) {
111 $value = $this->arguments['default'] !== NULL ? $this->arguments['default'] : $this->renderChildren();
112 if (is_array($this->arguments['arguments'])) {
113 $value = vsprintf($value, $this->arguments['arguments']);
114 }
115 } elseif ($this->arguments['htmlEscape']) {
116 $value = htmlspecialchars($value);
117 }
118 return $value;
119 }
120
121 }