11 * Retrieves the current locale.
13 * If the locale is set, then it will filter the locale in the {@see 'locale'}
14 * filter hook and return the value.
16 * If the locale is not set already, then the WPLANG constant is used if it is
17 * defined. Then it is filtered through the {@see 'locale'} filter hook and
18 * the value for the locale global set and the locale is returned.
20 * The process to get the locale should only be done once, but the locale will
21 * always be filtered using the {@see 'locale'} hook.
25 * @global string $locale
26 * @global string $wp_local_package
28 * @return string The locale of the blog or from the {@see 'locale'} hook.
30 function get_locale() {
31 global $locale, $wp_local_package;
33 if ( isset( $locale ) ) {
35 * Filters WordPress install's locale ID.
39 * @param string $locale The locale ID.
41 return apply_filters( 'locale', $locale );
44 if ( isset( $wp_local_package ) ) {
45 $locale = $wp_local_package;
48 // WPLANG was defined in wp-config.
49 if ( defined( 'WPLANG' ) ) {
53 // If multisite, check options.
54 if ( is_multisite() ) {
55 // Don't check blog option when installing.
56 if ( wp_installing() || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
57 $ms_locale = get_site_option( 'WPLANG' );
60 if ( $ms_locale !== false ) {
64 $db_locale = get_option( 'WPLANG' );
65 if ( $db_locale !== false ) {
70 if ( empty( $locale ) ) {
74 /** This filter is documented in wp-includes/l10n.php */
75 return apply_filters( 'locale', $locale );
79 * Retrieves the locale of a user.
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().
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.
89 function get_user_locale( $user_id = 0 ) {
91 if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
92 $user = wp_get_current_user();
93 } elseif ( $user_id instanceof WP_User ) {
95 } elseif ( $user_id && is_numeric( $user_id ) ) {
96 $user = get_user_by( 'id', $user_id );
103 $locale = $user->locale;
104 return $locale ? $locale : get_locale();
108 * Retrieve the translation of $text.
110 * If there is no translation, or the text domain isn't loaded, the original text is returned.
112 * *Note:* Don't use translate() directly, use __() or related functions.
116 * @param string $text Text to translate.
117 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
119 * @return string Translated text
121 function translate( $text, $domain = 'default' ) {
122 $translations = get_translations_for_domain( $domain );
123 $translation = $translations->translate( $text );
126 * Filters text with its translation.
130 * @param string $translation Translated text.
131 * @param string $text Text to translate.
132 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
134 return apply_filters( 'gettext', $translation, $text, $domain );
138 * Remove last item on a pipe-delimited string.
140 * Meant for removing the last item in a string, such as 'Role name|User role'. The original
141 * string will be returned if no pipe '|' characters are found in the string.
145 * @param string $string A pipe-delimited string.
146 * @return string Either $string or everything before the last pipe.
148 function before_last_bar( $string ) {
149 $last_bar = strrpos( $string, '|' );
150 if ( false === $last_bar ) {
153 return substr( $string, 0, $last_bar );
158 * Retrieve the translation of $text in the context defined in $context.
160 * If there is no translation, or the text domain isn't loaded the original
163 * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.
167 * @param string $text Text to translate.
168 * @param string $context Context information for the translators.
169 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
171 * @return string Translated text on success, original text on failure.
173 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
174 $translations = get_translations_for_domain( $domain );
175 $translation = $translations->translate( $text, $context );
177 * Filters text with its translation based on context information.
181 * @param string $translation Translated text.
182 * @param string $text Text to translate.
183 * @param string $context Context information for the translators.
184 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
186 return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
190 * Retrieve the translation of $text.
192 * If there is no translation, or the text domain isn't loaded, the original text is returned.
196 * @param string $text Text to translate.
197 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
199 * @return string Translated text.
201 function __( $text, $domain = 'default' ) {
202 return translate( $text, $domain );
206 * Retrieve the translation of $text and escapes it for safe use in an attribute.
208 * If there is no translation, or the text domain isn't loaded, the original text is returned.
212 * @param string $text Text to translate.
213 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
215 * @return string Translated text on success, original text on failure.
217 function esc_attr__( $text, $domain = 'default' ) {
218 return esc_attr( translate( $text, $domain ) );
222 * Retrieve the translation of $text and escapes it for safe use in HTML output.
224 * If there is no translation, or the text domain isn't loaded, the original text is returned.
228 * @param string $text Text to translate.
229 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
231 * @return string Translated text
233 function esc_html__( $text, $domain = 'default' ) {
234 return esc_html( translate( $text, $domain ) );
238 * Display translated text.
242 * @param string $text Text to translate.
243 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
246 function _e( $text, $domain = 'default' ) {
247 echo translate( $text, $domain );
251 * Display translated text that has been escaped for safe use in an attribute.
255 * @param string $text Text to translate.
256 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
259 function esc_attr_e( $text, $domain = 'default' ) {
260 echo esc_attr( translate( $text, $domain ) );
264 * Display translated text that has been escaped for safe use in HTML output.
268 * @param string $text Text to translate.
269 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
272 function esc_html_e( $text, $domain = 'default' ) {
273 echo esc_html( translate( $text, $domain ) );
277 * Retrieve translated string with gettext context.
279 * Quite a few times, there will be collisions with similar translatable text
280 * found in more than two places, but with different translated context.
282 * By including the context in the pot file, translators can translate the two
283 * strings differently.
287 * @param string $text Text to translate.
288 * @param string $context Context information for the translators.
289 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
291 * @return string Translated context string without pipe.
293 function _x( $text, $context, $domain = 'default' ) {
294 return translate_with_gettext_context( $text, $context, $domain );
298 * Display translated string with gettext context.
302 * @param string $text Text to translate.
303 * @param string $context Context information for the translators.
304 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
306 * @return string Translated context string without pipe.
308 function _ex( $text, $context, $domain = 'default' ) {
309 echo _x( $text, $context, $domain );
313 * Translate string with gettext context, and escapes it for safe use in an attribute.
317 * @param string $text Text to translate.
318 * @param string $context Context information for the translators.
319 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
321 * @return string Translated text
323 function esc_attr_x( $text, $context, $domain = 'default' ) {
324 return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
328 * Translate string with gettext context, and escapes it for safe use in HTML output.
332 * @param string $text Text to translate.
333 * @param string $context Context information for the translators.
334 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
336 * @return string Translated text.
338 function esc_html_x( $text, $context, $domain = 'default' ) {
339 return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
343 * Translates and retrieves the singular or plural form based on the supplied number.
345 * Used when you want to use the appropriate form of a string based on whether a
346 * number is singular or plural.
350 * printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );
354 * @param string $single The text to be used if the number is singular.
355 * @param string $plural The text to be used if the number is plural.
356 * @param int $number The number to compare against to use either the singular or plural form.
357 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
359 * @return string The translated singular or plural form.
361 function _n( $single, $plural, $number, $domain = 'default' ) {
362 $translations = get_translations_for_domain( $domain );
363 $translation = $translations->translate_plural( $single, $plural, $number );
366 * Filters the singular or plural form of a string.
370 * @param string $translation Translated text.
371 * @param string $single The text to be used if the number is singular.
372 * @param string $plural The text to be used if the number is plural.
373 * @param string $number The number to compare against to use either the singular or plural form.
374 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
376 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
380 * Translates and retrieves the singular or plural form based on the supplied number, with gettext context.
382 * This is a hybrid of _n() and _x(). It supports context and plurals.
384 * Used when you want to use the appropriate form of a string with context based on whether a
385 * number is singular or plural.
387 * Example of a generic phrase which is disambiguated via the context parameter:
389 * printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
390 * printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );
394 * @param string $single The text to be used if the number is singular.
395 * @param string $plural The text to be used if the number is plural.
396 * @param int $number The number to compare against to use either the singular or plural form.
397 * @param string $context Context information for the translators.
398 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
400 * @return string The translated singular or plural form.
402 function _nx($single, $plural, $number, $context, $domain = 'default') {
403 $translations = get_translations_for_domain( $domain );
404 $translation = $translations->translate_plural( $single, $plural, $number, $context );
407 * Filters the singular or plural form of a string with gettext context.
411 * @param string $translation Translated text.
412 * @param string $single The text to be used if the number is singular.
413 * @param string $plural The text to be used if the number is plural.
414 * @param string $number The number to compare against to use either the singular or plural form.
415 * @param string $context Context information for the translators.
416 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
418 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
422 * Registers plural strings in POT file, but does not translate them.
424 * Used when you want to keep structures with translatable plural
425 * strings and use them later when the number is known.
429 * $message = _n_noop( '%s post', '%s posts', 'text-domain' );
431 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
435 * @param string $singular Singular form to be localized.
436 * @param string $plural Plural form to be localized.
437 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
440 * Array of translation information for the strings.
442 * @type string $0 Singular form to be localized. No longer used.
443 * @type string $1 Plural form to be localized. No longer used.
444 * @type string $singular Singular form to be localized.
445 * @type string $plural Plural form to be localized.
446 * @type null $context Context information for the translators.
447 * @type string $domain Text domain.
450 function _n_noop( $singular, $plural, $domain = null ) {
451 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain );
455 * Registers plural strings with gettext context in POT file, but does not translate them.
457 * Used when you want to keep structures with translatable plural
458 * strings and use them later when the number is known.
460 * Example of a generic phrase which is disambiguated via the context parameter:
463 * 'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
464 * 'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
467 * $message = $messages[ $type ];
468 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
472 * @param string $singular Singular form to be localized.
473 * @param string $plural Plural form to be localized.
474 * @param string $context Context information for the translators.
475 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
478 * Array of translation information for the strings.
480 * @type string $0 Singular form to be localized. No longer used.
481 * @type string $1 Plural form to be localized. No longer used.
482 * @type string $2 Context information for the translators. No longer used.
483 * @type string $singular Singular form to be localized.
484 * @type string $plural Plural form to be localized.
485 * @type string $context Context information for the translators.
486 * @type string $domain Text domain.
489 function _nx_noop( $singular, $plural, $context, $domain = null ) {
490 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain );
494 * Translates and retrieves the singular or plural form of a string that's been registered
495 * with _n_noop() or _nx_noop().
497 * Used when you want to use a translatable plural string once the number is known.
501 * $message = _n_noop( '%s post', '%s posts', 'text-domain' );
503 * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
507 * @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
508 * @param int $count Number of objects.
509 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
510 * a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
511 * @return string Either $single or $plural translated text.
513 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
514 if ( $nooped_plural['domain'] )
515 $domain = $nooped_plural['domain'];
517 if ( $nooped_plural['context'] )
518 return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
520 return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
524 * Load a .mo file into the text domain $domain.
526 * If the text domain already exists, the translations will be merged. If both
527 * sets have the same string, the translation from the original value will be taken.
529 * On success, the .mo file will be placed in the $l10n global by $domain
530 * and will be a MO object.
534 * @global array $l10n An array of all currently loaded text domains.
535 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
537 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
538 * @param string $mofile Path to the .mo file.
539 * @return bool True on success, false on failure.
541 function load_textdomain( $domain, $mofile ) {
542 global $l10n, $l10n_unloaded;
544 $l10n_unloaded = (array) $l10n_unloaded;
547 * Filters whether to override the .mo file loading.
551 * @param bool $override Whether to override the .mo file loading. Default false.
552 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
553 * @param string $mofile Path to the MO file.
555 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
557 if ( true == $plugin_override ) {
558 unset( $l10n_unloaded[ $domain ] );
564 * Fires before the MO translation file is loaded.
568 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
569 * @param string $mofile Path to the .mo file.
571 do_action( 'load_textdomain', $domain, $mofile );
574 * Filters MO file path for loading translations for a specific text domain.
578 * @param string $mofile Path to the MO file.
579 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
581 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
583 if ( !is_readable( $mofile ) ) return false;
586 if ( !$mo->import_from_file( $mofile ) ) return false;
588 if ( isset( $l10n[$domain] ) )
589 $mo->merge_with( $l10n[$domain] );
591 unset( $l10n_unloaded[ $domain ] );
593 $l10n[$domain] = &$mo;
599 * Unload translations for a text domain.
603 * @global array $l10n An array of all currently loaded text domains.
604 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
606 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
607 * @return bool Whether textdomain was unloaded.
609 function unload_textdomain( $domain ) {
610 global $l10n, $l10n_unloaded;
612 $l10n_unloaded = (array) $l10n_unloaded;
615 * Filters whether to override the text domain unloading.
619 * @param bool $override Whether to override the text domain unloading. Default false.
620 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
622 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
624 if ( $plugin_override ) {
625 $l10n_unloaded[ $domain ] = true;
631 * Fires before the text domain is unloaded.
635 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
637 do_action( 'unload_textdomain', $domain );
639 if ( isset( $l10n[$domain] ) ) {
640 unset( $l10n[$domain] );
642 $l10n_unloaded[ $domain ] = true;
651 * Load default translated strings based on locale.
653 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
654 * The translated (.mo) file is named based on the locale.
656 * @see load_textdomain()
660 * @param string $locale Optional. Locale to load. Default is the value of get_locale().
661 * @return bool Whether the textdomain was loaded.
663 function load_default_textdomain( $locale = null ) {
664 if ( null === $locale ) {
665 $locale = is_admin() ? get_user_locale() : get_locale();
668 // Unload previously loaded strings so we can switch translations.
669 unload_textdomain( 'default' );
671 $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
673 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
674 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
678 if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
679 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
682 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
683 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
689 * Loads a plugin's translated strings.
691 * If the path is not given then it will be the root of the plugin directory.
693 * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
696 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
698 * @param string $domain Unique identifier for retrieving translated strings
699 * @param string $deprecated Optional. Use the $plugin_rel_path parameter instead. Default false.
700 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
702 * @return bool True when textdomain is successfully loaded, false otherwise.
704 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
706 * Filters a plugin's locale.
710 * @param string $locale The plugin's current locale.
711 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
713 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
715 $mofile = $domain . '-' . $locale . '.mo';
717 // Try to load from the languages directory first.
718 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
722 if ( false !== $plugin_rel_path ) {
723 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
724 } elseif ( false !== $deprecated ) {
725 _deprecated_argument( __FUNCTION__, '2.7.0' );
726 $path = ABSPATH . trim( $deprecated, '/' );
728 $path = WP_PLUGIN_DIR;
731 return load_textdomain( $domain, $path . '/' . $mofile );
735 * Load the translated strings for a plugin residing in the mu-plugins directory.
738 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
740 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
741 * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
742 * file resides. Default empty string.
743 * @return bool True when textdomain is successfully loaded, false otherwise.
745 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
746 /** This filter is documented in wp-includes/l10n.php */
747 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
749 $mofile = $domain . '-' . $locale . '.mo';
751 // Try to load from the languages directory first.
752 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
756 $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) );
758 return load_textdomain( $domain, $path . '/' . $mofile );
762 * Load the theme's translated strings.
764 * If the current locale exists as a .mo file in the theme's root directory, it
765 * will be included in the translated strings by the $domain.
767 * The .mo files must be named based on the locale exactly.
770 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
772 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
773 * @param string $path Optional. Path to the directory containing the .mo file.
775 * @return bool True when textdomain is successfully loaded, false otherwise.
777 function load_theme_textdomain( $domain, $path = false ) {
779 * Filters a theme's locale.
783 * @param string $locale The theme's current locale.
784 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
786 $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
788 $mofile = $domain . '-' . $locale . '.mo';
790 // Try to load from the languages directory first.
791 if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) {
796 $path = get_template_directory();
799 return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
803 * Load the child themes translated strings.
805 * If the current locale exists as a .mo file in the child themes
806 * root directory, it will be included in the translated strings by the $domain.
808 * The .mo files must be named based on the locale exactly.
812 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
813 * @param string $path Optional. Path to the directory containing the .mo file.
815 * @return bool True when the theme textdomain is successfully loaded, false otherwise.
817 function load_child_theme_textdomain( $domain, $path = false ) {
819 $path = get_stylesheet_directory();
820 return load_theme_textdomain( $domain, $path );
824 * Loads plugin and theme textdomains just-in-time.
826 * When a textdomain is encountered for the first time, we try to load
827 * the translation file from `wp-content/languages`, removing the need
828 * to call load_plugin_texdomain() or load_theme_texdomain().
833 * @see get_translations_for_domain()
834 * @global array $l10n_unloaded An array of all text domains that have been unloaded again.
836 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
837 * @return bool True when the textdomain is successfully loaded, false otherwise.
839 function _load_textdomain_just_in_time( $domain ) {
840 global $l10n_unloaded;
842 $l10n_unloaded = (array) $l10n_unloaded;
844 // Short-circuit if domain is 'default' which is reserved for core.
845 if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
849 $translation_path = _get_path_to_translation( $domain );
850 if ( false === $translation_path ) {
854 return load_textdomain( $domain, $translation_path );
858 * Gets the path to a translation file for loading a textdomain just in time.
860 * Caches the retrieved results internally.
865 * @see _load_textdomain_just_in_time()
867 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
868 * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality.
869 * @return string|false The path to the translation file or false if no translation file was found.
871 function _get_path_to_translation( $domain, $reset = false ) {
872 static $available_translations = array();
874 if ( true === $reset ) {
875 $available_translations = array();
878 if ( ! isset( $available_translations[ $domain ] ) ) {
879 $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
882 return $available_translations[ $domain ];
886 * Gets the path to a translation file in the languages directory for the current locale.
888 * Holds a cached list of available .mo files to improve performance.
893 * @see _get_path_to_translation()
895 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
896 * @return string|false The path to the translation file or false if no translation file was found.
898 function _get_path_to_translation_from_lang_dir( $domain ) {
899 static $cached_mofiles = null;
901 if ( null === $cached_mofiles ) {
902 $cached_mofiles = array();
905 WP_LANG_DIR . '/plugins',
906 WP_LANG_DIR . '/themes',
909 foreach ( $locations as $location ) {
910 $mofiles = glob( $location . '/*.mo' );
912 $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
917 $locale = is_admin() ? get_user_locale() : get_locale();
918 $mofile = "{$domain}-{$locale}.mo";
920 $path = WP_LANG_DIR . '/plugins/' . $mofile;
921 if ( in_array( $path, $cached_mofiles ) ) {
925 $path = WP_LANG_DIR . '/themes/' . $mofile;
926 if ( in_array( $path, $cached_mofiles ) ) {
934 * Return the Translations instance for a text domain.
936 * If there isn't one, returns empty Translations instance.
940 * @global array $l10n
942 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
943 * @return Translations|NOOP_Translations A Translations instance.
945 function get_translations_for_domain( $domain ) {
947 if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
948 return $l10n[ $domain ];
951 static $noop_translations = null;
952 if ( null === $noop_translations ) {
953 $noop_translations = new NOOP_Translations;
956 return $noop_translations;
960 * Whether there are translations for the text domain.
964 * @global array $l10n
966 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
967 * @return bool Whether there are translations.
969 function is_textdomain_loaded( $domain ) {
971 return isset( $l10n[ $domain ] );
975 * Translates role name.
977 * Since the role names are in the database and not in the source there
978 * are dummy gettext calls to get them into the POT file and this function
979 * properly translates them back.
981 * The before_last_bar() call is needed, because older installs keep the roles
982 * using the old context format: 'Role name|User role' and just skipping the
983 * content after the last bar is easier than fixing them in the DB. New installs
984 * won't suffer from that problem.
988 * @param string $name The role name.
989 * @return string Translated role name on success, original name on failure.
991 function translate_user_role( $name ) {
992 return translate_with_gettext_context( before_last_bar($name), 'User role' );
996 * Get all available languages based on the presence of *.mo files in a given directory.
998 * The default directory is WP_LANG_DIR.
1001 * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
1003 * @param string $dir A directory to search for language files.
1004 * Default WP_LANG_DIR.
1005 * @return array An 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.
1007 function get_available_languages( $dir = null ) {
1008 $languages = array();
1010 $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
1011 if ( $lang_files ) {
1012 foreach ( $lang_files as $lang_file ) {
1013 $lang_file = basename( $lang_file, '.mo' );
1014 if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
1015 0 !== strpos( $lang_file, 'admin-' ) ) {
1016 $languages[] = $lang_file;
1022 * Filters the list of available language codes.
1026 * @param array $languages An array of available language codes.
1027 * @param string $dir The directory where the language files were found.
1029 return apply_filters( 'get_available_languages', $languages, $dir );
1033 * Get installed translations.
1035 * Looks in the wp-content/languages directory for translations of
1036 * plugins or themes.
1040 * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
1041 * @return array Array of language data.
1043 function wp_get_installed_translations( $type ) {
1044 if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' )
1047 $dir = 'core' === $type ? '' : "/$type";
1049 if ( ! is_dir( WP_LANG_DIR ) )
1052 if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) )
1055 $files = scandir( WP_LANG_DIR . $dir );
1059 $language_data = array();
1061 foreach ( $files as $file ) {
1062 if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) {
1065 if ( substr( $file, -3 ) !== '.po' ) {
1068 if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
1071 if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) {
1075 list( , $textdomain, $language ) = $match;
1076 if ( '' === $textdomain ) {
1077 $textdomain = 'default';
1079 $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
1081 return $language_data;
1085 * Extract headers from a PO file.
1089 * @param string $po_file Path to PO file.
1090 * @return array PO file headers.
1092 function wp_get_pomo_file_data( $po_file ) {
1093 $headers = get_file_data( $po_file, array(
1094 'POT-Creation-Date' => '"POT-Creation-Date',
1095 'PO-Revision-Date' => '"PO-Revision-Date',
1096 'Project-Id-Version' => '"Project-Id-Version',
1097 'X-Generator' => '"X-Generator',
1099 foreach ( $headers as $header => $value ) {
1100 // Remove possible contextual '\n' and closing double quote.
1101 $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
1107 * Language selector.
1110 * @since 4.3.0 Introduced the `echo` argument.
1111 * @since 4.7.0 Introduced the `show_option_site_default` argument.
1113 * @see get_available_languages()
1114 * @see wp_get_available_translations()
1116 * @param string|array $args {
1117 * Optional. Array or string of arguments for outputting the language selector.
1119 * @type string $id ID attribute of the select element. Default empty.
1120 * @type string $name Name attribute of the select element. Default empty.
1121 * @type array $languages List of installed languages, contain only the locales.
1122 * Default empty array.
1123 * @type array $translations List of available translations. Default result of
1124 * wp_get_available_translations().
1125 * @type string $selected Language which should be selected. Default empty.
1126 * @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their
1127 * boolean equivalents. Default 1.
1128 * @type bool $show_available_translations Whether to show available translations. Default true.
1129 * @type bool $show_option_site_default Whether to show an option to fall back to the site's locale. Default false.
1131 * @return string HTML content
1133 function wp_dropdown_languages( $args = array() ) {
1135 $args = wp_parse_args( $args, array(
1138 'languages' => array(),
1139 'translations' => array(),
1142 'show_available_translations' => true,
1143 'show_option_site_default' => false,
1146 // English (United States) uses an empty string for the value attribute.
1147 if ( 'en_US' === $args['selected'] ) {
1148 $args['selected'] = '';
1151 $translations = $args['translations'];
1152 if ( empty( $translations ) ) {
1153 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
1154 $translations = wp_get_available_translations();
1158 * $args['languages'] should only contain the locales. Find the locale in
1159 * $translations to get the native name. Fall back to locale.
1161 $languages = array();
1162 foreach ( $args['languages'] as $locale ) {
1163 if ( isset( $translations[ $locale ] ) ) {
1164 $translation = $translations[ $locale ];
1165 $languages[] = array(
1166 'language' => $translation['language'],
1167 'native_name' => $translation['native_name'],
1168 'lang' => current( $translation['iso'] ),
1171 // Remove installed language from available translations.
1172 unset( $translations[ $locale ] );
1174 $languages[] = array(
1175 'language' => $locale,
1176 'native_name' => $locale,
1182 $translations_available = ( ! empty( $translations ) && $args['show_available_translations'] );
1184 $output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
1186 // Holds the HTML markup.
1187 $structure = array();
1189 // List installed languages.
1190 if ( $translations_available ) {
1191 $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
1194 if ( $args['show_option_site_default'] ) {
1195 $structure[] = sprintf(
1196 '<option value="site-default" data-installed="1"%s>%s</option>',
1197 selected( 'site-default', $args['selected'], false ),
1198 _x( 'Site Default', 'default site language' )
1202 $structure[] = sprintf(
1203 '<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
1204 selected( '', $args['selected'], false )
1207 foreach ( $languages as $language ) {
1208 $structure[] = sprintf(
1209 '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
1210 esc_attr( $language['language'] ),
1211 esc_attr( $language['lang'] ),
1212 selected( $language['language'], $args['selected'], false ),
1213 esc_html( $language['native_name'] )
1216 if ( $translations_available ) {
1217 $structure[] = '</optgroup>';
1220 // List available translations.
1221 if ( $translations_available ) {
1222 $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
1223 foreach ( $translations as $translation ) {
1224 $structure[] = sprintf(
1225 '<option value="%s" lang="%s"%s>%s</option>',
1226 esc_attr( $translation['language'] ),
1227 esc_attr( current( $translation['iso'] ) ),
1228 selected( $translation['language'], $args['selected'], false ),
1229 esc_html( $translation['native_name'] )
1232 $structure[] = '</optgroup>';
1235 $output .= join( "\n", $structure );
1237 $output .= '</select>';
1239 if ( $args['echo'] ) {
1247 * Checks if current locale is RTL.
1251 * @global WP_Locale $wp_locale
1253 * @return bool Whether locale is RTL.
1257 if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1260 return $wp_locale->is_rtl();
1264 * Switches the translations according to the given locale.
1268 * @global WP_Locale_Switcher $wp_locale_switcher
1270 * @param string $locale The locale.
1271 * @return bool True on success, false on failure.
1273 function switch_to_locale( $locale ) {
1274 /* @var WP_Locale_Switcher $wp_locale_switcher */
1275 global $wp_locale_switcher;
1277 return $wp_locale_switcher->switch_to_locale( $locale );
1281 * Restores the translations according to the previous locale.
1285 * @global WP_Locale_Switcher $wp_locale_switcher
1287 * @return string|false Locale on success, false on error.
1289 function restore_previous_locale() {
1290 /* @var WP_Locale_Switcher $wp_locale_switcher */
1291 global $wp_locale_switcher;
1293 return $wp_locale_switcher->restore_previous_locale();
1297 * Restores the translations according to the original locale.
1301 * @global WP_Locale_Switcher $wp_locale_switcher
1303 * @return string|false Locale on success, false on error.
1305 function restore_current_locale() {
1306 /* @var WP_Locale_Switcher $wp_locale_switcher */
1307 global $wp_locale_switcher;
1309 return $wp_locale_switcher->restore_current_locale();
1313 * Whether switch_to_locale() is in effect.
1317 * @global WP_Locale_Switcher $wp_locale_switcher
1319 * @return bool True if the locale has been switched, false otherwise.
1321 function is_locale_switched() {
1322 /* @var WP_Locale_Switcher $wp_locale_switcher */
1323 global $wp_locale_switcher;
1325 return $wp_locale_switcher->is_switched();