Merge "[TASK] Increase version number"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / TranslateViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
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.xml: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 class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
53
54 /**
55 * Translate a given key or use the tag body as default.
56 *
57 * @param string $key The locallang key
58 * @param string $default 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
59 * @param boolean $htmlEscape TRUE if the result should be htmlescaped. This won't have an effect for the default value
60 * @param array $arguments Arguments to be replaced in the resulting string
61 * @return string The translated key or tag body if key doesn't exist
62 */
63 public function render($key, $default = NULL, $htmlEscape = TRUE, array $arguments = NULL) {
64 $request = $this->controllerContext->getRequest();
65 $extensionName = $request->getControllerExtensionName();
66 $value = Tx_Extbase_Utility_Localization::translate($key, $extensionName, $arguments);
67 if ($value === NULL) {
68 $value = $default !== NULL ? $default : $this->renderChildren();
69 } elseif ($htmlEscape) {
70 $value = htmlspecialchars($value);
71 }
72 return $value;
73 }
74 }
75
76 ?>