/libraries/joomla/language/text.php

https://github.com/elinw/joomla-cms · PHP · 362 lines · 191 code · 42 blank · 129 comment · 25 complexity · 2ecc5db2a237c8fed0a763ba82283644 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 = preg_replace('/\[\[%([0-9]+):[^\]]*\]\]/', '%\1$s', $str);
  81. $str = vsprintf($str, $strs);
  82. return $str;
  83. }
  84. }
  85. if ($script)
  86. {
  87. self::$strings[$string] = $lang->_($string, $jsSafe, $interpretBackSlashes);
  88. return $string;
  89. }
  90. else
  91. {
  92. return $lang->_($string, $jsSafe, $interpretBackSlashes);
  93. }
  94. }
  95. /**
  96. * Translates a string into the current language.
  97. *
  98. * Examples:
  99. * <?php echo JText::alt("JALL","language");?> it will generate a 'All' string in English but a "Toutes" string in French
  100. * <?php echo JText::alt("JALL","module");?> it will generate a 'All' string in English but a "Tous" string in French
  101. *
  102. * @param string $string The string to translate.
  103. * @param string $alt The alternate option for global string
  104. * @param mixed $jsSafe Boolean: Make the result javascript safe.
  105. * @param boolean $interpretBackSlashes To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
  106. * @param boolean $script To indicate that the string will be pushed in the javascript language store
  107. *
  108. * @return string The translated string or the key if $script is true
  109. *
  110. * @since 11.1
  111. */
  112. public static function alt($string, $alt, $jsSafe = false, $interpretBackSlashes = true, $script = false)
  113. {
  114. $lang = JFactory::getLanguage();
  115. if ($lang->hasKey($string . '_' . $alt))
  116. {
  117. return self::_($string . '_' . $alt, $jsSafe, $interpretBackSlashes, $script);
  118. }
  119. else
  120. {
  121. return self::_($string, $jsSafe, $interpretBackSlashes, $script);
  122. }
  123. }
  124. /**
  125. * Like JText::sprintf but tries to pluralise the string.
  126. *
  127. * Note that this method can take a mixed number of arguments as for the sprintf function.
  128. *
  129. * The last argument can take an array of options:
  130. *
  131. * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
  132. *
  133. * where:
  134. *
  135. * jsSafe is a boolean to generate a javascript safe strings.
  136. * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
  137. * script is a boolean to indicate that the string will be push in the javascript language store.
  138. *
  139. * Examples:
  140. * <script>alert(Joomla.JText._('<?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1, array("script"=>true));?>'));</script>
  141. * will generate an alert message containing '1 plugin successfully disabled'
  142. * <?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1);?> it will generate a '1 plugin successfully disabled' string
  143. *
  144. * @param string $string The format string.
  145. * @param integer $n The number of items
  146. *
  147. * @return string The translated strings or the key if 'script' is true in the array of options
  148. *
  149. * @since 11.1
  150. */
  151. public static function plural($string, $n)
  152. {
  153. $lang = JFactory::getLanguage();
  154. $args = func_get_args();
  155. $count = count($args);
  156. if ($count > 1)
  157. {
  158. // Try the key from the language plural potential suffixes
  159. $found = false;
  160. $suffixes = $lang->getPluralSuffixes((int) $n);
  161. array_unshift($suffixes, (int) $n);
  162. foreach ($suffixes as $suffix)
  163. {
  164. $key = $string . '_' . $suffix;
  165. if ($lang->hasKey($key))
  166. {
  167. $found = true;
  168. break;
  169. }
  170. }
  171. if (!$found)
  172. {
  173. // Not found so revert to the original.
  174. $key = $string;
  175. }
  176. if (is_array($args[$count - 1]))
  177. {
  178. $args[0] = $lang->_(
  179. $key, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  180. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  181. );
  182. if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
  183. {
  184. self::$strings[$key] = call_user_func_array('sprintf', $args);
  185. return $key;
  186. }
  187. }
  188. else
  189. {
  190. $args[0] = $lang->_($key);
  191. }
  192. return call_user_func_array('sprintf', $args);
  193. }
  194. elseif ($count > 0)
  195. {
  196. // Default to the normal sprintf handling.
  197. $args[0] = $lang->_($string);
  198. return call_user_func_array('sprintf', $args);
  199. }
  200. return '';
  201. }
  202. /**
  203. * Passes a string thru a sprintf.
  204. *
  205. * Note that this method can take a mixed number of arguments as for the sprintf function.
  206. *
  207. * The last argument can take an array of options:
  208. *
  209. * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
  210. *
  211. * where:
  212. *
  213. * jsSafe is a boolean to generate a javascript safe strings.
  214. * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
  215. * script is a boolean to indicate that the string will be push in the javascript language store.
  216. *
  217. * @param string $string The format string.
  218. *
  219. * @return string The translated strings or the key if 'script' is true in the array of options.
  220. *
  221. * @since 11.1
  222. */
  223. public static function sprintf($string)
  224. {
  225. $lang = JFactory::getLanguage();
  226. $args = func_get_args();
  227. $count = count($args);
  228. if ($count > 0)
  229. {
  230. if (is_array($args[$count - 1]))
  231. {
  232. $args[0] = $lang->_(
  233. $string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  234. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  235. );
  236. if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
  237. {
  238. self::$strings[$string] = call_user_func_array('sprintf', $args);
  239. return $string;
  240. }
  241. }
  242. else
  243. {
  244. $args[0] = $lang->_($string);
  245. }
  246. $args[0] = preg_replace('/\[\[%([0-9]+):[^\]]*\]\]/', '%\1$s', $args[0]);
  247. return call_user_func_array('sprintf', $args);
  248. }
  249. return '';
  250. }
  251. /**
  252. * Passes a string thru an printf.
  253. *
  254. * Note that this method can take a mixed number of arguments as for the sprintf function.
  255. *
  256. * @param format $string The format string.
  257. *
  258. * @return mixed
  259. *
  260. * @since 11.1
  261. */
  262. public static function printf($string)
  263. {
  264. $lang = JFactory::getLanguage();
  265. $args = func_get_args();
  266. $count = count($args);
  267. if ($count > 0)
  268. {
  269. if (is_array($args[$count - 1]))
  270. {
  271. $args[0] = $lang->_(
  272. $string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
  273. array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
  274. );
  275. }
  276. else
  277. {
  278. $args[0] = $lang->_($string);
  279. }
  280. return call_user_func_array('printf', $args);
  281. }
  282. return '';
  283. }
  284. /**
  285. * Translate a string into the current language and stores it in the JavaScript language store.
  286. *
  287. * @param string $string The JText key.
  288. * @param boolean $jsSafe Ensure the output is JavaScript safe.
  289. * @param boolean $interpretBackSlashes Interpret \t and \n.
  290. *
  291. * @return string
  292. *
  293. * @since 11.1
  294. */
  295. public static function script($string = null, $jsSafe = false, $interpretBackSlashes = true)
  296. {
  297. if (is_array($jsSafe))
  298. {
  299. if (array_key_exists('interpretBackSlashes', $jsSafe))
  300. {
  301. $interpretBackSlashes = (boolean) $jsSafe['interpretBackSlashes'];
  302. }
  303. if (array_key_exists('jsSafe', $jsSafe))
  304. {
  305. $jsSafe = (boolean) $jsSafe['jsSafe'];
  306. }
  307. else
  308. {
  309. $jsSafe = false;
  310. }
  311. }
  312. // Add the string to the array if not null.
  313. if ($string !== null)
  314. {
  315. // Normalize the key and translate the string.
  316. self::$strings[strtoupper($string)] = JFactory::getLanguage()->_($string, $jsSafe, $interpretBackSlashes);
  317. }
  318. return self::$strings;
  319. }
  320. }