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