/framework/I18N/TTranslate.php

https://bitbucket.org/volatileeight/prado · PHP · 254 lines · 111 code · 23 blank · 120 comment · 12 complexity · a9e373192a71ac986830ce1a8948b136 MD5 · raw file

  1. <?php
  2. /**
  3. * TTranslate, I18N translation component.
  4. *
  5. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  6. * @link http://www.pradosoft.com/
  7. * @copyright Copyright &copy; 2005-2014 PradoSoft
  8. * @license http://www.pradosoft.com/license/
  9. * @package System.I18N
  10. */
  11. /**
  12. * Get the parent control class.
  13. */
  14. Prado::using('System.I18N.TI18NControl');
  15. /**
  16. * TTranslate class.
  17. *
  18. * This component performs message/string translation. The translation
  19. * source is set in the TGlobalization handler. The following example
  20. * demonstrated a simple message translation.
  21. * <code>
  22. * <com:TTranslate Text="Goodbye" />
  23. * </code>
  24. *
  25. * Depending on the culture set on the page, the phrase "Goodbye" will
  26. * be translated.
  27. *
  28. * The {@link getParameters Parameters} property can be use to add name values pairs for
  29. * substitution. Substrings enclosed with "{" and "}" in the translation message are consider as the
  30. * parameter names during substitution lookup. The following example will substitute the substring
  31. * "{time}" with the value of the parameter attribute "Parameters.time=<%= time() %>. Note that
  32. * the value of the parameter named "time" is evaluated.
  33. * <code>
  34. * <com:TTranslate Parameters.time=<%= time() %> >
  35. * The unix-time is "{time}".
  36. * </com:TTranslate>
  37. * </code>
  38. *
  39. * More complex string substitution can be applied using the
  40. * TTranslateParameter component.
  41. *
  42. * Namespace: System.I18N
  43. *
  44. * Properties
  45. * - <b>Text</b>, string,
  46. * <br>Gets or sets the string to translate.
  47. * - <b>Catalogue</b>, string,
  48. * <br>Gets or sets the catalogue for message translation. The
  49. * default catalogue can be set by the @Page directive.
  50. * - <b>Key</b>, string,
  51. * <br>Gets or sets the key used to message look up.
  52. * - <b>Trim</b>, boolean,
  53. * <br>Gets or sets an option to trim the contents.
  54. * Default is to trim the contents.
  55. *
  56. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  57. * @version v1.0, last update on Fri Dec 24 21:38:49 EST 2004
  58. * @package System.I18N
  59. */
  60. class TTranslate extends TI18NControl
  61. {
  62. /**
  63. * @return string the text to be localized/translated.
  64. */
  65. public function getText()
  66. {
  67. return $this->getViewState('Text','');
  68. }
  69. /**
  70. * Sets the text for localization.
  71. * @param string the text for translation.
  72. */
  73. public function setText($value)
  74. {
  75. $this->setViewState('Text',$value,'');
  76. }
  77. /**
  78. * Set the key for message lookup.
  79. * @param string key
  80. */
  81. public function setKey($value)
  82. {
  83. $this->setViewState('Key',$value,'');
  84. }
  85. /**
  86. * Get the key for message lookup.
  87. * @return string key
  88. */
  89. public function getKey()
  90. {
  91. return $this->getViewState('Key','');
  92. }
  93. /**
  94. * Get the message catalogue.
  95. * @return string catalogue.
  96. */
  97. public function getCatalogue()
  98. {
  99. return $this->getViewState('Catalogue','');
  100. }
  101. /**
  102. * Set the message catalogue.
  103. * @param string catalogue.
  104. */
  105. public function setCatalogue($value)
  106. {
  107. $this->setViewState('Catalogue',$value,'');
  108. }
  109. /**
  110. * Set the option to trim the contents.
  111. * @param boolean trim or not.
  112. */
  113. public function setTrim($value)
  114. {
  115. $this->setViewState('Trim',TPropertyValue::ensureBoolean($value),true);
  116. }
  117. /**
  118. * Trim the content or not.
  119. * @return boolean trim or not.
  120. */
  121. public function getTrim()
  122. {
  123. return $this->getViewState('Trim',true);
  124. }
  125. /**
  126. * Returns the list of custom parameters.
  127. * Custom parameters are name-value pairs that may subsititute translation
  128. * place holders during rendering.
  129. * @return TAttributeCollection the list of custom parameters
  130. */
  131. public function getParameters()
  132. {
  133. if($parameters=$this->getViewState('Parameters',null))
  134. return $parameters;
  135. else
  136. {
  137. $parameters=new TAttributeCollection;
  138. $parameters->setCaseSensitive(true);
  139. $this->setViewState('Parameters',$parameters,null);
  140. return $parameters;
  141. }
  142. }
  143. /**
  144. * @return boolean whether the named parameter exists
  145. */
  146. public function hasParameter($name)
  147. {
  148. if($parameters=$this->getViewState('Parameters',null))
  149. return $parameters->contains($name);
  150. else
  151. return false;
  152. }
  153. /**
  154. * @return string parameter value, null if parameter does not exist
  155. */
  156. public function getParameter($name)
  157. {
  158. if($parameters=$this->getViewState('Parameters',null))
  159. return $parameters->itemAt($name);
  160. else
  161. return null;
  162. }
  163. /**
  164. * @param string parameter name
  165. * @param string value of the parameter
  166. */
  167. public function setParameter($name,$value)
  168. {
  169. $this->getParameters()->add($name,$value);
  170. }
  171. /**
  172. * Removes the named parameter.
  173. * @param string the name of the parameter to be removed.
  174. * @return string parameter value removed, null if parameter does not exist.
  175. */
  176. public function removeParameter($name)
  177. {
  178. if($parameters=$this->getViewState('Parameters',null))
  179. return $parameters->remove($name);
  180. else
  181. return null;
  182. }
  183. /**
  184. * renders the translated string.
  185. */
  186. public function render($writer)
  187. {
  188. $htmlWriter = Prado::createComponent($this->GetResponse()->getHtmlWriterType(), new TTextWriter());
  189. $subs = array();
  190. foreach($this->getParameters() as $key => $value)
  191. $subs['{'.$key.'}'] = $value;
  192. foreach($this->getControls() as $control)
  193. {
  194. if($control instanceof TTranslateParameter)
  195. $subs['{'.$control->getKey().'}'] = $control->getParameter();
  196. elseif($control instanceof TControl)
  197. $control->render($htmlWriter);
  198. elseif(is_string($control))
  199. $htmlWriter->write($control);
  200. }
  201. $text = $this->getText();
  202. if(strlen($text)==0)
  203. $text = $htmlWriter->flush();
  204. if($this->getTrim())
  205. $text = trim($text);
  206. $writer->write($this->translateText($text, $subs));
  207. }
  208. /**
  209. * Translates the text with subsititution.
  210. * @param string text for translation
  211. * @param array list of substitutions
  212. * @return string translated text
  213. */
  214. protected function translateText($text, $subs)
  215. {
  216. $app = $this->getApplication()->getGlobalization();
  217. //no translation handler provided
  218. if(($config = $app->getTranslationConfiguration())===null)
  219. return strtr($text, $subs);
  220. $catalogue = $this->getCatalogue();
  221. if(empty($catalogue) && isset($config['catalogue']))
  222. $catalogue = $config['catalogue'];
  223. if (empty($catalogue)) $catalogue='messages';
  224. Translation::init($catalogue);
  225. $key = $this->getKey();
  226. if(!empty($key)) $text = $key;
  227. //translate it
  228. return Translation::formatter($catalogue)->format($text,
  229. $subs, $catalogue, $this->getCharset());
  230. }
  231. }