70 /** This filter is documented in wp-includes/l10n.php */ |
74 /** This filter is documented in wp-includes/l10n.php */ |
71 return apply_filters( 'locale', $locale ); |
75 return apply_filters( 'locale', $locale ); |
72 } |
76 } |
73 |
77 |
74 /** |
78 /** |
|
79 * Retrieves the locale of a user. |
|
80 * |
|
81 * If the user has a locale set to a non-empty string then it will be |
|
82 * returned. Otherwise it returns the locale of get_locale(). |
|
83 * |
|
84 * @since 4.7.0 |
|
85 * |
|
86 * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user. |
|
87 * @return string The locale of the user. |
|
88 */ |
|
89 function get_user_locale( $user_id = 0 ) { |
|
90 $user = false; |
|
91 if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) { |
|
92 $user = wp_get_current_user(); |
|
93 } elseif ( $user_id instanceof WP_User ) { |
|
94 $user = $user_id; |
|
95 } elseif ( $user_id && is_numeric( $user_id ) ) { |
|
96 $user = get_user_by( 'id', $user_id ); |
|
97 } |
|
98 |
|
99 if ( ! $user ) { |
|
100 return get_locale(); |
|
101 } |
|
102 |
|
103 $locale = $user->locale; |
|
104 return $locale ? $locale : get_locale(); |
|
105 } |
|
106 |
|
107 /** |
75 * Retrieve the translation of $text. |
108 * Retrieve the translation of $text. |
76 * |
109 * |
77 * If there is no translation, or the text domain isn't loaded, the original text is returned. |
110 * If there is no translation, or the text domain isn't loaded, the original text is returned. |
78 * |
111 * |
79 * *Note:* Don't use {@see translate()} directly, use `{@see __()} or related functions. |
112 * *Note:* Don't use translate() directly, use __() or related functions. |
80 * |
113 * |
81 * @since 2.2.0 |
114 * @since 2.2.0 |
82 * |
115 * |
83 * @param string $text Text to translate. |
116 * @param string $text Text to translate. |
84 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
117 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
118 * Default 'default'. |
85 * @return string Translated text |
119 * @return string Translated text |
86 */ |
120 */ |
87 function translate( $text, $domain = 'default' ) { |
121 function translate( $text, $domain = 'default' ) { |
88 $translations = get_translations_for_domain( $domain ); |
122 $translations = get_translations_for_domain( $domain ); |
89 $translations = $translations->translate( $text ); |
123 $translation = $translations->translate( $text ); |
90 |
124 |
91 /** |
125 /** |
92 * Filter text with its translation. |
126 * Filters text with its translation. |
93 * |
127 * |
94 * @since 2.0.11 |
128 * @since 2.0.11 |
95 * |
129 * |
96 * @param string $translations Translated text. |
130 * @param string $translation Translated text. |
97 * @param string $text Text to translate. |
131 * @param string $text Text to translate. |
98 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
132 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
99 */ |
133 */ |
100 return apply_filters( 'gettext', $translations, $text, $domain ); |
134 return apply_filters( 'gettext', $translation, $text, $domain ); |
101 } |
135 } |
102 |
136 |
103 /** |
137 /** |
104 * Remove last item on a pipe-delimited string. |
138 * Remove last item on a pipe-delimited string. |
105 * |
139 * |
111 * @param string $string A pipe-delimited string. |
145 * @param string $string A pipe-delimited string. |
112 * @return string Either $string or everything before the last pipe. |
146 * @return string Either $string or everything before the last pipe. |
113 */ |
147 */ |
114 function before_last_bar( $string ) { |
148 function before_last_bar( $string ) { |
115 $last_bar = strrpos( $string, '|' ); |
149 $last_bar = strrpos( $string, '|' ); |
116 if ( false == $last_bar ) |
150 if ( false === $last_bar ) { |
117 return $string; |
151 return $string; |
118 else |
152 } else { |
119 return substr( $string, 0, $last_bar ); |
153 return substr( $string, 0, $last_bar ); |
|
154 } |
120 } |
155 } |
121 |
156 |
122 /** |
157 /** |
123 * Retrieve the translation of $text in the context defined in $context. |
158 * Retrieve the translation of $text in the context defined in $context. |
124 * |
159 * |
125 * If there is no translation, or the text domain isn't loaded the original |
160 * If there is no translation, or the text domain isn't loaded the original |
126 * text is returned. |
161 * text is returned. |
|
162 * |
|
163 * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions. |
127 * |
164 * |
128 * @since 2.8.0 |
165 * @since 2.8.0 |
129 * |
166 * |
130 * @param string $text Text to translate. |
167 * @param string $text Text to translate. |
131 * @param string $context Context information for the translators. |
168 * @param string $context Context information for the translators. |
132 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
169 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
170 * Default 'default'. |
133 * @return string Translated text on success, original text on failure. |
171 * @return string Translated text on success, original text on failure. |
134 */ |
172 */ |
135 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { |
173 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { |
136 $translations = get_translations_for_domain( $domain ); |
174 $translations = get_translations_for_domain( $domain ); |
137 $translations = $translations->translate( $text, $context ); |
175 $translation = $translations->translate( $text, $context ); |
138 /** |
176 /** |
139 * Filter text with its translation based on context information. |
177 * Filters text with its translation based on context information. |
140 * |
178 * |
141 * @since 2.8.0 |
179 * @since 2.8.0 |
142 * |
180 * |
143 * @param string $translations Translated text. |
181 * @param string $translation Translated text. |
144 * @param string $text Text to translate. |
182 * @param string $text Text to translate. |
145 * @param string $context Context information for the translators. |
183 * @param string $context Context information for the translators. |
146 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
184 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
147 */ |
185 */ |
148 return apply_filters( 'gettext_with_context', $translations, $text, $context, $domain ); |
186 return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain ); |
149 } |
187 } |
150 |
188 |
151 /** |
189 /** |
152 * Retrieve the translation of $text. If there is no translation, |
190 * Retrieve the translation of $text. |
153 * or the text domain isn't loaded, the original text is returned. |
191 * |
|
192 * If there is no translation, or the text domain isn't loaded, the original text is returned. |
154 * |
193 * |
155 * @since 2.1.0 |
194 * @since 2.1.0 |
156 * |
195 * |
157 * @param string $text Text to translate. |
196 * @param string $text Text to translate. |
158 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
197 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
198 * Default 'default'. |
159 * @return string Translated text. |
199 * @return string Translated text. |
160 */ |
200 */ |
161 function __( $text, $domain = 'default' ) { |
201 function __( $text, $domain = 'default' ) { |
162 return translate( $text, $domain ); |
202 return translate( $text, $domain ); |
163 } |
203 } |
282 * @since 2.9.0 |
331 * @since 2.9.0 |
283 * |
332 * |
284 * @param string $text Text to translate. |
333 * @param string $text Text to translate. |
285 * @param string $context Context information for the translators. |
334 * @param string $context Context information for the translators. |
286 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
335 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
336 * Default 'default'. |
287 * @return string Translated text. |
337 * @return string Translated text. |
288 */ |
338 */ |
289 function esc_html_x( $text, $context, $domain = 'default' ) { |
339 function esc_html_x( $text, $context, $domain = 'default' ) { |
290 return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); |
340 return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); |
291 } |
341 } |
292 |
342 |
293 /** |
343 /** |
294 * Retrieve the plural or single form based on the supplied amount. |
344 * Translates and retrieves the singular or plural form based on the supplied number. |
295 * |
345 * |
296 * If the text domain is not set in the $l10n list, then a comparison will be made |
346 * Used when you want to use the appropriate form of a string based on whether a |
297 * and either $plural or $single parameters returned. |
347 * number is singular or plural. |
298 * |
348 * |
299 * If the text domain does exist, then the parameters $single, $plural, and $number |
349 * Example: |
300 * will first be passed to the text domain's ngettext method. Then it will be passed |
350 * |
301 * to the 'ngettext' filter hook along with the same parameters. The expected |
351 * printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) ); |
302 * type will be a string. |
|
303 * |
352 * |
304 * @since 2.8.0 |
353 * @since 2.8.0 |
305 * |
354 * |
306 * @param string $single The text that will be used if $number is 1. |
355 * @param string $single The text to be used if the number is singular. |
307 * @param string $plural The text that will be used if $number is not 1. |
356 * @param string $plural The text to be used if the number is plural. |
308 * @param int $number The number to compare against to use either $single or $plural. |
357 * @param int $number The number to compare against to use either the singular or plural form. |
309 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
358 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
310 * @return string Either $single or $plural translated text. |
359 * Default 'default'. |
|
360 * @return string The translated singular or plural form. |
311 */ |
361 */ |
312 function _n( $single, $plural, $number, $domain = 'default' ) { |
362 function _n( $single, $plural, $number, $domain = 'default' ) { |
313 $translations = get_translations_for_domain( $domain ); |
363 $translations = get_translations_for_domain( $domain ); |
314 $translation = $translations->translate_plural( $single, $plural, $number ); |
364 $translation = $translations->translate_plural( $single, $plural, $number ); |
|
365 |
315 /** |
366 /** |
316 * Filter text with its translation when plural option is available. |
367 * Filters the singular or plural form of a string. |
317 * |
368 * |
318 * @since 2.2.0 |
369 * @since 2.2.0 |
319 * |
370 * |
320 * @param string $translation Translated text. |
371 * @param string $translation Translated text. |
321 * @param string $single The text that will be used if $number is 1. |
372 * @param string $single The text to be used if the number is singular. |
322 * @param string $plural The text that will be used if $number is not 1. |
373 * @param string $plural The text to be used if the number is plural. |
323 * @param string $number The number to compare against to use either $single or $plural. |
374 * @param string $number The number to compare against to use either the singular or plural form. |
324 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
375 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
325 */ |
376 */ |
326 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); |
377 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); |
327 } |
378 } |
328 |
379 |
329 /** |
380 /** |
330 * Retrieve the plural or single form based on the supplied amount with gettext context. |
381 * Translates and retrieves the singular or plural form based on the supplied number, with gettext context. |
331 * |
382 * |
332 * This is a hybrid of _n() and _x(). It supports contexts and plurals. |
383 * This is a hybrid of _n() and _x(). It supports context and plurals. |
|
384 * |
|
385 * Used when you want to use the appropriate form of a string with context based on whether a |
|
386 * number is singular or plural. |
|
387 * |
|
388 * Example of a generic phrase which is disambiguated via the context parameter: |
|
389 * |
|
390 * printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) ); |
|
391 * printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) ); |
333 * |
392 * |
334 * @since 2.8.0 |
393 * @since 2.8.0 |
335 * |
394 * |
336 * @param string $single The text that will be used if $number is 1. |
395 * @param string $single The text to be used if the number is singular. |
337 * @param string $plural The text that will be used if $number is not 1. |
396 * @param string $plural The text to be used if the number is plural. |
338 * @param int $number The number to compare against to use either $single or $plural. |
397 * @param int $number The number to compare against to use either the singular or plural form. |
339 * @param string $context Context information for the translators. |
398 * @param string $context Context information for the translators. |
340 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
399 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
341 * @return string Either $single or $plural translated text with context. |
400 * Default 'default'. |
|
401 * @return string The translated singular or plural form. |
342 */ |
402 */ |
343 function _nx($single, $plural, $number, $context, $domain = 'default') { |
403 function _nx($single, $plural, $number, $context, $domain = 'default') { |
344 $translations = get_translations_for_domain( $domain ); |
404 $translations = get_translations_for_domain( $domain ); |
345 $translation = $translations->translate_plural( $single, $plural, $number, $context ); |
405 $translation = $translations->translate_plural( $single, $plural, $number, $context ); |
|
406 |
346 /** |
407 /** |
347 * Filter text with its translation while plural option and context are available. |
408 * Filters the singular or plural form of a string with gettext context. |
348 * |
409 * |
349 * @since 2.8.0 |
410 * @since 2.8.0 |
350 * |
411 * |
351 * @param string $translation Translated text. |
412 * @param string $translation Translated text. |
352 * @param string $single The text that will be used if $number is 1. |
413 * @param string $single The text to be used if the number is singular. |
353 * @param string $plural The text that will be used if $number is not 1. |
414 * @param string $plural The text to be used if the number is plural. |
354 * @param string $number The number to compare against to use either $single or $plural. |
415 * @param string $number The number to compare against to use either the singular or plural form. |
355 * @param string $context Context information for the translators. |
416 * @param string $context Context information for the translators. |
356 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
417 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
357 */ |
418 */ |
358 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); |
419 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); |
359 } |
420 } |
360 |
421 |
361 /** |
422 /** |
362 * Register plural strings in POT file, but don't translate them. |
423 * Registers plural strings in POT file, but does not translate them. |
363 * |
424 * |
364 * Used when you want to keep structures with translatable plural |
425 * Used when you want to keep structures with translatable plural |
365 * strings and use them later. |
426 * strings and use them later when the number is known. |
366 * |
427 * |
367 * Example: |
428 * Example: |
368 * |
429 * |
|
430 * $message = _n_noop( '%s post', '%s posts', 'text-domain' ); |
|
431 * ... |
|
432 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
|
433 * |
|
434 * @since 2.5.0 |
|
435 * |
|
436 * @param string $singular Singular form to be localized. |
|
437 * @param string $plural Plural form to be localized. |
|
438 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
439 * Default null. |
|
440 * @return array { |
|
441 * Array of translation information for the strings. |
|
442 * |
|
443 * @type string $0 Singular form to be localized. No longer used. |
|
444 * @type string $1 Plural form to be localized. No longer used. |
|
445 * @type string $singular Singular form to be localized. |
|
446 * @type string $plural Plural form to be localized. |
|
447 * @type null $context Context information for the translators. |
|
448 * @type string $domain Text domain. |
|
449 * } |
|
450 */ |
|
451 function _n_noop( $singular, $plural, $domain = null ) { |
|
452 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain ); |
|
453 } |
|
454 |
|
455 /** |
|
456 * Registers plural strings with gettext context in POT file, but does not translate them. |
|
457 * |
|
458 * Used when you want to keep structures with translatable plural |
|
459 * strings and use them later when the number is known. |
|
460 * |
|
461 * Example of a generic phrase which is disambiguated via the context parameter: |
|
462 * |
369 * $messages = array( |
463 * $messages = array( |
370 * 'post' => _n_noop( '%s post', '%s posts' ), |
464 * 'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ), |
371 * 'page' => _n_noop( '%s pages', '%s pages' ), |
465 * 'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ), |
372 * ); |
466 * ); |
373 * ... |
467 * ... |
374 * $message = $messages[ $type ]; |
468 * $message = $messages[ $type ]; |
375 * $usable_text = sprintf( translate_nooped_plural( $message, $count ), $count ); |
469 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
376 * |
470 * |
377 * @since 2.5.0 |
471 * @since 2.8.0 |
378 * |
472 * |
379 * @param string $singular Single form to be i18ned. |
473 * @param string $singular Singular form to be localized. |
380 * @param string $plural Plural form to be i18ned. |
474 * @param string $plural Plural form to be localized. |
|
475 * @param string $context Context information for the translators. |
381 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
476 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
382 * @return array array($singular, $plural) |
477 * Default null. |
383 */ |
478 * @return array { |
384 function _n_noop( $singular, $plural, $domain = null ) { |
479 * Array of translation information for the strings. |
385 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain ); |
480 * |
386 } |
481 * @type string $0 Singular form to be localized. No longer used. |
387 |
482 * @type string $1 Plural form to be localized. No longer used. |
388 /** |
483 * @type string $2 Context information for the translators. No longer used. |
389 * Register plural strings with context in POT file, but don't translate them. |
484 * @type string $singular Singular form to be localized. |
390 * |
485 * @type string $plural Plural form to be localized. |
391 * @since 2.8.0 |
486 * @type string $context Context information for the translators. |
392 * @param string $singular |
487 * @type string $domain Text domain. |
393 * @param string $plural |
488 * } |
394 * @param string $context |
|
395 * @param string|null $domain |
|
396 * @return array |
|
397 */ |
489 */ |
398 function _nx_noop( $singular, $plural, $context, $domain = null ) { |
490 function _nx_noop( $singular, $plural, $context, $domain = null ) { |
399 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain ); |
491 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain ); |
400 } |
492 } |
401 |
493 |
402 /** |
494 /** |
403 * Translate the result of _n_noop() or _nx_noop(). |
495 * Translates and retrieves the singular or plural form of a string that's been registered |
|
496 * with _n_noop() or _nx_noop(). |
|
497 * |
|
498 * Used when you want to use a translatable plural string once the number is known. |
|
499 * |
|
500 * Example: |
|
501 * |
|
502 * $message = _n_noop( '%s post', '%s posts', 'text-domain' ); |
|
503 * ... |
|
504 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
404 * |
505 * |
405 * @since 3.1.0 |
506 * @since 3.1.0 |
406 * |
507 * |
407 * @param array $nooped_plural Array with singular, plural and context keys, usually the result of _n_noop() or _nx_noop() |
508 * @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop(). |
408 * @param int $count Number of objects |
509 * @param int $count Number of objects. |
409 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains |
510 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains |
410 * a text domain passed to _n_noop() or _nx_noop(), it will override this value. |
511 * a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'. |
411 * @return string Either $single or $plural translated text. |
512 * @return string Either $single or $plural translated text. |
412 */ |
513 */ |
413 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { |
514 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { |
414 if ( $nooped_plural['domain'] ) |
515 if ( $nooped_plural['domain'] ) |
415 $domain = $nooped_plural['domain']; |
516 $domain = $nooped_plural['domain']; |
553 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { |
674 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { |
554 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" ); |
675 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" ); |
555 return $return; |
676 return $return; |
556 } |
677 } |
557 |
678 |
558 if ( is_admin() || defined( 'WP_INSTALLING' ) || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { |
679 if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { |
559 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); |
680 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); |
560 } |
681 } |
561 |
682 |
562 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) |
683 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) |
563 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); |
684 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); |
564 |
685 |
565 return $return; |
686 return $return; |
566 } |
687 } |
567 |
688 |
568 /** |
689 /** |
569 * Load a plugin's translated strings. |
690 * Loads a plugin's translated strings. |
570 * |
691 * |
571 * If the path is not given then it will be the root of the plugin directory. |
692 * If the path is not given then it will be the root of the plugin directory. |
572 * |
693 * |
573 * The .mo file should be named based on the text domain with a dash, and then the locale exactly. |
694 * The .mo file should be named based on the text domain with a dash, and then the locale exactly. |
574 * |
695 * |
575 * @since 1.5.0 |
696 * @since 1.5.0 |
|
697 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. |
576 * |
698 * |
577 * @param string $domain Unique identifier for retrieving translated strings |
699 * @param string $domain Unique identifier for retrieving translated strings |
578 * @param string $deprecated Use the $plugin_rel_path parameter instead. |
700 * @param string $deprecated Optional. Use the $plugin_rel_path parameter instead. Default false. |
579 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. |
701 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. |
580 * Default false. |
702 * Default false. |
581 * @return bool True when textdomain is successfully loaded, false otherwise. |
703 * @return bool True when textdomain is successfully loaded, false otherwise. |
582 */ |
704 */ |
583 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { |
705 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { |
584 $locale = get_locale(); |
|
585 /** |
706 /** |
586 * Filter a plugin's locale. |
707 * Filters a plugin's locale. |
587 * |
708 * |
588 * @since 3.0.0 |
709 * @since 3.0.0 |
589 * |
710 * |
590 * @param string $locale The plugin's current locale. |
711 * @param string $locale The plugin's current locale. |
591 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
712 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
592 */ |
713 */ |
593 $locale = apply_filters( 'plugin_locale', $locale, $domain ); |
714 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); |
594 |
715 |
595 if ( false !== $plugin_rel_path ) { |
716 $mofile = $domain . '-' . $locale . '.mo'; |
|
717 |
|
718 // Try to load from the languages directory first. |
|
719 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) { |
|
720 return true; |
|
721 } |
|
722 |
|
723 if ( false !== $plugin_rel_path ) { |
596 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); |
724 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); |
597 } elseif ( false !== $deprecated ) { |
725 } elseif ( false !== $deprecated ) { |
598 _deprecated_argument( __FUNCTION__, '2.7' ); |
726 _deprecated_argument( __FUNCTION__, '2.7.0' ); |
599 $path = ABSPATH . trim( $deprecated, '/' ); |
727 $path = ABSPATH . trim( $deprecated, '/' ); |
600 } else { |
728 } else { |
601 $path = WP_PLUGIN_DIR; |
729 $path = WP_PLUGIN_DIR; |
602 } |
730 } |
603 |
731 |
604 // Load the textdomain according to the plugin first |
732 return load_textdomain( $domain, $path . '/' . $mofile ); |
605 $mofile = $domain . '-' . $locale . '.mo'; |
|
606 if ( $loaded = load_textdomain( $domain, $path . '/'. $mofile ) ) |
|
607 return $loaded; |
|
608 |
|
609 // Otherwise, load from the languages directory |
|
610 $mofile = WP_LANG_DIR . '/plugins/' . $mofile; |
|
611 return load_textdomain( $domain, $mofile ); |
|
612 } |
733 } |
613 |
734 |
614 /** |
735 /** |
615 * Load the translated strings for a plugin residing in the mu-plugins directory. |
736 * Load the translated strings for a plugin residing in the mu-plugins directory. |
616 * |
737 * |
617 * @since 3.0.0 |
738 * @since 3.0.0 |
|
739 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. |
618 * |
740 * |
619 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
741 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
620 * @param string $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which the .mo file resides. |
742 * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo |
621 * Default empty string. |
743 * file resides. Default empty string. |
622 * @return bool True when textdomain is successfully loaded, false otherwise. |
744 * @return bool True when textdomain is successfully loaded, false otherwise. |
623 */ |
745 */ |
624 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { |
746 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { |
625 /** This filter is documented in wp-includes/l10n.php */ |
747 /** This filter is documented in wp-includes/l10n.php */ |
626 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
748 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); |
627 $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) ); |
749 |
628 |
|
629 // Load the textdomain according to the plugin first |
|
630 $mofile = $domain . '-' . $locale . '.mo'; |
750 $mofile = $domain . '-' . $locale . '.mo'; |
631 if ( $loaded = load_textdomain( $domain, $path . $mofile ) ) |
751 |
632 return $loaded; |
752 // Try to load from the languages directory first. |
633 |
753 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) { |
634 // Otherwise, load from the languages directory |
754 return true; |
635 $mofile = WP_LANG_DIR . '/plugins/' . $mofile; |
755 } |
636 return load_textdomain( $domain, $mofile ); |
756 |
|
757 $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); |
|
758 |
|
759 return load_textdomain( $domain, $path . '/' . $mofile ); |
637 } |
760 } |
638 |
761 |
639 /** |
762 /** |
640 * Load the theme's translated strings. |
763 * Load the theme's translated strings. |
641 * |
764 * |
696 $path = get_stylesheet_directory(); |
820 $path = get_stylesheet_directory(); |
697 return load_theme_textdomain( $domain, $path ); |
821 return load_theme_textdomain( $domain, $path ); |
698 } |
822 } |
699 |
823 |
700 /** |
824 /** |
|
825 * Loads plugin and theme textdomains just-in-time. |
|
826 * |
|
827 * When a textdomain is encountered for the first time, we try to load |
|
828 * the translation file from `wp-content/languages`, removing the need |
|
829 * to call load_plugin_texdomain() or load_theme_texdomain(). |
|
830 * |
|
831 * @since 4.6.0 |
|
832 * @access private |
|
833 * |
|
834 * @see get_translations_for_domain() |
|
835 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. |
|
836 * |
|
837 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
838 * @return bool True when the textdomain is successfully loaded, false otherwise. |
|
839 */ |
|
840 function _load_textdomain_just_in_time( $domain ) { |
|
841 global $l10n_unloaded; |
|
842 |
|
843 $l10n_unloaded = (array) $l10n_unloaded; |
|
844 |
|
845 // Short-circuit if domain is 'default' which is reserved for core. |
|
846 if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) { |
|
847 return false; |
|
848 } |
|
849 |
|
850 $translation_path = _get_path_to_translation( $domain ); |
|
851 if ( false === $translation_path ) { |
|
852 return false; |
|
853 } |
|
854 |
|
855 return load_textdomain( $domain, $translation_path ); |
|
856 } |
|
857 |
|
858 /** |
|
859 * Gets the path to a translation file for loading a textdomain just in time. |
|
860 * |
|
861 * Caches the retrieved results internally. |
|
862 * |
|
863 * @since 4.7.0 |
|
864 * @access private |
|
865 * |
|
866 * @see _load_textdomain_just_in_time() |
|
867 * |
|
868 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
869 * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. |
|
870 * @return string|false The path to the translation file or false if no translation file was found. |
|
871 */ |
|
872 function _get_path_to_translation( $domain, $reset = false ) { |
|
873 static $available_translations = array(); |
|
874 |
|
875 if ( true === $reset ) { |
|
876 $available_translations = array(); |
|
877 } |
|
878 |
|
879 if ( ! isset( $available_translations[ $domain ] ) ) { |
|
880 $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain ); |
|
881 } |
|
882 |
|
883 return $available_translations[ $domain ]; |
|
884 } |
|
885 |
|
886 /** |
|
887 * Gets the path to a translation file in the languages directory for the current locale. |
|
888 * |
|
889 * Holds a cached list of available .mo files to improve performance. |
|
890 * |
|
891 * @since 4.7.0 |
|
892 * @access private |
|
893 * |
|
894 * @see _get_path_to_translation() |
|
895 * |
|
896 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
897 * @return string|false The path to the translation file or false if no translation file was found. |
|
898 */ |
|
899 function _get_path_to_translation_from_lang_dir( $domain ) { |
|
900 static $cached_mofiles = null; |
|
901 |
|
902 if ( null === $cached_mofiles ) { |
|
903 $cached_mofiles = array(); |
|
904 |
|
905 $locations = array( |
|
906 WP_LANG_DIR . '/plugins', |
|
907 WP_LANG_DIR . '/themes', |
|
908 ); |
|
909 |
|
910 foreach ( $locations as $location ) { |
|
911 $mofiles = glob( $location . '/*.mo' ); |
|
912 if ( $mofiles ) { |
|
913 $cached_mofiles = array_merge( $cached_mofiles, $mofiles ); |
|
914 } |
|
915 } |
|
916 } |
|
917 |
|
918 $locale = is_admin() ? get_user_locale() : get_locale(); |
|
919 $mofile = "{$domain}-{$locale}.mo"; |
|
920 |
|
921 $path = WP_LANG_DIR . '/plugins/' . $mofile; |
|
922 if ( in_array( $path, $cached_mofiles ) ) { |
|
923 return $path; |
|
924 } |
|
925 |
|
926 $path = WP_LANG_DIR . '/themes/' . $mofile; |
|
927 if ( in_array( $path, $cached_mofiles ) ) { |
|
928 return $path; |
|
929 } |
|
930 |
|
931 return false; |
|
932 } |
|
933 |
|
934 /** |
701 * Return the Translations instance for a text domain. |
935 * Return the Translations instance for a text domain. |
702 * |
936 * |
703 * If there isn't one, returns empty Translations instance. |
937 * If there isn't one, returns empty Translations instance. |
704 * |
938 * |
705 * @since 2.8.0 |
939 * @since 2.8.0 |
706 * |
940 * |
|
941 * @global array $l10n |
|
942 * |
707 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
943 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
708 * @return NOOP_Translations A Translations instance. |
944 * @return Translations|NOOP_Translations A Translations instance. |
709 */ |
945 */ |
710 function get_translations_for_domain( $domain ) { |
946 function get_translations_for_domain( $domain ) { |
711 global $l10n; |
947 global $l10n; |
712 if ( !isset( $l10n[$domain] ) ) { |
948 if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) { |
713 $l10n[$domain] = new NOOP_Translations; |
949 return $l10n[ $domain ]; |
714 } |
950 } |
715 return $l10n[$domain]; |
951 |
|
952 static $noop_translations = null; |
|
953 if ( null === $noop_translations ) { |
|
954 $noop_translations = new NOOP_Translations; |
|
955 } |
|
956 |
|
957 return $noop_translations; |
716 } |
958 } |
717 |
959 |
718 /** |
960 /** |
719 * Whether there are translations for the text domain. |
961 * Whether there are translations for the text domain. |
720 * |
962 * |
721 * @since 3.0.0 |
963 * @since 3.0.0 |
|
964 * |
|
965 * @global array $l10n |
722 * |
966 * |
723 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
967 * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
724 * @return bool Whether there are translations. |
968 * @return bool Whether there are translations. |
725 */ |
969 */ |
726 function is_textdomain_loaded( $domain ) { |
970 function is_textdomain_loaded( $domain ) { |
727 global $l10n; |
971 global $l10n; |
728 return isset( $l10n[$domain] ); |
972 return isset( $l10n[ $domain ] ); |
729 } |
973 } |
730 |
974 |
731 /** |
975 /** |
732 * Translates role name. |
976 * Translates role name. |
733 * |
977 * |
734 * Since the role names are in the database and not in the source there |
978 * Since the role names are in the database and not in the source there |
735 * are dummy gettext calls to get them into the POT file and this function |
979 * are dummy gettext calls to get them into the POT file and this function |
736 * properly translates them back. |
980 * properly translates them back. |
737 * |
981 * |
738 * The before_last_bar() call is needed, because older installs keep the roles |
982 * The before_last_bar() call is needed, because older installations keep the roles |
739 * using the old context format: 'Role name|User role' and just skipping the |
983 * using the old context format: 'Role name|User role' and just skipping the |
740 * content after the last bar is easier than fixing them in the DB. New installs |
984 * content after the last bar is easier than fixing them in the DB. New installations |
741 * won't suffer from that problem. |
985 * won't suffer from that problem. |
742 * |
986 * |
743 * @since 2.8.0 |
987 * @since 2.8.0 |
744 * |
988 * |
745 * @param string $name The role name. |
989 * @param string $name The role name. |
849 |
1106 |
850 /** |
1107 /** |
851 * Language selector. |
1108 * Language selector. |
852 * |
1109 * |
853 * @since 4.0.0 |
1110 * @since 4.0.0 |
|
1111 * @since 4.3.0 Introduced the `echo` argument. |
|
1112 * @since 4.7.0 Introduced the `show_option_site_default` argument. |
854 * |
1113 * |
855 * @see get_available_languages() |
1114 * @see get_available_languages() |
856 * @see wp_get_available_translations() |
1115 * @see wp_get_available_translations() |
857 * |
1116 * |
858 * @param string|array $args { |
1117 * @param string|array $args { |
859 * Optional. Array or string of arguments for outputting the language selector. |
1118 * Optional. Array or string of arguments for outputting the language selector. |
860 * |
1119 * |
861 * @type string $id ID attribute of the select element. Default empty. |
1120 * @type string $id ID attribute of the select element. Default 'locale'. |
862 * @type string $name Name attribute of the select element. Default empty. |
1121 * @type string $name Name attribute of the select element. Default 'locale'. |
863 * @type array $languages List of installed languages, contain only the locales. |
1122 * @type array $languages List of installed languages, contain only the locales. |
864 * Default empty array. |
1123 * Default empty array. |
865 * @type array $translations List of available translations. Default result of |
1124 * @type array $translations List of available translations. Default result of |
866 * {@see wp_get_available_translations()}. |
1125 * wp_get_available_translations(). |
867 * @type string $selected Language which should be selected. Default empty. |
1126 * @type string $selected Language which should be selected. Default empty. |
868 * @type bool $show_available_translations Whether to show available translations. Default true. |
1127 * @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their |
|
1128 * boolean equivalents. Default 1. |
|
1129 * @type bool $show_available_translations Whether to show available translations. Default true. |
|
1130 * @type bool $show_option_site_default Whether to show an option to fall back to the site's locale. Default false. |
869 * } |
1131 * } |
|
1132 * @return string HTML content |
870 */ |
1133 */ |
871 function wp_dropdown_languages( $args = array() ) { |
1134 function wp_dropdown_languages( $args = array() ) { |
872 |
1135 |
873 $args = wp_parse_args( $args, array( |
1136 $parsed_args = wp_parse_args( $args, array( |
874 'id' => '', |
1137 'id' => 'locale', |
875 'name' => '', |
1138 'name' => 'locale', |
876 'languages' => array(), |
1139 'languages' => array(), |
877 'translations' => array(), |
1140 'translations' => array(), |
878 'selected' => '', |
1141 'selected' => '', |
|
1142 'echo' => 1, |
879 'show_available_translations' => true, |
1143 'show_available_translations' => true, |
|
1144 'show_option_site_default' => false, |
880 ) ); |
1145 ) ); |
881 |
1146 |
882 $translations = $args['translations']; |
1147 // Bail if no ID or no name. |
|
1148 if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) { |
|
1149 return; |
|
1150 } |
|
1151 |
|
1152 // English (United States) uses an empty string for the value attribute. |
|
1153 if ( 'en_US' === $parsed_args['selected'] ) { |
|
1154 $parsed_args['selected'] = ''; |
|
1155 } |
|
1156 |
|
1157 $translations = $parsed_args['translations']; |
883 if ( empty( $translations ) ) { |
1158 if ( empty( $translations ) ) { |
884 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
1159 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
885 $translations = wp_get_available_translations(); |
1160 $translations = wp_get_available_translations(); |
886 } |
1161 } |
887 |
1162 |
888 /* |
1163 /* |
889 * $args['languages'] should only contain the locales. Find the locale in |
1164 * $parsed_args['languages'] should only contain the locales. Find the locale in |
890 * $translations to get the native name. Fall back to locale. |
1165 * $translations to get the native name. Fall back to locale. |
891 */ |
1166 */ |
892 $languages = array(); |
1167 $languages = array(); |
893 foreach ( $args['languages'] as $locale ) { |
1168 foreach ( $parsed_args['languages'] as $locale ) { |
894 if ( isset( $translations[ $locale ] ) ) { |
1169 if ( isset( $translations[ $locale ] ) ) { |
895 $translation = $translations[ $locale ]; |
1170 $translation = $translations[ $locale ]; |
896 $languages[] = array( |
1171 $languages[] = array( |
897 'language' => $translation['language'], |
1172 'language' => $translation['language'], |
898 'native_name' => $translation['native_name'], |
1173 'native_name' => $translation['native_name'], |
941 foreach ( $translations as $translation ) { |
1230 foreach ( $translations as $translation ) { |
942 $structure[] = sprintf( |
1231 $structure[] = sprintf( |
943 '<option value="%s" lang="%s"%s>%s</option>', |
1232 '<option value="%s" lang="%s"%s>%s</option>', |
944 esc_attr( $translation['language'] ), |
1233 esc_attr( $translation['language'] ), |
945 esc_attr( current( $translation['iso'] ) ), |
1234 esc_attr( current( $translation['iso'] ) ), |
946 selected( $translation['language'], $args['selected'], false ), |
1235 selected( $translation['language'], $parsed_args['selected'], false ), |
947 esc_html( $translation['native_name'] ) |
1236 esc_html( $translation['native_name'] ) |
948 ); |
1237 ); |
949 } |
1238 } |
950 $structure[] = '</optgroup>'; |
1239 $structure[] = '</optgroup>'; |
951 } |
1240 } |
952 |
1241 |
953 echo join( "\n", $structure ); |
1242 // Combine the output string. |
954 |
1243 $output = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) ); |
955 echo '</select>'; |
1244 $output .= join( "\n", $structure ); |
956 } |
1245 $output .= '</select>'; |
|
1246 |
|
1247 if ( $parsed_args['echo'] ) { |
|
1248 echo $output; |
|
1249 } |
|
1250 |
|
1251 return $output; |
|
1252 } |
|
1253 |
|
1254 /** |
|
1255 * Checks if current locale is RTL. |
|
1256 * |
|
1257 * @since 3.0.0 |
|
1258 * |
|
1259 * @global WP_Locale $wp_locale |
|
1260 * |
|
1261 * @return bool Whether locale is RTL. |
|
1262 */ |
|
1263 function is_rtl() { |
|
1264 global $wp_locale; |
|
1265 if ( ! ( $wp_locale instanceof WP_Locale ) ) { |
|
1266 return false; |
|
1267 } |
|
1268 return $wp_locale->is_rtl(); |
|
1269 } |
|
1270 |
|
1271 /** |
|
1272 * Switches the translations according to the given locale. |
|
1273 * |
|
1274 * @since 4.7.0 |
|
1275 * |
|
1276 * @global WP_Locale_Switcher $wp_locale_switcher |
|
1277 * |
|
1278 * @param string $locale The locale. |
|
1279 * @return bool True on success, false on failure. |
|
1280 */ |
|
1281 function switch_to_locale( $locale ) { |
|
1282 /* @var WP_Locale_Switcher $wp_locale_switcher */ |
|
1283 global $wp_locale_switcher; |
|
1284 |
|
1285 return $wp_locale_switcher->switch_to_locale( $locale ); |
|
1286 } |
|
1287 |
|
1288 /** |
|
1289 * Restores the translations according to the previous locale. |
|
1290 * |
|
1291 * @since 4.7.0 |
|
1292 * |
|
1293 * @global WP_Locale_Switcher $wp_locale_switcher |
|
1294 * |
|
1295 * @return string|false Locale on success, false on error. |
|
1296 */ |
|
1297 function restore_previous_locale() { |
|
1298 /* @var WP_Locale_Switcher $wp_locale_switcher */ |
|
1299 global $wp_locale_switcher; |
|
1300 |
|
1301 return $wp_locale_switcher->restore_previous_locale(); |
|
1302 } |
|
1303 |
|
1304 /** |
|
1305 * Restores the translations according to the original locale. |
|
1306 * |
|
1307 * @since 4.7.0 |
|
1308 * |
|
1309 * @global WP_Locale_Switcher $wp_locale_switcher |
|
1310 * |
|
1311 * @return string|false Locale on success, false on error. |
|
1312 */ |
|
1313 function restore_current_locale() { |
|
1314 /* @var WP_Locale_Switcher $wp_locale_switcher */ |
|
1315 global $wp_locale_switcher; |
|
1316 |
|
1317 return $wp_locale_switcher->restore_current_locale(); |
|
1318 } |
|
1319 |
|
1320 /** |
|
1321 * Whether switch_to_locale() is in effect. |
|
1322 * |
|
1323 * @since 4.7.0 |
|
1324 * |
|
1325 * @global WP_Locale_Switcher $wp_locale_switcher |
|
1326 * |
|
1327 * @return bool True if the locale has been switched, false otherwise. |
|
1328 */ |
|
1329 function is_locale_switched() { |
|
1330 /* @var WP_Locale_Switcher $wp_locale_switcher */ |
|
1331 global $wp_locale_switcher; |
|
1332 |
|
1333 return $wp_locale_switcher->is_switched(); |
|
1334 } |