TYPO3 CMS  TYPO3_6-2
TranslateViewHelper.php
Go to the documentation of this file.
1 <?php
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  * */
68 
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 
90  public function render() {
91  $id = $this->hasArgument('id') ? $this->arguments['id'] : $this->arguments['key'];
92 
93  if (strlen($id) > 0) {
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 
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 }
registerArgument($name, $type, $description, $required=FALSE, $defaultValue=NULL)
static translate($key, $extensionName, $arguments=NULL)