PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/symfony/i18n/sfMessageFormat.class.php

https://github.com/jonphipps/Metadata-Registry
PHP | 295 lines | 122 code | 35 blank | 138 comment | 15 complexity | 929435e4b886efbab4880e6b21e15811 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * sfMessageFormat class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfMessageFormat.class.php 24622 2009-11-30 23:49:47Z FabianLange $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * Gets the encoding utilities
  21. */
  22. require_once(dirname(__FILE__).'/util.php');
  23. /**
  24. * sfMessageFormat class.
  25. *
  26. * Format a message, that is, for a particular message find the
  27. * translated message. The following is an example using
  28. * a SQLite database to store the translation message.
  29. * Create a new message format instance and echo "Hello"
  30. * in simplified Chinese. This assumes that the world "Hello"
  31. * is translated in the database.
  32. *
  33. * <code>
  34. * $source = sfMessageSource::factory('SQLite', 'sqlite://messages.db');
  35. * $source->setCulture('zh_CN');
  36. * $source->setCache(new sfMessageCache('./tmp'));
  37. *
  38. * $formatter = new sfMessageFormat($source);
  39. *
  40. * echo $formatter->format('Hello');
  41. * </code>
  42. *
  43. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  44. * @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
  45. * @package symfony
  46. * @subpackage i18n
  47. */
  48. class sfMessageFormat
  49. {
  50. /**
  51. * The message source.
  52. * @var sfMessageSource
  53. */
  54. protected $source;
  55. /**
  56. * A list of loaded message catalogues.
  57. * @var array
  58. */
  59. protected $catalogues = array();
  60. /**
  61. * The translation messages.
  62. * @var array
  63. */
  64. protected $messages = array();
  65. /**
  66. * A list of untranslated messages.
  67. * @var array
  68. */
  69. protected $untranslated = array();
  70. /**
  71. * The prefix and suffix to append to untranslated messages.
  72. * @var array
  73. */
  74. protected $postscript = array('', '');
  75. /**
  76. * Set the default catalogue.
  77. * @var string
  78. */
  79. public $Catalogue;
  80. /**
  81. * Output encoding charset
  82. * @var string
  83. */
  84. protected $charset = 'UTF-8';
  85. /**
  86. * Constructor.
  87. * Create a new instance of sfMessageFormat using the messages
  88. * from the supplied message source.
  89. *
  90. * @param MessageSource the source of translation messages.
  91. * @param string charset for the message output.
  92. */
  93. function __construct(sfIMessageSource $source, $charset = 'UTF-8')
  94. {
  95. $this->source = $source;
  96. $this->setCharset($charset);
  97. }
  98. /**
  99. * Sets the charset for message output.
  100. *
  101. * @param string charset, default is UTF-8
  102. */
  103. public function setCharset($charset)
  104. {
  105. $this->charset = $charset;
  106. }
  107. /**
  108. * Gets the charset for message output. Default is UTF-8.
  109. *
  110. * @return string charset, default UTF-8
  111. */
  112. public function getCharset()
  113. {
  114. return $this->charset;
  115. }
  116. /**
  117. * Loads the message from a particular catalogue. A listed
  118. * loaded catalogues is kept to prevent reload of the same
  119. * catalogue. The load catalogue messages are stored
  120. * in the $this->message array.
  121. *
  122. * @param string message catalogue to load.
  123. */
  124. protected function loadCatalogue($catalogue)
  125. {
  126. if (in_array($catalogue, $this->catalogues))
  127. {
  128. return;
  129. }
  130. if ($this->source->load($catalogue))
  131. {
  132. $this->messages[$catalogue] = $this->source->read();
  133. $this->catalogues[] = $catalogue;
  134. }
  135. }
  136. /**
  137. * Formats the string. That is, for a particular string find
  138. * the corresponding translation. Variable subsitution is performed
  139. * for the $args parameter. A different catalogue can be specified
  140. * using the $catalogue parameter.
  141. * The output charset is determined by $this->getCharset();
  142. *
  143. * @param string the string to translate.
  144. * @param array a list of string to substitute.
  145. * @param string get the translation from a particular message
  146. * @param string charset, the input AND output charset catalogue.
  147. * @return string translated string.
  148. */
  149. public function format($string, $args = array(), $catalogue = null, $charset = null)
  150. {
  151. // make sure that objects with __toString() are converted to strings
  152. $string = (string) $string;
  153. if (empty($charset))
  154. {
  155. $charset = $this->getCharset();
  156. }
  157. $s = $this->formatString(I18N_toUTF8($string, $charset), $args, $catalogue);
  158. return I18N_toEncoding($s, $charset);
  159. }
  160. public function formatExists($string, $args = array(), $catalogue = null, $charset = null)
  161. {
  162. if (empty($charset))
  163. {
  164. $charset = $this->getCharset();
  165. }
  166. $s = $this->getFormattedString(I18N_toUTF8($string, $charset), $args, $catalogue);
  167. return I18N_toEncoding($s, $charset);
  168. }
  169. /**
  170. * Do string translation.
  171. *
  172. * @param string the string to translate.
  173. * @param array a list of string to substitute.
  174. * @param string get the translation from a particular message catalogue.
  175. * @return string translated string.
  176. */
  177. protected function formatString($string, $args = array(), $catalogue = null)
  178. {
  179. if (empty($args))
  180. {
  181. $args = array();
  182. }
  183. $target = $this->getFormattedString($string, $args, $catalogue);
  184. // well we did not find the translation string.
  185. if (!$target)
  186. {
  187. $this->source->append($string);
  188. $target = $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1];
  189. }
  190. return $target;
  191. }
  192. protected function getFormattedString($string, $args = array(), $catalogue = null)
  193. {
  194. if (empty($catalogue))
  195. {
  196. $catalogue = empty($this->catalogue) ? 'messages' : $this->catalogue;
  197. }
  198. if (empty($args))
  199. {
  200. $args = array();
  201. }
  202. $this->loadCatalogue($catalogue);
  203. foreach ($this->messages[$catalogue] as $variant)
  204. {
  205. // we found it, so return the target translation
  206. if (isset($variant[$string]))
  207. {
  208. $target = $variant[$string];
  209. // check if it contains only strings.
  210. if (is_array($target))
  211. {
  212. $target = array_shift($target);
  213. }
  214. // found, but untranslated
  215. if (empty($target))
  216. {
  217. return $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1];
  218. }
  219. return $this->replaceArgs($target, $args);
  220. }
  221. }
  222. return null;
  223. }
  224. protected function replaceArgs($string, $args)
  225. {
  226. // replace object with strings
  227. foreach ($args as $key => $value)
  228. {
  229. if (is_object($value) && method_exists($value, '__toString'))
  230. {
  231. $args[$key] = $value->__toString();
  232. }
  233. }
  234. return strtr($string, $args);
  235. }
  236. /**
  237. * Gets the message source.
  238. *
  239. * @return MessageSource
  240. */
  241. function getSource()
  242. {
  243. return $this->source;
  244. }
  245. /**
  246. * Sets the prefix and suffix to append to untranslated messages.
  247. * e.g. $postscript=array('[T]','[/T]'); will output
  248. * "[T]Hello[/T]" if the translation for "Hello" can not be determined.
  249. *
  250. * @param array first element is the prefix, second element the suffix.
  251. */
  252. function setUntranslatedPS($postscript)
  253. {
  254. if (is_array($postscript) && count($postscript) >= 2)
  255. {
  256. $this->postscript[0] = $postscript[0];
  257. $this->postscript[1] = $postscript[1];
  258. }
  259. }
  260. }