/libraries/joomla/language/text.php

https://bitbucket.org/asosso/joomla31 · PHP · 333 lines · 189 code · 15 blank · 129 comment · 25 complexity · c5c82838713e0a30300e92eea2f930b1 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Language
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Text handling class.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Language
  15. * @since 11.1
  16. */
  17. class JText
  18. {
  19. /**
  20. * javascript strings
  21. *
  22. * @var array
  23. * @since 11.1
  24. */
  25. protected static $strings = array();
  26. /**
  27. * Translates a string into the current language.
  28. *
  29. * Examples:
  30. * <script>alert(Joomla.JText._('<?php echo JText::_("JDEFAULT", array("script"=>true));?>'));</script>
  31. * will generate an alert message containing 'Default'
  32. * <?php echo JText::_("JDEFAULT");?> it will generate a 'Default' string
  33. *
  34. * @param string $string The string to translate.
  35. * @param mixed $jsSafe Boolean: Make the result javascript safe.
  36. * @param boolean $interpretBackSlashes To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
  37. * @param boolean $script To indicate that the string will be push in the javascript language store
  38. *
  39. * @return string The translated string or the key is $script is true
  40. *
  41. * @since 11.1
  42. */
  43. public static function _($string, $jsSafe = false, $interpretBackSlashes = true, $script = false)
  44. {
  45. $lang = JFactory::getLanguage();
  46. if (is_array($jsSafe))
  47. {
  48. if (array_key_exists('interpretBackSlashes', $jsSafe))
  49. {
  50. $interpretBackSlashes = (boolean) $jsSafe['interpretBackSlashes'];
  51. }
  52. if (array_key_exists('script', $jsSafe))
  53. {
  54. $script = (boolean) $jsSafe['script'];
  55. }
  56. if (array_key_exists('jsSafe', $jsSafe))
  57. {
  58. $jsSafe = (boolean) $jsSafe['jsSafe'];
  59. }
  60. else
  61. {
  62. $jsSafe = false;
  63. }
  64. }
  65. if (!(strpos($string, ',') === false))
  66. {
  67. $test = substr($string, strpos($string, ','));
  68. if (strtoupper($test) === $test)
  69. {
  70. $strs = explode(',', $string);
  71. foreach ($strs as $i => $str)
  72. {
  73. $strs[$i] = $lang->_($str, $jsSafe, $interpretBackSlashes);
  74. if ($script)
  75. {
  76. self::$strings[$str] = $strs[$i];
  77. }
  78. }
  79. $str = array_shift($strs);
  80. $str = vsprintf($str, $strs);
  81. return $str;
  82. }
  83. }
  84. if ($script)
  85. {
  86. self::$strings[$string] = $lang->_($string, $jsSafe, $interpretBackSlashes);
  87. return $string;
  88. }
  89. else
  90. {
  91. return $lang->_($string, $jsSafe, $interpretBackSlashes);
  92. }
  93. }
  94. /**
  95. * Translates a string into the current language.
  96. *
  97. * Examples:
  98. * <?php echo JText::alt("JALL","language");?> it will generate a 'All' string in English but a "Toutes" string in French
  99. * <?php echo JText::alt("JALL","module");?> it will generate a 'All' string in English but a "Tous" string in French
  100. *
  101. * @param string $string The string to translate.
  102. * @param string $alt The alternate option for global string
  103. * @param mixed $jsSafe Boolean: Make the result javascript safe.
  104. * @param boolean $interpretBackSlashes To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
  105. * @param boolean $script To indicate that the string will be pushed in the javascript language store
  106. *
  107. * @return string The translated string or the key if $script is true
  108. *
  109. * @since 11.1
  110. */
  111. public static function alt($string, $alt, $jsSafe = false, $interpretBackSlashes = true, $script = false)
  112. {
  113. $lang = JFactory::getLanguage();
  114. if ($lang->hasKey($string . '_' . $alt))
  115. {
  116. return self::_($string . '_' . $alt, $jsSafe, $interpretBackSlashes);
  117. }
  118. else
  119. {
  120. return self::_($string, $jsSafe, $interpretBackSlashes);
  121. }
  122. }
  123. /**
  124. * Like JText::sprintf but tries to pluralise the string.
  125. *
  126. * Note that this method can take a mixed number of arguments as for the sprintf function.
  127. *
  128. * The last argument can take an array of options:
  129. *
  130. * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
  131. *
  132. * where:
  133. *
  134. * jsSafe is a boolean to generate a javascript safe strings.
  135. * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
  136. * script is a boolean to indicate that the string will be push in the javascript language store.
  137. *
  138. * Examples:
  139. * <script>alert(Joomla.JText._('<?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1, array("script"=>true));?>'));</script>
  140. * will generate an alert message containing '1 plugin successfully disabled'
  141. * <?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1);?> it will generate a '1 plugin successfully disabled' string
  142. *
  143. * @param string $string The format string.
  144. * @param integer $n The number of items
  145. *
  146. * @return string The translated strings or the key if 'script' is true in the array of options
  147. *
  148. * @since 11.1
  149. */
  150. public static function plural($string, $n)
  151. {
  152. $lang = JFactory::getLanguage();
  153. $args = func_get_args();
  154. $count = count($args);
  155. if ($count > 1)
  156. {
  157. // Try the key from the language plural potential suffixes
  158. $found = false;
  159. $suffixes = $lang->getPluralSuffixes((int) $n);
  160. array_unshift($suffixes, (int) $n);
  161. foreach ($suffixes as $suffix)
  162. {
  163. $key = $string . '_' . $suffix;
  164. if ($lang->hasKey($key))
  165. {
  166. $found = true;
  167. break;
  168. }
  169. }
  170. if (!$found)
  171. {
  172. // Not found so revert to the original.
  173. $key = $string;
  174. }
  175. if (is_array($args[$count - 1]))
  176. {
  177. $args[0] = $lang->_(
  178. $key, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  179. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  180. );
  181. if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
  182. {
  183. self::$strings[$key] = call_user_func_array('sprintf', $args);
  184. return $key;
  185. }
  186. }
  187. else
  188. {
  189. $args[0] = $lang->_($key);
  190. }
  191. return call_user_func_array('sprintf', $args);
  192. }
  193. elseif ($count > 0)
  194. {
  195. // Default to the normal sprintf handling.
  196. $args[0] = $lang->_($string);
  197. return call_user_func_array('sprintf', $args);
  198. }
  199. return '';
  200. }
  201. /**
  202. * Passes a string thru a sprintf.
  203. *
  204. * Note that this method can take a mixed number of arguments as for the sprintf function.
  205. *
  206. * The last argument can take an array of options:
  207. *
  208. * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
  209. *
  210. * where:
  211. *
  212. * jsSafe is a boolean to generate a javascript safe strings.
  213. * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
  214. * script is a boolean to indicate that the string will be push in the javascript language store.
  215. *
  216. * @param string $string The format string.
  217. *
  218. * @return string The translated strings or the key if 'script' is true in the array of options.
  219. *
  220. * @since 11.1
  221. */
  222. public static function sprintf($string)
  223. {
  224. $lang = JFactory::getLanguage();
  225. $args = func_get_args();
  226. $count = count($args);
  227. if ($count > 0)
  228. {
  229. if (is_array($args[$count - 1]))
  230. {
  231. $args[0] = $lang->_(
  232. $string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  233. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  234. );
  235. if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
  236. {
  237. self::$strings[$string] = call_user_func_array('sprintf', $args);
  238. return $string;
  239. }
  240. }
  241. else
  242. {
  243. $args[0] = $lang->_($string);
  244. }
  245. return call_user_func_array('sprintf', $args);
  246. }
  247. return '';
  248. }
  249. /**
  250. * Passes a string thru an printf.
  251. *
  252. * Note that this method can take a mixed number of arguments as for the sprintf function.
  253. *
  254. * @param format $string The format string.
  255. *
  256. * @return mixed
  257. *
  258. * @since 11.1
  259. */
  260. public static function printf($string)
  261. {
  262. $lang = JFactory::getLanguage();
  263. $args = func_get_args();
  264. $count = count($args);
  265. if ($count > 0)
  266. {
  267. if (is_array($args[$count - 1]))
  268. {
  269. $args[0] = $lang->_(
  270. $string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  271. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  272. );
  273. }
  274. else
  275. {
  276. $args[0] = $lang->_($string);
  277. }
  278. return call_user_func_array('printf', $args);
  279. }
  280. return '';
  281. }
  282. /**
  283. * Translate a string into the current language and stores it in the JavaScript language store.
  284. *
  285. * @param string $string The JText key.
  286. * @param boolean $jsSafe Ensure the output is JavaScript safe.
  287. * @param boolean $interpretBackSlashes Interpret \t and \n.
  288. *
  289. * @return string
  290. *
  291. * @since 11.1
  292. */
  293. public static function script($string = null, $jsSafe = false, $interpretBackSlashes = true)
  294. {
  295. if (is_array($jsSafe))
  296. {
  297. if (array_key_exists('interpretBackSlashes', $jsSafe))
  298. {
  299. $interpretBackSlashes = (boolean) $jsSafe['interpretBackSlashes'];
  300. }
  301. if (array_key_exists('jsSafe', $jsSafe))
  302. {
  303. $jsSafe = (boolean) $jsSafe['jsSafe'];
  304. }
  305. else
  306. {
  307. $jsSafe = false;
  308. }
  309. }
  310. // Add the string to the array if not null.
  311. if ($string !== null)
  312. {
  313. // Normalize the key and translate the string.
  314. self::$strings[strtoupper($string)] = JFactory::getLanguage()->_($string, $jsSafe, $interpretBackSlashes);
  315. }
  316. return self::$strings;
  317. }
  318. }