TYPO3 API  SVNRelease
TranslateViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script is part of the TYPO3 project - inspiring people to share!  *
00005  *                                                                        *
00006  * TYPO3 is free software; you can redistribute it and/or modify it under *
00007  * the terms of the GNU General Public License version 2 as published by  *
00008  * the Free Software Foundation.                                          *
00009  *                                                                        *
00010  * This script is distributed in the hope that it will be useful, but     *
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00012  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
00013  * Public License for more details.                                       *
00014  *                                                                        */
00015 
00016 /**
00017  * Translate a key from locallang. The files are loaded from the folder
00018  * "Resources/Private/Language/".
00019  *
00020  * == Examples ==
00021  *
00022  * <code title="Translate key">
00023  * <f:translate key="key1" />
00024  * </code>
00025  * <output>
00026  * // value of key "key1" in the current website language
00027  * </output>
00028  *
00029  * <code title="Keep HTML tags">
00030  * <f:translate key="htmlKey" htmlEscape="false" />
00031  * </code>
00032  * <output>
00033  * // value of key "htmlKey" in the current website language, no htmlspecialchars applied
00034  * </output>
00035  *
00036  * <code title="Translate key from custom locallang file">
00037  * <f:translate key="LLL:EXT:myext/Resources/Private/Language/locallang.xml:key1" />
00038  * </code>
00039  * <output>
00040  * // value of key "key1" in the current website language
00041  * </output>
00042  *
00043  * <code title="Inline notation with arguments and default value">
00044  * {f:translate(key: 'argumentsKey', arguments: {0: 'dog', 1: 'fox'}, default: 'default value')}
00045  * </code>
00046  * <output>
00047  * // value of key "argumentsKey" in the current website language
00048  * // with "%1" and "%2" are replaced by "dog" and "fox" (printf)
00049  * // if the key is not found, the output is "default value"
00050  * </output>
00051  */
00052 class Tx_Fluid_ViewHelpers_TranslateViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00053 
00054     /**
00055      * Translate a given key or use the tag body as default.
00056      *
00057      * @param string $key The locallang key
00058      * @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
00059      * @param boolean $htmlEscape TRUE if the result should be htmlescaped. This won't have an effect for the default value
00060      * @param array $arguments Arguments to be replaced in the resulting string
00061      * @return string The translated key or tag body if key doesn't exist
00062      * @author Christopher Hlubek <hlubek@networkteam.com>
00063      * @author Bastian Waidelich <bastian@typo3.org>
00064      */
00065     public function render($key, $default = NULL, $htmlEscape = TRUE, array $arguments = NULL) {
00066         $request = $this->controllerContext->getRequest();
00067         $extensionName = $request->getControllerExtensionName();
00068         $value = Tx_Extbase_Utility_Localization::translate($key, $extensionName, $arguments);
00069         if ($value === NULL) {
00070             $value = $default !== NULL ? $default : $this->renderChildren();
00071         } elseif ($htmlEscape) {
00072             $value = htmlspecialchars($value);
00073         }
00074         return $value;
00075     }
00076 }
00077 
00078 ?>