3 * YOURLS Translation API
\r
5 * YOURLS modification of a small subset from WordPress' Translation API implementation.
\r
13 * Load POMO files required to run library
\r
15 require_once dirname(__FILE__) . '/pomo/mo.php';
\r
16 require_once dirname(__FILE__) . '/pomo/translations.php';
\r
19 * Gets the current locale.
\r
21 * If the locale is set, then it will filter the locale in the 'get_locale' filter
\r
22 * hook and return the value.
\r
24 * If the locale is not set already, then the YOURLS_LANG constant is used if it is
\r
25 * defined. Then it is filtered through the 'get_locale' filter hook and the value
\r
26 * for the locale global set and the locale is returned.
\r
28 * The process to get the locale should only be done once, but the locale will
\r
29 * always be filtered using the 'get_locale' hook.
\r
32 * @uses yourls_apply_filters() Calls 'get_locale' hook on locale value.
\r
33 * @uses $yourls_locale Gets the locale stored in the global.
\r
35 * @return string The locale of the blog or from the 'get_locale' hook.
\r
37 function yourls_get_locale() {
\r
38 global $yourls_locale;
\r
40 if ( isset( $yourls_locale ) )
\r
41 return yourls_apply_filters( 'get_locale', $yourls_locale );
\r
43 // YOURLS_LANG is defined in config.
\r
44 if ( defined( 'YOURLS_LANG' ) )
\r
45 $yourls_locale = YOURLS_LANG;
\r
47 if ( empty( $yourls_locale ) )
\r
48 $yourls_locale = 'en_US';
\r
50 return yourls_apply_filters( 'get_locale', $yourls_locale );
\r
54 * Retrieves the translation of $text. If there is no translation, or
\r
55 * the domain isn't loaded, the original text is returned.
\r
57 * @see yourls__() Don't use yourls_translate() directly, use yourls__()
\r
59 * @uses yourls_apply_filters() Calls 'translate' on domain translated text
\r
60 * with the untranslated text as second parameter.
\r
62 * @param string $text Text to translate.
\r
63 * @param string $domain Domain to retrieve the translated text.
\r
64 * @return string Translated text
\r
66 function yourls_translate( $text, $domain = 'default' ) {
\r
67 $translations = yourls_get_translations_for_domain( $domain );
\r
68 return yourls_apply_filters( 'translate', $translations->translate( $text ), $text, $domain );
\r
72 * Retrieves the translation of $text with a given $context. If there is no translation, or
\r
73 * the domain isn't loaded, the original text is returned.
\r
75 * Quite a few times, there will be collisions with similar translatable text
\r
76 * found in more than two places but with different translated context.
\r
78 * By including the context in the pot file translators can translate the two
\r
79 * strings differently.
\r
82 * @param string $text Text to translate.
\r
83 * @param string $context Context.
\r
84 * @param string $domain Domain to retrieve the translated text.
\r
85 * @return string Translated text
\r
87 function yourls_translate_with_context( $text, $context, $domain = 'default' ) {
\r
88 $translations = yourls_get_translations_for_domain( $domain );
\r
89 return yourls_apply_filters( 'translate_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
\r
93 * Retrieves the translation of $text. If there is no translation, or
\r
94 * the domain isn't loaded, the original text is returned.
\r
96 * @see yourls_translate() An alias of yourls_translate()
\r
99 * @param string $text Text to translate
\r
100 * @param string $domain Optional. Domain to retrieve the translated text
\r
101 * @return string Translated text
\r
103 function yourls__( $text, $domain = 'default' ) {
\r
104 return yourls_translate( $text, $domain );
\r
108 * Retrieves the translation of $text and escapes it for safe use in an attribute.
\r
109 * If there is no translation, or the domain isn't loaded, the original text is returned.
\r
111 * @see yourls_translate() An alias of yourls_translate()
\r
112 * @see yourls_esc_attr()
\r
115 * @param string $text Text to translate
\r
116 * @param string $domain Optional. Domain to retrieve the translated text
\r
117 * @return string Translated text
\r
119 function yourls_esc_attr__( $text, $domain = 'default' ) {
\r
120 return yourls_esc_attr( yourls_translate( $text, $domain ) );
\r
124 * Retrieves the translation of $text and escapes it for safe use in HTML output.
\r
125 * If there is no translation, or the domain isn't loaded, the original text is returned.
\r
127 * @see yourls_translate() An alias of yourls_translate()
\r
128 * @see yourls_esc_html()
\r
131 * @param string $text Text to translate
\r
132 * @param string $domain Optional. Domain to retrieve the translated text
\r
133 * @return string Translated text
\r
135 function yourls_esc_html__( $text, $domain = 'default' ) {
\r
136 return yourls_esc_html( yourls_translate( $text, $domain ) );
\r
140 * Displays the returned translated text from yourls_translate().
\r
142 * @see yourls_translate() Echoes returned yourls_translate() string
\r
145 * @param string $text Text to translate
\r
146 * @param string $domain Optional. Domain to retrieve the translated text
\r
148 function yourls_e( $text, $domain = 'default' ) {
\r
149 echo yourls_translate( $text, $domain );
\r
153 * Displays translated text that has been escaped for safe use in an attribute.
\r
155 * @see yourls_translate() Echoes returned yourls_translate() string
\r
156 * @see yourls_esc_attr()
\r
159 * @param string $text Text to translate
\r
160 * @param string $domain Optional. Domain to retrieve the translated text
\r
162 function yourls_esc_attr_e( $text, $domain = 'default' ) {
\r
163 echo yourls_esc_attr( yourls_translate( $text, $domain ) );
\r
167 * Displays translated text that has been escaped for safe use in HTML output.
\r
169 * @see yourls_translate() Echoes returned yourls_translate() string
\r
170 * @see yourls_esc_html()
\r
173 * @param string $text Text to translate
\r
174 * @param string $domain Optional. Domain to retrieve the translated text
\r
176 function yourls_esc_html_e( $text, $domain = 'default' ) {
\r
177 echo yourls_esc_html( yourls_translate( $text, $domain ) );
\r
181 * Retrieve translated string with gettext context
\r
183 * Quite a few times, there will be collisions with similar translatable text
\r
184 * found in more than two places but with different translated context.
\r
186 * By including the context in the pot file translators can translate the two
\r
187 * strings differently.
\r
191 * @param string $text Text to translate
\r
192 * @param string $context Context information for the translators
\r
193 * @param string $domain Optional. Domain to retrieve the translated text
\r
194 * @return string Translated context string without pipe
\r
196 function yourls_x( $text, $context, $domain = 'default' ) {
\r
197 return yourls_translate_with_context( $text, $context, $domain );
\r
201 * Displays translated string with gettext context
\r
206 * @param string $text Text to translate
\r
207 * @param string $context Context information for the translators
\r
208 * @param string $domain Optional. Domain to retrieve the translated text
\r
209 * @return string Translated context string without pipe
\r
211 function yourls_ex( $text, $context, $domain = 'default' ) {
\r
212 echo yourls_x( $text, $context, $domain );
\r
217 * Return translated text, with context, that has been escaped for safe use in an attribute
\r
219 * @see yourls_translate() Return returned yourls_translate() string
\r
220 * @see yourls_esc_attr()
\r
224 * @param string $text Text to translate
\r
225 * @param string $domain Optional. Domain to retrieve the translated text
\r
227 function yourls_esc_attr_x( $single, $context, $domain = 'default' ) {
\r
228 return yourls_esc_attr( yourls_translate_with_context( $single, $context, $domain ) );
\r
232 * Return translated text, with context, that has been escaped for safe use in HTML output
\r
234 * @see yourls_translate() Return returned yourls_translate() string
\r
235 * @see yourls_esc_attr()
\r
239 * @param string $text Text to translate
\r
240 * @param string $domain Optional. Domain to retrieve the translated text
\r
242 function yourls_esc_html_x( $single, $context, $domain = 'default' ) {
\r
243 return yourls_esc_html( yourls_translate_with_context( $single, $context, $domain ) );
\r
247 * Retrieve the plural or single form based on the amount.
\r
249 * If the domain is not set in the $yourls_l10n list, then a comparison will be made
\r
250 * and either $plural or $single parameters returned.
\r
252 * If the domain does exist, then the parameters $single, $plural, and $number
\r
253 * will first be passed to the domain's ngettext method. Then it will be passed
\r
254 * to the 'translate_n' filter hook along with the same parameters. The expected
\r
255 * type will be a string.
\r
258 * @uses $yourls_l10n Gets list of domain translated string (gettext_reader) objects
\r
259 * @uses yourls_apply_filters() Calls 'translate_n' hook on domains text returned,
\r
260 * along with $single, $plural, and $number parameters. Expected to return string.
\r
262 * @param string $single The text that will be used if $number is 1
\r
263 * @param string $plural The text that will be used if $number is not 1
\r
264 * @param int $number The number to compare against to use either $single or $plural
\r
265 * @param string $domain Optional. The domain identifier the text should be retrieved in
\r
266 * @return string Either $single or $plural translated text
\r
268 function yourls_n( $single, $plural, $number, $domain = 'default' ) {
\r
269 $translations = yourls_get_translations_for_domain( $domain );
\r
270 $translation = $translations->translate_plural( $single, $plural, $number );
\r
271 return yourls_apply_filters( 'translate_n', $translation, $single, $plural, $number, $domain );
\r
275 * A hybrid of yourls_n() and yourls_x(). It supports contexts and plurals.
\r
282 function yourls_nx($single, $plural, $number, $context, $domain = 'default') {
\r
283 $translations = yourls_get_translations_for_domain( $domain );
\r
284 $translation = $translations->translate_plural( $single, $plural, $number, $context );
\r
285 return yourls_apply_filters( 'translate_nx', $translation, $single, $plural, $number, $context, $domain );
\r
289 * Register plural strings in POT file, but don't translate them.
\r
291 * Used when you want to keep structures with translatable plural strings and
\r
295 * $messages = array(
\r
296 * 'post' => yourls_n_noop('%s post', '%s posts'),
\r
297 * 'page' => yourls_n_noop('%s pages', '%s pages')
\r
300 * $message = $messages[$type];
\r
301 * $usable_text = sprintf( yourls_translate_nooped_plural( $message, $count ), $count );
\r
304 * @param string $singular Single form to be i18ned
\r
305 * @param string $plural Plural form to be i18ned
\r
306 * @param string $domain Optional. The domain identifier the text will be retrieved in
\r
307 * @return array array($singular, $plural)
\r
309 function yourls_n_noop( $singular, $plural, $domain = null ) {
\r
313 'singular' => $singular,
\r
314 'plural' => $plural,
\r
316 'domain' => $domain
\r
321 * Register plural strings with context in POT file, but don't translate them.
\r
324 * @see yourls_n_noop()
\r
326 function yourls_nx_noop( $singular, $plural, $context, $domain = null ) {
\r
331 'singular' => $singular,
\r
332 'plural' => $plural,
\r
333 'context' => $context,
\r
334 'domain' => $domain
\r
339 * Translate the result of yourls_n_noop() or yourls_nx_noop()
\r
342 * @param array $nooped_plural Array with singular, plural and context keys, usually the result of yourls_n_noop() or yourls_nx_noop()
\r
343 * @param int $count Number of objects
\r
344 * @param string $domain Optional. The domain identifier the text should be retrieved in. If $nooped_plural contains
\r
345 * a domain passed to yourls_n_noop() or yourls_nx_noop(), it will override this value.
\r
347 function yourls_translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
\r
348 if ( $nooped_plural['domain'] )
\r
349 $domain = $nooped_plural['domain'];
\r
351 if ( $nooped_plural['context'] )
\r
352 return yourls_nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
\r
354 return yourls_n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
\r
358 * Loads a MO file into the domain $domain.
\r
360 * If the domain already exists, the translations will be merged. If both
\r
361 * sets have the same string, the translation from the original value will be taken.
\r
363 * On success, the .mo file will be placed in the $yourls_l10n global by $domain
\r
364 * and will be a MO object.
\r
367 * @uses $yourls_l10n Gets list of domain translated string objects
\r
369 * @param string $domain Unique identifier for retrieving translated strings
\r
370 * @param string $mofile Path to the .mo file
\r
371 * @return bool True on success, false on failure
\r
373 function yourls_load_textdomain( $domain, $mofile ) {
\r
374 global $yourls_l10n;
\r
376 $plugin_override = yourls_apply_filters( 'override_load_textdomain', false, $domain, $mofile );
\r
378 if ( true == $plugin_override ) {
\r
382 yourls_do_action( 'load_textdomain', $domain, $mofile );
\r
384 $mofile = yourls_apply_filters( 'load_textdomain_mofile', $mofile, $domain );
\r
386 if ( !is_readable( $mofile ) ) return false;
\r
389 if ( !$mo->import_from_file( $mofile ) ) return false;
\r
391 if ( isset( $yourls_l10n[$domain] ) )
\r
392 $mo->merge_with( $yourls_l10n[$domain] );
\r
394 $yourls_l10n[$domain] = &$mo;
\r
400 * Unloads translations for a domain
\r
403 * @param string $domain Textdomain to be unloaded
\r
404 * @return bool Whether textdomain was unloaded
\r
406 function yourls_unload_textdomain( $domain ) {
\r
407 global $yourls_l10n;
\r
409 $plugin_override = yourls_apply_filters( 'override_unload_textdomain', false, $domain );
\r
411 if ( $plugin_override )
\r
414 yourls_do_action( 'unload_textdomain', $domain );
\r
416 if ( isset( $yourls_l10n[$domain] ) ) {
\r
417 unset( $yourls_l10n[$domain] );
\r
425 * Loads default translated strings based on locale.
\r
427 * Loads the .mo file in YOURLS_LANG_DIR constant path from YOURLS root. The
\r
428 * translated (.mo) file is named based on the locale.
\r
432 function yourls_load_default_textdomain() {
\r
433 $yourls_locale = yourls_get_locale();
\r
435 yourls_load_textdomain( 'default', YOURLS_LANG_DIR . "/$yourls_locale.mo" );
\r
440 * Returns the Translations instance for a domain. If there isn't one,
\r
441 * returns empty Translations instance.
\r
443 * @param string $domain
\r
444 * @return object A Translation instance
\r
446 function yourls_get_translations_for_domain( $domain ) {
\r
447 global $yourls_l10n;
\r
448 if ( !isset( $yourls_l10n[$domain] ) ) {
\r
449 $yourls_l10n[$domain] = new NOOP_Translations;
\r
451 return $yourls_l10n[$domain];
\r
455 * Whether there are translations for the domain
\r
458 * @param string $domain
\r
459 * @return bool Whether there are translations
\r
461 function yourls_is_textdomain_loaded( $domain ) {
\r
462 global $yourls_l10n;
\r
463 return isset( $yourls_l10n[$domain] );
\r
467 * Translates role name. Unused.
\r
469 * Unused function for the moment, we'll see when there are roles.
\r
470 * From the WP source: Since the role names are in the database and
\r
471 * not in the source there are dummy gettext calls to get them into the POT
\r
472 * file and this function properly translates them back.
\r
476 function yourls_translate_user_role( $name ) {
\r
477 return yourls_translate_with_context( $name, 'User role' );
\r
481 * Get all available languages (*.mo files) in a given directory. The default directory is YOURLS_LANG_DIR.
\r
485 * @param string $dir A directory in which to search for language files. The default directory is YOURLS_LANG_DIR.
\r
486 * @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.
\r
488 function yourls_get_available_languages( $dir = null ) {
\r
489 $languages = array();
\r
491 $dir = is_null( $dir) ? YOURLS_LANG_DIR : $dir;
\r
493 foreach( (array) glob( $dir . '/*.mo' ) as $lang_file ) {
\r
494 $languages[] = basename( $lang_file, '.mo' );
\r
497 return yourls_apply_filters( 'get_available_languages', $languages );
\r