3 * WordPress Translation API
10 * Get the current locale.
12 * If the locale is set, then it will filter the locale in the 'locale' filter
13 * hook and return the value.
15 * If the locale is not set already, then the WPLANG constant is used if it is
16 * defined. Then it is filtered through the 'locale' filter hook and the value
17 * for the locale global set and the locale is returned.
19 * The process to get the locale should only be done once, but the locale will
20 * always be filtered using the 'locale' hook.
24 * @return string The locale of the blog or from the 'locale' hook.
26 function get_locale() {
27 global $locale, $wp_local_package;
29 if ( isset( $locale ) ) {
31 * Filter WordPress install's locale ID.
35 * @param string $locale The locale ID.
37 return apply_filters( 'locale', $locale );
40 if ( isset( $wp_local_package ) ) {
41 $locale = $wp_local_package;
44 // WPLANG was defined in wp-config.
45 if ( defined( 'WPLANG' ) ) {
49 // If multisite, check options.
50 if ( is_multisite() ) {
51 // Don't check blog option when installing.
52 if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
53 $ms_locale = get_site_option( 'WPLANG' );
56 if ( $ms_locale !== false ) {
60 $db_locale = get_option( 'WPLANG' );
61 if ( $db_locale !== false ) {
66 if ( empty( $locale ) ) {
70 /** This filter is documented in wp-includes/l10n.php */
71 return apply_filters( 'locale', $locale );
75 * Retrieve the translation of $text.
77 * If there is no translation, or the text domain isn't loaded, the original text is returned.
79 * *Note:* Don't use {@see translate()} directly, use `{@see __()} or related functions.
83 * @param string $text Text to translate.
84 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
85 * @return string Translated text
87 function translate( $text, $domain = 'default' ) {
88 $translations = get_translations_for_domain( $domain );
89 $translations = $translations->translate( $text );
92 * Filter text with its translation.
96 * @param string $translations Translated text.
97 * @param string $text Text to translate.
98 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
100 return apply_filters( 'gettext', $translations, $text, $domain );
104 * Remove last item on a pipe-delimited string.
106 * Meant for removing the last item in a string, such as 'Role name|User role'. The original
107 * string will be returned if no pipe '|' characters are found in the string.
111 * @param string $string A pipe-delimited string.
112 * @return string Either $string or everything before the last pipe.
114 function before_last_bar( $string ) {
115 $last_bar = strrpos( $string, '|' );
116 if ( false == $last_bar )
119 return substr( $string, 0, $last_bar );
123 * Retrieve the translation of $text in the context defined in $context.
125 * If there is no translation, or the text domain isn't loaded the original
130 * @param string $text Text to translate.
131 * @param string $context Context information for the translators.
132 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
133 * @return string Translated text on success, original text on failure.
135 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
136 $translations = get_translations_for_domain( $domain );
137 $translations = $translations->translate( $text, $context );
139 * Filter text with its translation based on context information.
143 * @param string $translations Translated text.
144 * @param string $text Text to translate.
145 * @param string $context Context information for the translators.
146 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
148 return apply_filters( 'gettext_with_context', $translations, $text, $context, $domain );
152 * Retrieve the translation of $text. If there is no translation,
153 * or the text domain isn't loaded, the original text is returned.
157 * @param string $text Text to translate.
158 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
159 * @return string Translated text.
161 function __( $text, $domain = 'default' ) {
162 return translate( $text, $domain );
166 * Retrieve the translation of $text and escapes it for safe use in an attribute.
168 * If there is no translation, or the text domain isn't loaded, the original text is returned.
172 * @param string $text Text to translate.
173 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
174 * @return string Translated text on success, original text on failure.
176 function esc_attr__( $text, $domain = 'default' ) {
177 return esc_attr( translate( $text, $domain ) );
181 * Retrieve the translation of $text and escapes it for safe use in HTML output.
183 * If there is no translation, or the text domain isn't loaded, the original text is returned.
187 * @param string $text Text to translate.
188 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
189 * @return string Translated text
191 function esc_html__( $text, $domain = 'default' ) {
192 return esc_html( translate( $text, $domain ) );
196 * Display translated text.
200 * @param string $text Text to translate.
201 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
203 function _e( $text, $domain = 'default' ) {
204 echo translate( $text, $domain );
208 * Display translated text that has been escaped for safe use in an attribute.
212 * @param string $text Text to translate.
213 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
215 function esc_attr_e( $text, $domain = 'default' ) {
216 echo esc_attr( translate( $text, $domain ) );
220 * Display translated text that has been escaped for safe use in HTML output.
224 * @param string $text Text to translate.
225 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
227 function esc_html_e( $text, $domain = 'default' ) {
228 echo esc_html( translate( $text, $domain ) );
232 * Retrieve translated string with gettext context.
234 * Quite a few times, there will be collisions with similar translatable text
235 * found in more than two places, but with different translated context.
237 * By including the context in the pot file, translators can translate the two
238 * strings differently.
242 * @param string $text Text to translate.
243 * @param string $context Context information for the translators.
244 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
245 * @return string Translated context string without pipe.
247 function _x( $text, $context, $domain = 'default' ) {
248 return translate_with_gettext_context( $text, $context, $domain );
252 * Display translated string with gettext context.
256 * @param string $text Text to translate.
257 * @param string $context Context information for the translators.
258 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
259 * @return string Translated context string without pipe.
261 function _ex( $text, $context, $domain = 'default' ) {
262 echo _x( $text, $context, $domain );
266 * Translate string with gettext context, and escapes it for safe use in an attribute.
270 * @param string $text Text to translate.
271 * @param string $context Context information for the translators.
272 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
273 * @return string Translated text
275 function esc_attr_x( $text, $context, $domain = 'default' ) {
276 return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
280 * Translate string with gettext context, and escapes it for safe use in HTML output.
284 * @param string $text Text to translate.
285 * @param string $context Context information for the translators.
286 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
287 * @return string Translated text.
289 function esc_html_x( $text, $context, $domain = 'default' ) {
290 return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
294 * Retrieve the plural or single form based on the supplied amount.
296 * If the text domain is not set in the $l10n list, then a comparison will be made
297 * and either $plural or $single parameters returned.
299 * If the text domain does exist, then the parameters $single, $plural, and $number
300 * will first be passed to the text domain's ngettext method. Then it will be passed
301 * to the 'ngettext' filter hook along with the same parameters. The expected
302 * type will be a string.
306 * @param string $single The text that will be used if $number is 1.
307 * @param string $plural The text that will be used if $number is not 1.
308 * @param int $number The number to compare against to use either $single or $plural.
309 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
310 * @return string Either $single or $plural translated text.
312 function _n( $single, $plural, $number, $domain = 'default' ) {
313 $translations = get_translations_for_domain( $domain );
314 $translation = $translations->translate_plural( $single, $plural, $number );
316 * Filter text with its translation when plural option is available.
320 * @param string $translation Translated text.
321 * @param string $single The text that will be used if $number is 1.
322 * @param string $plural The text that will be used if $number is not 1.
323 * @param string $number The number to compare against to use either $single or $plural.
324 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
326 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
330 * Retrieve the plural or single form based on the supplied amount with gettext context.
332 * This is a hybrid of _n() and _x(). It supports contexts and plurals.
336 * @param string $single The text that will be used if $number is 1.
337 * @param string $plural The text that will be used if $number is not 1.
338 * @param int $number The number to compare against to use either $single or $plural.
339 * @param string $context Context information for the translators.
340 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
341 * @return string Either $single or $plural translated text with context.
343 function _nx($single, $plural, $number, $context, $domain = 'default') {
344 $translations = get_translations_for_domain( $domain );
345 $translation = $translations->translate_plural( $single, $plural, $number, $context );
347 * Filter text with its translation while plural option and context are available.
351 * @param string $translation Translated text.
352 * @param string $single The text that will be used if $number is 1.
353 * @param string $plural The text that will be used if $number is not 1.
354 * @param string $number The number to compare against to use either $single or $plural.
355 * @param string $context Context information for the translators.
356 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
358 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
362 * Register plural strings in POT file, but don't translate them.
364 * Used when you want to keep structures with translatable plural
365 * strings and use them later.
370 * 'post' => _n_noop( '%s post', '%s posts' ),
371 * 'page' => _n_noop( '%s pages', '%s pages' ),
374 * $message = $messages[ $type ];
375 * $usable_text = sprintf( translate_nooped_plural( $message, $count ), $count );
379 * @param string $singular Single form to be i18ned.
380 * @param string $plural Plural form to be i18ned.
381 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
382 * @return array array($singular, $plural)
384 function _n_noop( $singular, $plural, $domain = null ) {
385 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain );
389 * Register plural strings with context in POT file, but don't translate them.
392 * @param string $singular
393 * @param string $plural
394 * @param string $context
395 * @param string|null $domain
398 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 );
403 * Translate the result of _n_noop() or _nx_noop().
407 * @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
409 * @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.
411 * @return string Either $single or $plural translated text.
413 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
414 if ( $nooped_plural['domain'] )
415 $domain = $nooped_plural['domain'];
417 if ( $nooped_plural['context'] )
418 return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
420 return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
424 * Load a .mo file into the text domain $domain.
426 * If the text domain already exists, the translations will be merged. If both
427 * sets have the same string, the translation from the original value will be taken.
429 * On success, the .mo file will be placed in the $l10n global by $domain
430 * and will be a MO object.
434 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
435 * @param string $mofile Path to the .mo file.
436 * @return bool True on success, false on failure.
438 function load_textdomain( $domain, $mofile ) {
442 * Filter text domain and/or MO file path for loading translations.
446 * @param bool $override Whether to override the text domain. Default false.
447 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
448 * @param string $mofile Path to the MO file.
450 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
452 if ( true == $plugin_override ) {
457 * Fires before the MO translation file is loaded.
461 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
462 * @param string $mofile Path to the .mo file.
464 do_action( 'load_textdomain', $domain, $mofile );
467 * Filter MO file path for loading translations for a specific text domain.
471 * @param string $mofile Path to the MO file.
472 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
474 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
476 if ( !is_readable( $mofile ) ) return false;
479 if ( !$mo->import_from_file( $mofile ) ) return false;
481 if ( isset( $l10n[$domain] ) )
482 $mo->merge_with( $l10n[$domain] );
484 $l10n[$domain] = &$mo;
490 * Unload translations for a text domain.
494 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
495 * @return bool Whether textdomain was unloaded.
497 function unload_textdomain( $domain ) {
501 * Filter the text domain for loading translation.
505 * @param bool $override Whether to override unloading the text domain. Default false.
506 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
508 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
510 if ( $plugin_override )
514 * Fires before the text domain is unloaded.
518 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
520 do_action( 'unload_textdomain', $domain );
522 if ( isset( $l10n[$domain] ) ) {
523 unset( $l10n[$domain] );
531 * Load default translated strings based on locale.
533 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
534 * The translated (.mo) file is named based on the locale.
536 * @see load_textdomain()
540 * @param string $locale Optional. Locale to load. Default is the value of {@see get_locale()}.
541 * @return bool Whether the textdomain was loaded.
543 function load_default_textdomain( $locale = null ) {
544 if ( null === $locale ) {
545 $locale = get_locale();
548 // Unload previously loaded strings so we can switch translations.
549 unload_textdomain( 'default' );
551 $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
553 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" );
558 if ( is_admin() || defined( 'WP_INSTALLING' ) || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
559 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
562 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
563 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
569 * Load a plugin's translated strings.
571 * If the path is not given then it will be the root of the plugin directory.
573 * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
577 * @param string $domain Unique identifier for retrieving translated strings
578 * @param string $deprecated Use the $plugin_rel_path parameter instead.
579 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
581 * @return bool True when textdomain is successfully loaded, false otherwise.
583 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
584 $locale = get_locale();
586 * Filter a plugin's locale.
590 * @param string $locale The plugin's current locale.
591 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
593 $locale = apply_filters( 'plugin_locale', $locale, $domain );
595 if ( false !== $plugin_rel_path ) {
596 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
597 } elseif ( false !== $deprecated ) {
598 _deprecated_argument( __FUNCTION__, '2.7' );
599 $path = ABSPATH . trim( $deprecated, '/' );
601 $path = WP_PLUGIN_DIR;
604 // Load the textdomain according to the plugin first
605 $mofile = $domain . '-' . $locale . '.mo';
606 if ( $loaded = load_textdomain( $domain, $path . '/'. $mofile ) )
609 // Otherwise, load from the languages directory
610 $mofile = WP_LANG_DIR . '/plugins/' . $mofile;
611 return load_textdomain( $domain, $mofile );
615 * Load the translated strings for a plugin residing in the mu-plugins directory.
619 * @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.
621 * Default empty string.
622 * @return bool True when textdomain is successfully loaded, false otherwise.
624 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
625 /** This filter is documented in wp-includes/l10n.php */
626 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
627 $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) );
629 // Load the textdomain according to the plugin first
630 $mofile = $domain . '-' . $locale . '.mo';
631 if ( $loaded = load_textdomain( $domain, $path . $mofile ) )
634 // Otherwise, load from the languages directory
635 $mofile = WP_LANG_DIR . '/plugins/' . $mofile;
636 return load_textdomain( $domain, $mofile );
640 * Load the theme's translated strings.
642 * If the current locale exists as a .mo file in the theme's root directory, it
643 * will be included in the translated strings by the $domain.
645 * The .mo files must be named based on the locale exactly.
649 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
650 * @param string $path Optional. Path to the directory containing the .mo file.
652 * @return bool True when textdomain is successfully loaded, false otherwise.
654 function load_theme_textdomain( $domain, $path = false ) {
655 $locale = get_locale();
657 * Filter a theme's locale.
661 * @param string $locale The theme's current locale.
662 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
664 $locale = apply_filters( 'theme_locale', $locale, $domain );
667 $path = get_template_directory();
669 // Load the textdomain according to the theme
670 $mofile = untrailingslashit( $path ) . "/{$locale}.mo";
671 if ( $loaded = load_textdomain( $domain, $mofile ) )
674 // Otherwise, load from the languages directory
675 $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
676 return load_textdomain( $domain, $mofile );
680 * Load the child themes translated strings.
682 * If the current locale exists as a .mo file in the child themes
683 * root directory, it will be included in the translated strings by the $domain.
685 * The .mo files must be named based on the locale exactly.
689 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
690 * @param string $path Optional. Path to the directory containing the .mo file.
692 * @return bool True when the theme textdomain is successfully loaded, false otherwise.
694 function load_child_theme_textdomain( $domain, $path = false ) {
696 $path = get_stylesheet_directory();
697 return load_theme_textdomain( $domain, $path );
701 * Return the Translations instance for a text domain.
703 * If there isn't one, returns empty Translations instance.
707 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
708 * @return NOOP_Translations A Translations instance.
710 function get_translations_for_domain( $domain ) {
712 if ( !isset( $l10n[$domain] ) ) {
713 $l10n[$domain] = new NOOP_Translations;
715 return $l10n[$domain];
719 * Whether there are translations for the text domain.
723 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
724 * @return bool Whether there are translations.
726 function is_textdomain_loaded( $domain ) {
728 return isset( $l10n[$domain] );
732 * Translates role name.
734 * 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
736 * properly translates them back.
738 * The before_last_bar() call is needed, because older installs keep the roles
739 * 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
741 * won't suffer from that problem.
745 * @param string $name The role name.
746 * @return string Translated role name on success, original name on failure.
748 function translate_user_role( $name ) {
749 return translate_with_gettext_context( before_last_bar($name), 'User role' );
753 * Get all available languages based on the presence of *.mo files in a given directory.
755 * The default directory is WP_LANG_DIR.
759 * @param string $dir A directory to search for language files.
760 * Default WP_LANG_DIR.
761 * @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.
763 function get_available_languages( $dir = null ) {
764 $languages = array();
766 foreach( (array)glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ) as $lang_file ) {
767 $lang_file = basename($lang_file, '.mo');
768 if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
769 0 !== strpos( $lang_file, 'admin-' ))
770 $languages[] = $lang_file;
777 * Get installed translations.
779 * Looks in the wp-content/languages directory for translations of
784 * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
785 * @return array Array of language data.
787 function wp_get_installed_translations( $type ) {
788 if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' )
791 $dir = 'core' === $type ? '' : "/$type";
793 if ( ! is_dir( WP_LANG_DIR ) )
796 if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) )
799 $files = scandir( WP_LANG_DIR . $dir );
803 $language_data = array();
805 foreach ( $files as $file ) {
806 if ( '.' === $file[0] || is_dir( $file ) ) {
809 if ( substr( $file, -3 ) !== '.po' ) {
812 if ( ! preg_match( '/(?:(.+)-)?([A-Za-z_]{2,6}).po/', $file, $match ) ) {
815 if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) {
819 list( , $textdomain, $language ) = $match;
820 if ( '' === $textdomain ) {
821 $textdomain = 'default';
823 $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
825 return $language_data;
829 * Extract headers from a PO file.
833 * @param string $po_file Path to PO file.
834 * @return array PO file headers.
836 function wp_get_pomo_file_data( $po_file ) {
837 $headers = get_file_data( $po_file, array(
838 'POT-Creation-Date' => '"POT-Creation-Date',
839 'PO-Revision-Date' => '"PO-Revision-Date',
840 'Project-Id-Version' => '"Project-Id-Version',
841 'X-Generator' => '"X-Generator',
843 foreach ( $headers as $header => $value ) {
844 // Remove possible contextual '\n' and closing double quote.
845 $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
855 * @see get_available_languages()
856 * @see wp_get_available_translations()
858 * @param string|array $args {
859 * Optional. Array or string of arguments for outputting the language selector.
861 * @type string $id ID attribute of the select element. Default empty.
862 * @type string $name Name attribute of the select element. Default empty.
863 * @type array $languages List of installed languages, contain only the locales.
864 * Default empty array.
865 * @type array $translations List of available translations. Default result of
866 * {@see wp_get_available_translations()}.
867 * @type string $selected Language which should be selected. Default empty.
868 * @type bool $show_available_translations Whether to show available translations. Default true.
871 function wp_dropdown_languages( $args = array() ) {
873 $args = wp_parse_args( $args, array(
876 'languages' => array(),
877 'translations' => array(),
879 'show_available_translations' => true,
882 $translations = $args['translations'];
883 if ( empty( $translations ) ) {
884 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
885 $translations = wp_get_available_translations();
889 * $args['languages'] should only contain the locales. Find the locale in
890 * $translations to get the native name. Fall back to locale.
892 $languages = array();
893 foreach ( $args['languages'] as $locale ) {
894 if ( isset( $translations[ $locale ] ) ) {
895 $translation = $translations[ $locale ];
896 $languages[] = array(
897 'language' => $translation['language'],
898 'native_name' => $translation['native_name'],
899 'lang' => current( $translation['iso'] ),
902 // Remove installed language from available translations.
903 unset( $translations[ $locale ] );
905 $languages[] = array(
906 'language' => $locale,
907 'native_name' => $locale,
913 $translations_available = ( ! empty( $translations ) && $args['show_available_translations'] );
915 printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
917 // Holds the HTML markup.
918 $structure = array();
920 // List installed languages.
921 if ( $translations_available ) {
922 $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
924 $structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
925 foreach ( $languages as $language ) {
926 $structure[] = sprintf(
927 '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
928 esc_attr( $language['language'] ),
929 esc_attr( $language['lang'] ),
930 selected( $language['language'], $args['selected'], false ),
931 esc_html( $language['native_name'] )
934 if ( $translations_available ) {
935 $structure[] = '</optgroup>';
938 // List available translations.
939 if ( $translations_available ) {
940 $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
941 foreach ( $translations as $translation ) {
942 $structure[] = sprintf(
943 '<option value="%s" lang="%s"%s>%s</option>',
944 esc_attr( $translation['language'] ),
945 esc_attr( current( $translation['iso'] ) ),
946 selected( $translation['language'], $args['selected'], false ),
947 esc_html( $translation['native_name'] )
950 $structure[] = '</optgroup>';
953 echo join( "\n", $structure );