]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-l10n.php
Fix end of line
[Github/YOURLS.git] / includes / functions-l10n.php
1 <?php
2 /**
3  * YOURLS Translation API
4  *
5  * YOURLS modification of a small subset from WordPress' Translation API implementation.
6  * GPL License
7  *
8  * @package POMO
9  * @subpackage i18n
10  */
11
12 /**
13  * Load POMO files required to run library
14  */
15 require_once dirname(__FILE__) . '/pomo/mo.php';
16 require_once dirname(__FILE__) . '/pomo/translations.php';
17
18 /**
19  * Gets the current locale.
20  *
21  * If the locale is set, then it will filter the locale in the 'get_locale' filter
22  * hook and return the value.
23  *
24  * If the locale is not set already, then the YOURLS_LANG constant is used if it is
25  * defined. Then it is filtered through the 'get_locale' filter hook and the value
26  * for the locale global set and the locale is returned.
27  *
28  * The process to get the locale should only be done once, but the locale will
29  * always be filtered using the 'get_locale' hook.
30  *
31  * @since 1.6
32  * @uses yourls_apply_filters() Calls 'get_locale' hook on locale value.
33  * @uses $yourls_locale Gets the locale stored in the global.
34  *
35  * @return string The locale of the blog or from the 'get_locale' hook.
36  */
37 function yourls_get_locale() {
38         global $yourls_locale;
39
40         if ( !isset( $yourls_locale ) ) {
41                 // YOURLS_LANG is defined in config.
42                 if ( defined( 'YOURLS_LANG' ) )
43                         $yourls_locale = YOURLS_LANG;
44
45                 if ( empty( $yourls_locale ) )
46                         $yourls_locale = 'en_US';
47         }
48         return yourls_apply_filters( 'get_locale', $yourls_locale );
49 }
50
51 /**
52  * Retrieves the translation of $text. If there is no translation, or
53  * the domain isn't loaded, the original text is returned.
54  *
55  * @see yourls__() Don't use yourls_translate() directly, use yourls__()
56  * @since 1.6
57  * @uses yourls_apply_filters() Calls 'translate' on domain translated text
58  *              with the untranslated text as second parameter.
59  *
60  * @param string $text Text to translate.
61  * @param string $domain Domain to retrieve the translated text.
62  * @return string Translated text
63  */
64 function yourls_translate( $text, $domain = 'default' ) {
65         $translations = yourls_get_translations_for_domain( $domain );
66         return yourls_apply_filters( 'translate', $translations->translate( $text ), $text, $domain );
67 }
68
69 /**
70  * Retrieves the translation of $text with a given $context. If there is no translation, or
71  * the domain isn't loaded, the original text is returned.
72  *
73  * Quite a few times, there will be collisions with similar translatable text
74  * found in more than two places but with different translated context.
75  *
76  * By including the context in the pot file translators can translate the two
77  * strings differently.
78  *
79  * @since 1.6
80  * @param string $text Text to translate.
81  * @param string $context Context.
82  * @param string $domain Domain to retrieve the translated text.
83  * @return string Translated text
84  */
85 function yourls_translate_with_context( $text, $context, $domain = 'default' ) {
86         $translations = yourls_get_translations_for_domain( $domain );
87         return yourls_apply_filters( 'translate_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
88 }
89
90 /**
91  * Retrieves the translation of $text. If there is no translation, or
92  * the domain isn't loaded, the original text is returned.
93  *
94  * @see yourls_translate() An alias of yourls_translate()
95  * @since 1.6
96  *
97  * @param string $text Text to translate
98  * @param string $domain Optional. Domain to retrieve the translated text
99  * @return string Translated text
100  */
101 function yourls__( $text, $domain = 'default' ) {
102         return yourls_translate( $text, $domain );
103 }
104
105 /**
106  * Return a translated sprintf() string (mix yourls__() and sprintf() in one func)
107  *
108  * Instead of doing sprintf( yourls__( 'string %s' ), $arg ) you can simply use:
109  * yourls_s( 'string %s', $arg )
110  * This function accepts an arbitrary number of arguments:
111  * - first one will be the string to translate, eg "hello %s my name is %s"
112  * - following ones will be the sprintf arguments, eg "world" and "Ozh"
113  * - if there are more arguments passed than needed, the last one will be used as the translation domain
114  * This function will not accept a textdomain argument: do not use in plugins or outside YOURLS core.
115  *
116  * @see sprintf()
117  * @since 1.6
118  *
119  * @param string $text Text to translate
120  * @param string $arg1, $arg2... Optional: sprintf tokens, and translation domain
121  * @return string Translated text
122  */
123 function yourls_s( $pattern ) {
124         // Get pattern and pattern arguments 
125         $args = func_get_args();
126         // If yourls_s() called by yourls_se(), all arguments are wrapped in the same array key
127         if( count( $args ) == 1 && is_array( $args ) ) {
128                 $args = $args[0];
129         }
130         $pattern = $args[0];
131         
132         // get list of sprintf tokens (%s and such)
133         $num_of_tokens = substr_count( $pattern, '%' ) - 2 * substr_count( $pattern, '%%' );
134         
135         $domain = 'default';
136         // More arguments passed than needed for the sprintf? The last one will be the domain
137         if( $num_of_tokens < ( count( $args ) - 1 ) ) {
138                 $domain = array_pop( $args );
139         }
140         
141         // Translate text
142         $args[0] = yourls__( $pattern, $domain );
143         
144         return call_user_func_array( 'sprintf', $args );        
145 }
146
147 /**
148  * Echo a translated sprintf() string (mix yourls__() and sprintf() in one func)
149  *
150  * Instead of doing printf( yourls__( 'string %s' ), $arg ) you can simply use:
151  * yourls_se( 'string %s', $arg )
152  * This function accepts an arbitrary number of arguments:
153  * - first one will be the string to translate, eg "hello %s my name is %s"
154  * - following ones will be the sprintf arguments, eg "world" and "Ozh"
155  * - if there are more arguments passed than needed, the last one will be used as the translation domain
156  *
157  * @see yourls_s()
158  * @see sprintf()
159  * @since 1.6
160  *
161  * @param string $text Text to translate
162  * @param string $arg1, $arg2... Optional: sprintf tokens, and translation domain
163  * @return string Translated text
164  */
165 function yourls_se( $pattern ) {
166         echo yourls_s( func_get_args() );
167 }
168
169
170 /**
171  * Retrieves the translation of $text and escapes it for safe use in an attribute.
172  * If there is no translation, or the domain isn't loaded, the original text is returned.
173  *
174  * @see yourls_translate() An alias of yourls_translate()
175  * @see yourls_esc_attr()
176  * @since 1.6
177  *
178  * @param string $text Text to translate
179  * @param string $domain Optional. Domain to retrieve the translated text
180  * @return string Translated text
181  */
182 function yourls_esc_attr__( $text, $domain = 'default' ) {
183         return yourls_esc_attr( yourls_translate( $text, $domain ) );
184 }
185
186 /**
187  * Retrieves the translation of $text and escapes it for safe use in HTML output.
188  * If there is no translation, or the domain isn't loaded, the original text is returned.
189  *
190  * @see yourls_translate() An alias of yourls_translate()
191  * @see yourls_esc_html()
192  * @since 1.6
193  *
194  * @param string $text Text to translate
195  * @param string $domain Optional. Domain to retrieve the translated text
196  * @return string Translated text
197  */
198 function yourls_esc_html__( $text, $domain = 'default' ) {
199         return yourls_esc_html( yourls_translate( $text, $domain ) );
200 }
201
202 /**
203  * Displays the returned translated text from yourls_translate().
204  *
205  * @see yourls_translate() Echoes returned yourls_translate() string
206  * @since 1.6
207  *
208  * @param string $text Text to translate
209  * @param string $domain Optional. Domain to retrieve the translated text
210  */
211 function yourls_e( $text, $domain = 'default' ) {
212         echo yourls_translate( $text, $domain );
213 }
214
215 /**
216  * Displays translated text that has been escaped for safe use in an attribute.
217  *
218  * @see yourls_translate() Echoes returned yourls_translate() string
219  * @see yourls_esc_attr()
220  * @since 1.6
221  *
222  * @param string $text Text to translate
223  * @param string $domain Optional. Domain to retrieve the translated text
224  */
225 function yourls_esc_attr_e( $text, $domain = 'default' ) {
226         echo yourls_esc_attr( yourls_translate( $text, $domain ) );
227 }
228
229 /**
230  * Displays translated text that has been escaped for safe use in HTML output.
231  *
232  * @see yourls_translate() Echoes returned yourls_translate() string
233  * @see yourls_esc_html()
234  * @since 1.6
235  *
236  * @param string $text Text to translate
237  * @param string $domain Optional. Domain to retrieve the translated text
238  */
239 function yourls_esc_html_e( $text, $domain = 'default' ) {
240         echo yourls_esc_html( yourls_translate( $text, $domain ) );
241 }
242
243 /**
244  * Retrieve translated string with gettext context
245  *
246  * Quite a few times, there will be collisions with similar translatable text
247  * found in more than two places but with different translated context.
248  *
249  * By including the context in the pot file translators can translate the two
250  * strings differently.
251  *
252  * @since 1.6
253  *
254  * @param string $text Text to translate
255  * @param string $context Context information for the translators
256  * @param string $domain Optional. Domain to retrieve the translated text
257  * @return string Translated context string without pipe
258  */
259 function yourls_x( $text, $context, $domain = 'default' ) {
260         return yourls_translate_with_context( $text, $context, $domain );
261 }
262
263 /**
264  * Displays translated string with gettext context
265  *
266  * @see yourls_x()
267  * @since 1.6
268  *
269  * @param string $text Text to translate
270  * @param string $context Context information for the translators
271  * @param string $domain Optional. Domain to retrieve the translated text
272  * @return string Translated context string without pipe
273  */
274 function yourls_ex( $text, $context, $domain = 'default' ) {
275         echo yourls_x( $text, $context, $domain );
276 }
277
278
279 /**
280  * Return translated text, with context, that has been escaped for safe use in an attribute
281  *
282  * @see yourls_translate() Return returned yourls_translate() string
283  * @see yourls_esc_attr()
284  * @see yourls_x()
285  * @since 1.6
286  *
287  * @param string $text Text to translate
288  * @param string $domain Optional. Domain to retrieve the translated text
289  */
290 function yourls_esc_attr_x( $single, $context, $domain = 'default' ) {
291         return yourls_esc_attr( yourls_translate_with_context( $single, $context, $domain ) );
292 }
293
294 /**
295  * Return translated text, with context, that has been escaped for safe use in HTML output
296  *
297  * @see yourls_translate() Return returned yourls_translate() string
298  * @see yourls_esc_attr()
299  * @see yourls_x()
300  * @since 1.6
301  *
302  * @param string $text Text to translate
303  * @param string $domain Optional. Domain to retrieve the translated text
304  */
305 function yourls_esc_html_x( $single, $context, $domain = 'default' ) {
306         return yourls_esc_html( yourls_translate_with_context( $single, $context, $domain ) );
307 }
308
309 /**
310  * Retrieve the plural or single form based on the amount.
311  *
312  * If the domain is not set in the $yourls_l10n list, then a comparison will be made
313  * and either $plural or $single parameters returned.
314  *
315  * If the domain does exist, then the parameters $single, $plural, and $number
316  * will first be passed to the domain's ngettext method. Then it will be passed
317  * to the 'translate_n' filter hook along with the same parameters. The expected
318  * type will be a string.
319  *
320  * @since 1.6
321  * @uses $yourls_l10n Gets list of domain translated string (gettext_reader) objects
322  * @uses yourls_apply_filters() Calls 'translate_n' hook on domains text returned,
323  *              along with $single, $plural, and $number parameters. Expected to return string.
324  *
325  * @param string $single The text that will be used if $number is 1
326  * @param string $plural The text that will be used if $number is not 1
327  * @param int $number The number to compare against to use either $single or $plural
328  * @param string $domain Optional. The domain identifier the text should be retrieved in
329  * @return string Either $single or $plural translated text
330  */
331 function yourls_n( $single, $plural, $number, $domain = 'default' ) {
332         $translations = yourls_get_translations_for_domain( $domain );
333         $translation = $translations->translate_plural( $single, $plural, $number );
334         return yourls_apply_filters( 'translate_n', $translation, $single, $plural, $number, $domain );
335 }
336
337 /**
338  * A hybrid of yourls_n() and yourls_x(). It supports contexts and plurals.
339  *
340  * @since 1.6
341  * @see yourls_n()
342  * @see yourls_x()
343  *
344  */
345 function yourls_nx($single, $plural, $number, $context, $domain = 'default') {
346         $translations = yourls_get_translations_for_domain( $domain );
347         $translation = $translations->translate_plural( $single, $plural, $number, $context );
348         return yourls_apply_filters( 'translate_nx', $translation, $single, $plural, $number, $context, $domain );
349 }
350
351 /**
352  * Register plural strings in POT file, but don't translate them.
353  *
354  * Used when you want to keep structures with translatable plural strings and
355  * use them later.
356  *
357  * Example:
358  *  $messages = array(
359  *      'post' => yourls_n_noop('%s post', '%s posts'),
360  *      'page' => yourls_n_noop('%s pages', '%s pages')
361  *  );
362  *  ...
363  *  $message = $messages[$type];
364  *  $usable_text = sprintf( yourls_translate_nooped_plural( $message, $count ), $count );
365  *
366  * @since 1.6
367  * @param string $singular Single form to be i18ned
368  * @param string $plural Plural form to be i18ned
369  * @param string $domain Optional. The domain identifier the text will be retrieved in
370  * @return array array($singular, $plural)
371  */
372 function yourls_n_noop( $singular, $plural, $domain = null ) {
373         return array(
374                 0 => $singular,
375                 1 => $plural, 
376                 'singular' => $singular,
377                 'plural' => $plural,
378                 'context' => null,
379                 'domain' => $domain
380         );
381 }
382
383 /**
384  * Register plural strings with context in POT file, but don't translate them.
385  *
386  * @since 1.6
387  * @see yourls_n_noop()
388  */
389 function yourls_nx_noop( $singular, $plural, $context, $domain = null ) {
390         return array(
391                 0 => $singular,
392                 1 => $plural,
393                 2 => $context,
394                 'singular' => $singular,
395                 'plural' => $plural,
396                 'context' => $context,
397                 'domain' => $domain
398         );
399 }
400
401 /**
402  * Translate the result of yourls_n_noop() or yourls_nx_noop()
403  *
404  * @since 1.6
405  * @param array $nooped_plural Array with singular, plural and context keys, usually the result of yourls_n_noop() or yourls_nx_noop()
406  * @param int $count Number of objects
407  * @param string $domain Optional. The domain identifier the text should be retrieved in. If $nooped_plural contains
408  *      a domain passed to yourls_n_noop() or yourls_nx_noop(), it will override this value.
409  */
410 function yourls_translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
411         if ( $nooped_plural['domain'] )
412                 $domain = $nooped_plural['domain'];
413
414         if ( $nooped_plural['context'] )
415                 return yourls_nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
416         else
417                 return yourls_n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
418 }
419
420 /**
421  * Loads a MO file into the domain $domain.
422  *
423  * If the domain already exists, the translations will be merged. If both
424  * sets have the same string, the translation from the original value will be taken.
425  *
426  * On success, the .mo file will be placed in the $yourls_l10n global by $domain
427  * and will be a MO object.
428  *
429  * @since 1.6
430  * @uses $yourls_l10n Gets list of domain translated string objects
431  *
432  * @param string $domain Unique identifier for retrieving translated strings
433  * @param string $mofile Path to the .mo file
434  * @return bool True on success, false on failure
435  */
436 function yourls_load_textdomain( $domain, $mofile ) {
437         global $yourls_l10n;
438
439         $plugin_override = yourls_apply_filters( 'override_load_textdomain', false, $domain, $mofile );
440
441         if ( true == $plugin_override ) {
442                 return true;
443         }
444
445         yourls_do_action( 'load_textdomain', $domain, $mofile );
446
447         $mofile = yourls_apply_filters( 'load_textdomain_mofile', $mofile, $domain );
448
449         if ( !is_readable( $mofile ) ) return false;
450
451         $mo = new MO();
452         if ( !$mo->import_from_file( $mofile ) ) return false;
453
454         if ( isset( $yourls_l10n[$domain] ) )
455                 $mo->merge_with( $yourls_l10n[$domain] );
456
457         $yourls_l10n[$domain] = &$mo;
458
459         return true;
460 }
461
462 /**
463  * Unloads translations for a domain
464  *
465  * @since 1.6
466  * @param string $domain Textdomain to be unloaded
467  * @return bool Whether textdomain was unloaded
468  */
469 function yourls_unload_textdomain( $domain ) {
470         global $yourls_l10n;
471
472         $plugin_override = yourls_apply_filters( 'override_unload_textdomain', false, $domain );
473
474         if ( $plugin_override )
475                 return true;
476
477         yourls_do_action( 'unload_textdomain', $domain );
478
479         if ( isset( $yourls_l10n[$domain] ) ) {
480                 unset( $yourls_l10n[$domain] );
481                 return true;
482         }
483
484         return false;
485 }
486
487 /**
488  * Loads default translated strings based on locale.
489  *
490  * Loads the .mo file in YOURLS_LANG_DIR constant path from YOURLS root. The
491  * translated (.mo) file is named based on the locale.
492  *
493  * @since 1.6
494  */
495 function yourls_load_default_textdomain() {
496         $yourls_locale = yourls_get_locale();
497
498         yourls_load_textdomain( 'default', YOURLS_LANG_DIR . "/$yourls_locale.mo" );
499
500 }
501
502 /**
503  * Returns the Translations instance for a domain. If there isn't one,
504  * returns empty Translations instance.
505  *
506  * @param string $domain
507  * @return object A Translation instance
508  */
509 function yourls_get_translations_for_domain( $domain ) {
510         global $yourls_l10n;
511         if ( !isset( $yourls_l10n[$domain] ) ) {
512                 $yourls_l10n[$domain] = new NOOP_Translations;
513         }
514         return $yourls_l10n[$domain];
515 }
516
517 /**
518  * Whether there are translations for the domain
519  *
520  * @since 1.6
521  * @param string $domain
522  * @return bool Whether there are translations
523  */
524 function yourls_is_textdomain_loaded( $domain ) {
525         global $yourls_l10n;
526         return isset( $yourls_l10n[$domain] );
527 }
528
529 /**
530  * Translates role name. Unused.
531  *
532  * Unused function for the moment, we'll see when there are roles.
533  * From the WP source: Since the role names are in the database and
534  * not in the source there are dummy gettext calls to get them into the POT
535  * file and this function properly translates them back.
536  *
537  * @since 1.6
538  */
539 function yourls_translate_user_role( $name ) {
540         return yourls_translate_with_context( $name, 'User role' );
541 }
542
543 /**
544  * Get all available languages (*.mo files) in a given directory. The default directory is YOURLS_LANG_DIR.
545  *
546  * @since 1.6
547  *
548  * @param string $dir A directory in which to search for language files. The default directory is YOURLS_LANG_DIR.
549  * @return array Array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names.
550  */
551 function yourls_get_available_languages( $dir = null ) {
552         $languages = array();
553         
554         $dir = is_null( $dir) ? YOURLS_LANG_DIR : $dir;
555         
556         foreach( (array) glob( $dir . '/*.mo' ) as $lang_file ) {
557                 $languages[] = basename( $lang_file, '.mo' );
558         }
559         
560         return yourls_apply_filters( 'get_available_languages', $languages );
561 }
562
563 /**
564  * Return integer number to format based on the locale.
565  *
566  * @since 1.6
567  *
568  * @param int $number The number to convert based on locale.
569  * @param int $decimals Precision of the number of decimal places.
570  * @return string Converted number in string format.
571  */
572 function yourls_number_format_i18n( $number, $decimals = 0 ) {
573     global $yourls_locale_formats;
574         if( !isset( $yourls_locale_formats ) )
575                 $yourls_locale_formats = new YOURLS_Locale_Formats();
576                 
577     $formatted = number_format( $number, abs( intval( $decimals ) ), $yourls_locale_formats->number_format['decimal_point'], $yourls_locale_formats->number_format['thousands_sep'] );
578     return yourls_apply_filters( 'number_format_i18n', $formatted );
579 }
580
581 /**
582  * Return the date in localized format, based on timestamp.
583  *
584  * If the locale specifies the locale month and weekday, then the locale will
585  * take over the format for the date. If it isn't, then the date format string
586  * will be used instead.
587  *
588  * @since 1.6
589  *
590  * @param string $dateformatstring Format to display the date.
591  * @param int $unixtimestamp Optional. Unix timestamp.
592  * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
593  * @return string The date, translated if locale specifies it.
594  */
595 function yourls_date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
596     global $yourls_locale_formats;
597         if( !isset( $yourls_locale_formats ) )
598                 $yourls_locale_formats = new YOURLS_Locale_Formats();
599
600         $i = $unixtimestamp;
601
602     if ( false === $i ) {
603         if ( ! $gmt )
604             $i = yourls_current_time( 'timestamp' );
605         else
606             $i = time();
607         // we should not let date() interfere with our
608         // specially computed timestamp
609         $gmt = true;
610     }
611
612     // store original value for language with untypical grammars
613     // see http://core.trac.wordpress.org/ticket/9396
614     $req_format = $dateformatstring;
615
616     $datefunc = $gmt? 'gmdate' : 'date';
617
618     if ( ( !empty( $yourls_locale_formats->month ) ) && ( !empty( $yourls_locale_formats->weekday ) ) ) {
619         $datemonth            = $yourls_locale_formats->get_month( $datefunc( 'm', $i ) );
620         $datemonth_abbrev     = $yourls_locale_formats->get_month_abbrev( $datemonth );
621         $dateweekday          = $yourls_locale_formats->get_weekday( $datefunc( 'w', $i ) );
622         $dateweekday_abbrev   = $yourls_locale_formats->get_weekday_abbrev( $dateweekday );
623         $datemeridiem         = $yourls_locale_formats->get_meridiem( $datefunc( 'a', $i ) );
624         $datemeridiem_capital = $yourls_locale_formats->get_meridiem( $datefunc( 'A', $i ) );
625                 
626         $dateformatstring = ' '.$dateformatstring;
627         $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . yourls_backslashit( $dateweekday_abbrev ), $dateformatstring );
628         $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . yourls_backslashit( $datemonth ), $dateformatstring );
629         $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . yourls_backslashit( $dateweekday ), $dateformatstring );
630         $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . yourls_backslashit( $datemonth_abbrev ), $dateformatstring );
631         $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . yourls_backslashit( $datemeridiem ), $dateformatstring );
632         $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . yourls_backslashit( $datemeridiem_capital ), $dateformatstring );
633
634         $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
635     }
636     $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
637     $timezone_formats_re = implode( '|', $timezone_formats );
638     if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
639         
640                 // TODO: implement a timezone option
641         $timezone_string = yourls_get_option( 'timezone_string' );
642         if ( $timezone_string ) {
643             $timezone_object = timezone_open( $timezone_string );
644             $date_object = date_create( null, $timezone_object );
645             foreach( $timezone_formats as $timezone_format ) {
646                 if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
647                     $formatted = date_format( $date_object, $timezone_format );
648                     $dateformatstring = ' '.$dateformatstring;
649                     $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . yourls_backslashit( $formatted ), $dateformatstring );
650                     $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
651                 }
652             }
653         }
654     }
655     $j = @$datefunc( $dateformatstring, $i );
656     // allow plugins to redo this entirely for languages with untypical grammars
657     $j = yourls_apply_filters('date_i18n', $j, $req_format, $i, $gmt);
658     return $j;
659 }
660
661 /**
662  * Retrieve the current time based on specified type. Stolen from WP.
663  *
664  * The 'mysql' type will return the time in the format for MySQL DATETIME field.
665  * The 'timestamp' type will return the current timestamp.
666  *
667  * If $gmt is set to either '1' or 'true', then both types will use GMT time.
668  * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
669  *
670  * @since 1.6
671  *
672  * @param string $type Either 'mysql' or 'timestamp'.
673  * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
674  * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
675  */
676 function yourls_current_time( $type, $gmt = 0 ) {
677     switch ( $type ) {
678         case 'mysql':
679             return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', time() + YOURLS_HOURS_OFFSET * 3600 );
680             break;
681         case 'timestamp':
682             return ( $gmt ) ? time() : time() + YOURLS_HOURS_OFFSET * 3600;
683             break;
684     }
685 }
686
687
688 /**
689  * Class that loads the calendar locale.
690  *
691  * @since 1.6
692  */
693 class YOURLS_Locale_Formats {
694         /**
695          * Stores the translated strings for the full weekday names.
696          *
697          * @since 1.6
698          * @var array
699          * @access private
700          */
701         var $weekday;
702
703         /**
704          * Stores the translated strings for the one character weekday names.
705          *
706          * There is a hack to make sure that Tuesday and Thursday, as well
707          * as Sunday and Saturday, don't conflict. See init() method for more.
708          *
709          * @see YOURLS_Locale_Formats::init() for how to handle the hack.
710          *
711          * @since 1.6
712          * @var array
713          * @access private
714          */
715         var $weekday_initial;
716
717         /**
718          * Stores the translated strings for the abbreviated weekday names.
719          *
720          * @since 1.6
721          * @var array
722          * @access private
723          */
724         var $weekday_abbrev;
725
726         /**
727          * Stores the translated strings for the full month names.
728          *
729          * @since 1.6
730          * @var array
731          * @access private
732          */
733         var $month;
734
735         /**
736          * Stores the translated strings for the abbreviated month names.
737          *
738          * @since 1.6
739          * @var array
740          * @access private
741          */
742         var $month_abbrev;
743
744         /**
745          * Stores the translated strings for 'am' and 'pm'.
746          *
747          * Also the capitalized versions.
748          *
749          * @since 1.6
750          * @var array
751          * @access private
752          */
753         var $meridiem;
754
755         /**
756          * The text direction of the locale language.
757          *
758          * Default is left to right 'ltr'.
759          *
760          * @since 1.6
761          * @var string
762          * @access private
763          */
764         var $text_direction = 'ltr';
765
766         /**
767          * Sets up the translated strings and object properties.
768          *
769          * The method creates the translatable strings for various
770          * calendar elements. Which allows for specifying locale
771          * specific calendar names and text direction.
772          *
773          * @since 1.6
774          * @access private
775          */
776         function init() {
777                 // The Weekdays
778                 $this->weekday[0] = /* //translators: weekday */ yourls__( 'Sunday' );
779                 $this->weekday[1] = /* //translators: weekday */ yourls__( 'Monday' );
780                 $this->weekday[2] = /* //translators: weekday */ yourls__( 'Tuesday' );
781                 $this->weekday[3] = /* //translators: weekday */ yourls__( 'Wednesday' );
782                 $this->weekday[4] = /* //translators: weekday */ yourls__( 'Thursday' );
783                 $this->weekday[5] = /* //translators: weekday */ yourls__( 'Friday' );
784                 $this->weekday[6] = /* //translators: weekday */ yourls__( 'Saturday' );
785
786                 // The first letter of each day. The _%day%_initial suffix is a hack to make
787                 // sure the day initials are unique.
788                 $this->weekday_initial[yourls__( 'Sunday' )]    = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'S_Sunday_initial' );
789                 $this->weekday_initial[yourls__( 'Monday' )]    = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'M_Monday_initial' );
790                 $this->weekday_initial[yourls__( 'Tuesday' )]   = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'T_Tuesday_initial' );
791                 $this->weekday_initial[yourls__( 'Wednesday' )] = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'W_Wednesday_initial' );
792                 $this->weekday_initial[yourls__( 'Thursday' )]  = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'T_Thursday_initial' );
793                 $this->weekday_initial[yourls__( 'Friday' )]    = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'F_Friday_initial' );
794                 $this->weekday_initial[yourls__( 'Saturday' )]  = /* //translators: one-letter abbreviation of the weekday */ yourls__( 'S_Saturday_initial' );
795
796                 foreach ($this->weekday_initial as $weekday_ => $weekday_initial_) {
797                         $this->weekday_initial[$weekday_] = preg_replace('/_.+_initial$/', '', $weekday_initial_);
798                 }
799
800                 // Abbreviations for each day.
801                 $this->weekday_abbrev[ yourls__( 'Sunday' ) ]    = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Sun' );
802                 $this->weekday_abbrev[ yourls__( 'Monday' ) ]    = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Mon' );
803                 $this->weekday_abbrev[ yourls__( 'Tuesday' ) ]   = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Tue' );
804                 $this->weekday_abbrev[ yourls__( 'Wednesday' ) ] = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Wed' );
805                 $this->weekday_abbrev[ yourls__( 'Thursday' ) ]  = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Thu' );
806                 $this->weekday_abbrev[ yourls__( 'Friday' ) ]    = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Fri' );
807                 $this->weekday_abbrev[ yourls__( 'Saturday' ) ]  = /* //translators: three-letter abbreviation of the weekday */ yourls__( 'Sat' );
808
809                 // The Months
810                 $this->month['01'] = /* //translators: month name */ yourls__( 'January' );
811                 $this->month['02'] = /* //translators: month name */ yourls__( 'February' );
812                 $this->month['03'] = /* //translators: month name */ yourls__( 'March' );
813                 $this->month['04'] = /* //translators: month name */ yourls__( 'April' );
814                 $this->month['05'] = /* //translators: month name */ yourls__( 'May' );
815                 $this->month['06'] = /* //translators: month name */ yourls__( 'June' );
816                 $this->month['07'] = /* //translators: month name */ yourls__( 'July' );
817                 $this->month['08'] = /* //translators: month name */ yourls__( 'August' );
818                 $this->month['09'] = /* //translators: month name */ yourls__( 'September' );
819                 $this->month['10'] = /* //translators: month name */ yourls__( 'October' );
820                 $this->month['11'] = /* //translators: month name */ yourls__( 'November' );
821                 $this->month['12'] = /* //translators: month name */ yourls__( 'December' );
822
823                 // Abbreviations for each month. Uses the same hack as above to get around the
824                 // 'May' duplication.
825                 $this->month_abbrev[ yourls__( 'January' ) ]   = /* //translators: three-letter abbreviation of the month */ yourls__( 'Jan_January_abbreviation' );
826                 $this->month_abbrev[ yourls__( 'February' ) ]  = /* //translators: three-letter abbreviation of the month */ yourls__( 'Feb_February_abbreviation' );
827                 $this->month_abbrev[ yourls__( 'March' ) ]     = /* //translators: three-letter abbreviation of the month */ yourls__( 'Mar_March_abbreviation' );
828                 $this->month_abbrev[ yourls__( 'April' ) ]     = /* //translators: three-letter abbreviation of the month */ yourls__( 'Apr_April_abbreviation' );
829                 $this->month_abbrev[ yourls__( 'May' ) ]       = /* //translators: three-letter abbreviation of the month */ yourls__( 'May_May_abbreviation' );
830                 $this->month_abbrev[ yourls__( 'June' ) ]      = /* //translators: three-letter abbreviation of the month */ yourls__( 'Jun_June_abbreviation' );
831                 $this->month_abbrev[ yourls__( 'July' ) ]      = /* //translators: three-letter abbreviation of the month */ yourls__( 'Jul_July_abbreviation' );
832                 $this->month_abbrev[ yourls__( 'August' ) ]    = /* //translators: three-letter abbreviation of the month */ yourls__( 'Aug_August_abbreviation' );
833                 $this->month_abbrev[ yourls__( 'September' ) ] = /* //translators: three-letter abbreviation of the month */ yourls__( 'Sep_September_abbreviation' );
834                 $this->month_abbrev[ yourls__( 'October' ) ]   = /* //translators: three-letter abbreviation of the month */ yourls__( 'Oct_October_abbreviation' );
835                 $this->month_abbrev[ yourls__( 'November' ) ]  = /* //translators: three-letter abbreviation of the month */ yourls__( 'Nov_November_abbreviation' );
836                 $this->month_abbrev[ yourls__( 'December' ) ]  = /* //translators: three-letter abbreviation of the month */ yourls__( 'Dec_December_abbreviation' );
837
838                 foreach ($this->month_abbrev as $month_ => $month_abbrev_) {
839                         $this->month_abbrev[$month_] = preg_replace('/_.+_abbreviation$/', '', $month_abbrev_);
840                 }
841
842                 // The Meridiems
843                 $this->meridiem['am'] = yourls__( 'am' );
844                 $this->meridiem['pm'] = yourls__( 'pm' );
845                 $this->meridiem['AM'] = yourls__( 'AM' );
846                 $this->meridiem['PM'] = yourls__( 'PM' );
847
848                 // Numbers formatting
849                 // See http://php.net/number_format
850
851                 /* //translators: $thousands_sep argument for http://php.net/number_format, default is , */
852                 $trans = yourls__( 'number_format_thousands_sep' );
853                 $this->number_format['thousands_sep'] = ('number_format_thousands_sep' == $trans) ? ',' : $trans;
854
855                 /* //translators: $dec_point argument for http://php.net/number_format, default is . */
856                 $trans = yourls__( 'number_format_decimal_point' );
857                 $this->number_format['decimal_point'] = ('number_format_decimal_point' == $trans) ? '.' : $trans;
858
859                 // Set text direction.
860                 if ( isset( $GLOBALS['text_direction'] ) )
861                         $this->text_direction = $GLOBALS['text_direction'];
862                 /* //translators: 'rtl' or 'ltr'. This sets the text direction for YOURLS. */
863                 elseif ( 'rtl' == yourls_x( 'ltr', 'text direction' ) )
864                         $this->text_direction = 'rtl';
865         }
866
867         /**
868          * Retrieve the full translated weekday word.
869          *
870          * Week starts on translated Sunday and can be fetched
871          * by using 0 (zero). So the week starts with 0 (zero)
872          * and ends on Saturday with is fetched by using 6 (six).
873          *
874          * @since 1.6
875          * @access public
876          *
877          * @param int $weekday_number 0 for Sunday through 6 Saturday
878          * @return string Full translated weekday
879          */
880         function get_weekday( $weekday_number ) {
881                 return $this->weekday[ $weekday_number ];
882         }
883
884         /**
885          * Retrieve the translated weekday initial.
886          *
887          * The weekday initial is retrieved by the translated
888          * full weekday word. When translating the weekday initial
889          * pay attention to make sure that the starting letter does
890          * not conflict.
891          *
892          * @since 1.6
893          * @access public
894          *
895          * @param string $weekday_name
896          * @return string
897          */
898         function get_weekday_initial( $weekday_name ) {
899                 return $this->weekday_initial[ $weekday_name ];
900         }
901
902         /**
903          * Retrieve the translated weekday abbreviation.
904          *
905          * The weekday abbreviation is retrieved by the translated
906          * full weekday word.
907          *
908          * @since 1.6
909          * @access public
910          *
911          * @param string $weekday_name Full translated weekday word
912          * @return string Translated weekday abbreviation
913          */
914         function get_weekday_abbrev( $weekday_name ) {
915                 return $this->weekday_abbrev[ $weekday_name ];
916         }
917
918         /**
919          * Retrieve the full translated month by month number.
920          *
921          * The $month_number parameter has to be a string
922          * because it must have the '0' in front of any number
923          * that is less than 10. Starts from '01' and ends at
924          * '12'.
925          *
926          * You can use an integer instead and it will add the
927          * '0' before the numbers less than 10 for you.
928          *
929          * @since 1.6
930          * @access public
931          *
932          * @param string|int $month_number '01' through '12'
933          * @return string Translated full month name
934          */
935         function get_month( $month_number ) {
936                 return $this->month[ sprintf( '%02s', $month_number ) ];                
937         }
938
939         /**
940          * Retrieve translated version of month abbreviation string.
941          *
942          * The $month_name parameter is expected to be the translated or
943          * translatable version of the month.
944          *
945          * @since 1.6
946          * @access public
947          *
948          * @param string $month_name Translated month to get abbreviated version
949          * @return string Translated abbreviated month
950          */
951         function get_month_abbrev( $month_name ) {
952                 return $this->month_abbrev[ $month_name ];
953         }
954
955         /**
956          * Retrieve translated version of meridiem string.
957          *
958          * The $meridiem parameter is expected to not be translated.
959          *
960          * @since 1.6
961          * @access public
962          *
963          * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
964          * @return string Translated version
965          */
966         function get_meridiem( $meridiem ) {
967                 return $this->meridiem[ $meridiem ];
968         }
969
970         /**
971          * Global variables are deprecated. For backwards compatibility only.
972          *
973          * @deprecated For backwards compatibility only.
974          * @access private
975          *
976          * @since 1.6
977          */
978         function register_globals() {
979                 $GLOBALS['weekday']         = $this->weekday;
980                 $GLOBALS['weekday_initial'] = $this->weekday_initial;
981                 $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
982                 $GLOBALS['month']           = $this->month;
983                 $GLOBALS['month_abbrev']    = $this->month_abbrev;
984         }
985
986         /**
987          * Constructor which calls helper methods to set up object variables
988          *
989          * @uses YOURLS_Locale_Formats::init()
990          * @uses YOURLS_Locale_Formats::register_globals()
991          * @since 1.6
992          *
993          * @return YOURLS_Locale_Formats
994          */
995         function __construct() {
996                 $this->init();
997                 $this->register_globals();
998         }
999
1000         /**
1001          * Checks if current locale is RTL.
1002          *
1003          * @since 1.6
1004          * @return bool Whether locale is RTL.
1005          */
1006         function is_rtl() {
1007                 return 'rtl' == $this->text_direction;
1008         }
1009 }
1010
1011 /**
1012  * Loads a custom translation file (for a plugin, a theme, a public interface...)
1013  *
1014  * The .mo file should be named based on the domain with a dash, and then the locale exactly,
1015  * eg 'myplugin-pt_BR.mo'
1016  *
1017  * @since 1.6
1018  *
1019  * @param string $domain Unique identifier (the "domain") for retrieving translated strings
1020  * @param string $path Full path to directory containing MO files.
1021  */
1022 function yourls_load_custom_textdomain( $domain, $path ) {
1023         $locale = yourls_apply_filters( 'load_custom_textdomain', yourls_get_locale(), $domain );
1024     $mofile = trim( $path, '/' ) . '/'. $domain . '-' . $locale . '.mo';
1025
1026     return yourls_load_textdomain( $domain, $mofile );
1027 }
1028
1029 /**
1030  * Checks if current locale is RTL. Stolen from WP.
1031  *
1032  * @since 1.6
1033  * @return bool Whether locale is RTL.
1034  */
1035 function yourls_is_rtl() {
1036     global $yourls_locale_formats;
1037         if( !isset( $yourls_locale_formats ) )
1038                 $yourls_locale_formats = new YOURLS_Locale_Formats();
1039                 
1040         return $yourls_locale_formats->is_rtl();
1041 }
1042
1043 /**
1044  * Return translated weekday abbreviation (3 letters, eg 'Fri' for 'Friday')
1045  *
1046  * The $weekday var can be a textual string ('Friday'), a integer (0 to 6) or an empty string
1047  * If $weekday is an empty string, the function returns an array of all translated weekday abbrev
1048  *
1049  * @since 1.6
1050  * @param mixed $weekday A full textual weekday, eg "Friday", or an integer (0 = Sunday, 1 = Monday, .. 6 = Saturday)
1051  * @return mixed Translated weekday abbreviation, eg "Ven" (abbrev of "Vendredi") for "Friday" or 5, or array of all weekday abbrev
1052  */
1053 function yourls_l10n_weekday_abbrev( $weekday = '' ){
1054     global $yourls_locale_formats;
1055         if( !isset( $yourls_locale_formats ) )
1056                 $yourls_locale_formats = new YOURLS_Locale_Formats();
1057                 
1058         if( $weekday === '' )
1059                 return $yourls_locale_formats->weekday_abbrev;
1060         
1061         if( is_int( $weekday ) ) {
1062                 $day = $yourls_locale_formats->weekday[ $weekday ];
1063                 return $yourls_locale_formats->weekday_abbrev[ $day ];
1064         } else {
1065                 return $yourls_locale_formats->weekday_abbrev[ yourls__( $weekday ) ];
1066         }
1067 }
1068
1069 /**
1070  * Return translated weekday initial (1 letter, eg 'F' for 'Friday')
1071  *
1072  * The $weekday var can be a textual string ('Friday'), a integer (0 to 6) or an empty string
1073  * If $weekday is an empty string, the function returns an array of all translated weekday initials
1074  *
1075  * @since 1.6
1076  * @param mixed $weekday A full textual weekday, eg "Friday", an integer (0 = Sunday, 1 = Monday, .. 6 = Saturday) or empty string
1077  * @return mixed Translated weekday initial, eg "V" (initial of "Vendredi") for "Friday" or 5, or array of all weekday initials
1078  */
1079 function yourls_l10n_weekday_initial( $weekday = '' ){
1080     global $yourls_locale_formats;
1081         if( !isset( $yourls_locale_formats ) )
1082                 $yourls_locale_formats = new YOURLS_Locale_Formats();
1083                 
1084         if( $weekday === '' )
1085                 return $yourls_locale_formats->weekday_initial;
1086         
1087         if( is_int( $weekday ) ) {
1088                 $weekday = $yourls_locale_formats->weekday[ $weekday ];
1089                 return $yourls_locale_formats->weekday_initial[ $weekday ];
1090         } else {
1091                 return $yourls_locale_formats->weekday_initial[ yourls__( $weekday ) ];
1092         }
1093 }
1094
1095 /**
1096  * Return translated month abbrevation (3 letters, eg 'Nov' for 'November')
1097  *
1098  * The $month var can be a textual string ('November'), a integer (1 to 12), a two digits strings ('01' to '12), or an empty string
1099  * If $month is an empty string, the function returns an array of all translated abbrev months ('January' => 'Jan', ...)
1100  *
1101  * @since 1.6
1102  * @param mixed $month Empty string, a full textual weekday, eg "November", or an integer (1 = January, .., 12 = December)
1103  * @return mixed Translated month abbrev (eg "Nov"), or array of all translated abbrev months
1104  */
1105 function yourls_l10n_month_abbrev( $month = '' ){
1106     global $yourls_locale_formats;
1107         if( !isset( $yourls_locale_formats ) )
1108                 $yourls_locale_formats = new YOURLS_Locale_Formats();
1109         
1110         if( $month === '' )
1111                 return $yourls_locale_formats->month_abbrev;
1112         
1113         if( intval( $month ) > 0 ) {
1114                 $month = $yourls_locale_formats->month[ $month ];
1115                 return $yourls_locale_formats->month_abbrev[ $month ];
1116         } else {
1117                 return $yourls_locale_formats->month_abbrev[ yourls__( $month ) ];
1118         }
1119 }
1120
1121 /**
1122  * Return array of all translated months
1123  *
1124  * @since 1.6
1125  * @return array Array of all translated months
1126  */
1127 function yourls_l10n_months(){
1128     global $yourls_locale_formats;
1129         if( !isset( $yourls_locale_formats ) )
1130                 $yourls_locale_formats = new YOURLS_Locale_Formats();
1131         
1132         return $yourls_locale_formats->month;
1133 }