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 * Filter 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 * Retrieve the translation of $text.
81 * If there is no translation, or the text domain isn't loaded, the original text is returned.
83 * *Note:* Don't use translate() directly, use __() or related functions.
87 * @param string $text Text to translate.
88 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
90 * @return string Translated text
92 function translate( $text, $domain = 'default' ) {
93 $translations = get_translations_for_domain( $domain );
94 $translations = $translations->translate( $text );
97 * Filter text with its translation.
101 * @param string $translations Translated text.
102 * @param string $text Text to translate.
103 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
105 return apply_filters( 'gettext', $translations, $text, $domain );
109 * Remove last item on a pipe-delimited string.
111 * Meant for removing the last item in a string, such as 'Role name|User role'. The original
112 * string will be returned if no pipe '|' characters are found in the string.
116 * @param string $string A pipe-delimited string.
117 * @return string Either $string or everything before the last pipe.
119 function before_last_bar( $string ) {
120 $last_bar = strrpos( $string, '|' );
121 if ( false === $last_bar )
124 return substr( $string, 0, $last_bar );
128 * Retrieve the translation of $text in the context defined in $context.
130 * If there is no translation, or the text domain isn't loaded the original
133 * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.
137 * @param string $text Text to translate.
138 * @param string $context Context information for the translators.
139 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
141 * @return string Translated text on success, original text on failure.
143 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
144 $translations = get_translations_for_domain( $domain );
145 $translations = $translations->translate( $text, $context );
147 * Filter text with its translation based on context information.
151 * @param string $translations Translated text.
152 * @param string $text Text to translate.
153 * @param string $context Context information for the translators.
154 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
156 return apply_filters( 'gettext_with_context', $translations, $text, $context, $domain );
160 * Retrieve the translation of $text.
162 * If there is no translation, or the text domain isn't loaded, the original text is returned.
166 * @param string $text Text to translate.
167 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
169 * @return string Translated text.
171 function __( $text, $domain = 'default' ) {
172 return translate( $text, $domain );
176 * Retrieve the translation of $text and escapes it for safe use in an attribute.
178 * If there is no translation, or the text domain isn't loaded, the original text is returned.
182 * @param string $text Text to translate.
183 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
185 * @return string Translated text on success, original text on failure.
187 function esc_attr__( $text, $domain = 'default' ) {
188 return esc_attr( translate( $text, $domain ) );
192 * Retrieve the translation of $text and escapes it for safe use in HTML output.
194 * If there is no translation, or the text domain isn't loaded, the original text is returned.
198 * @param string $text Text to translate.
199 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
201 * @return string Translated text
203 function esc_html__( $text, $domain = 'default' ) {
204 return esc_html( translate( $text, $domain ) );
208 * Display translated text.
212 * @param string $text Text to translate.
213 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
216 function _e( $text, $domain = 'default' ) {
217 echo translate( $text, $domain );
221 * Display translated text that has been escaped for safe use in an attribute.
225 * @param string $text Text to translate.
226 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
229 function esc_attr_e( $text, $domain = 'default' ) {
230 echo esc_attr( translate( $text, $domain ) );
234 * Display translated text that has been escaped for safe use in HTML output.
238 * @param string $text Text to translate.
239 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
242 function esc_html_e( $text, $domain = 'default' ) {
243 echo esc_html( translate( $text, $domain ) );
247 * Retrieve translated string with gettext context.
249 * Quite a few times, there will be collisions with similar translatable text
250 * found in more than two places, but with different translated context.
252 * By including the context in the pot file, translators can translate the two
253 * strings differently.
257 * @param string $text Text to translate.
258 * @param string $context Context information for the translators.
259 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
261 * @return string Translated context string without pipe.
263 function _x( $text, $context, $domain = 'default' ) {
264 return translate_with_gettext_context( $text, $context, $domain );
268 * Display translated string with gettext context.
272 * @param string $text Text to translate.
273 * @param string $context Context information for the translators.
274 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
276 * @return string Translated context string without pipe.
278 function _ex( $text, $context, $domain = 'default' ) {
279 echo _x( $text, $context, $domain );
283 * Translate string with gettext context, and escapes it for safe use in an attribute.
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 text
293 function esc_attr_x( $text, $context, $domain = 'default' ) {
294 return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
298 * Translate string with gettext context, and escapes it for safe use in HTML output.
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 text.
308 function esc_html_x( $text, $context, $domain = 'default' ) {
309 return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
313 * Translates and retrieves the singular or plural form based on the supplied number.
315 * Used when you want to use the appropriate form of a string based on whether a
316 * number is singular or plural.
320 * $people = sprintf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );
324 * @param string $single The text to be used if the number is singular.
325 * @param string $plural The text to be used if the number is plural.
326 * @param int $number The number to compare against to use either the singular or plural form.
327 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
329 * @return string The translated singular or plural form.
331 function _n( $single, $plural, $number, $domain = 'default' ) {
332 $translations = get_translations_for_domain( $domain );
333 $translation = $translations->translate_plural( $single, $plural, $number );
336 * Filter the singular or plural form of a string.
340 * @param string $translation Translated text.
341 * @param string $single The text to be used if the number is singular.
342 * @param string $plural The text to be used if the number is plural.
343 * @param string $number The number to compare against to use either the singular or plural form.
344 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
346 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
350 * Translates and retrieves the singular or plural form based on the supplied number, with gettext context.
352 * This is a hybrid of _n() and _x(). It supports context and plurals.
354 * Used when you want to use the appropriate form of a string with context based on whether a
355 * number is singular or plural.
359 * $people = sprintf( _n( '%s person', '%s people', $count, 'context', 'text-domain' ), number_format_i18n( $count ) );
363 * @param string $single The text to be used if the number is singular.
364 * @param string $plural The text to be used if the number is plural.
365 * @param int $number The number to compare against to use either the singular or plural form.
366 * @param string $context Context information for the translators.
367 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
369 * @return string The translated singular or plural form.
371 function _nx($single, $plural, $number, $context, $domain = 'default') {
372 $translations = get_translations_for_domain( $domain );
373 $translation = $translations->translate_plural( $single, $plural, $number, $context );
376 * Filter the singular or plural form of a string with gettext context.
380 * @param string $translation Translated text.
381 * @param string $single The text to be used if the number is singular.
382 * @param string $plural The text to be used if the number is plural.
383 * @param string $number The number to compare against to use either the singular or plural form.
384 * @param string $context Context information for the translators.
385 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
387 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
391 * Registers plural strings in POT file, but don't translate them.
393 * Used when you want to keep structures with translatable plural
394 * strings and use them later when the number is known.
399 * 'post' => _n_noop( '%s post', '%s posts', 'text-domain' ),
400 * 'page' => _n_noop( '%s pages', '%s pages', 'text-domain' ),
403 * $message = $messages[ $type ];
404 * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
408 * @param string $singular Singular form to be localized.
409 * @param string $plural Plural form to be localized.
410 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
413 * Array of translation information for the strings.
415 * @type string $0 Singular form to be localized. No longer used.
416 * @type string $1 Plural form to be localized. No longer used.
417 * @type string $singular Singular form to be localized.
418 * @type string $plural Plural form to be localized.
419 * @type null $context Context information for the translators.
420 * @type string $domain Text domain.
423 function _n_noop( $singular, $plural, $domain = null ) {
424 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain );
428 * Register plural strings with gettext context in the POT file, but don't translate them.
430 * Used when you want to keep structures with translatable plural
431 * strings and use them later when the number is known.
436 * 'post' => _n_noop( '%s post', '%s posts', 'context', 'text-domain' ),
437 * 'page' => _n_noop( '%s pages', '%s pages', 'context', 'text-domain' ),
440 * $message = $messages[ $type ];
441 * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
445 * @param string $singular Singular form to be localized.
446 * @param string $plural Plural form to be localized.
447 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
450 * Array of translation information for the strings.
452 * @type string $0 Singular form to be localized. No longer used.
453 * @type string $1 Plural form to be localized. No longer used.
454 * @type string $2 Context information for the translators. No longer used.
455 * @type string $singular Singular form to be localized.
456 * @type string $plural Plural form to be localized.
457 * @type string $context Context information for the translators.
458 * @type string $domain Text domain.
461 function _nx_noop( $singular, $plural, $context, $domain = null ) {
462 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain );
466 * Translates and retrieves the singular or plural form of a string that's been registered
467 * with _n_noop() or _nx_noop().
469 * Used when you want to use a translatable plural string once the number is known.
474 * 'post' => _n_noop( '%s post', '%s posts', 'text-domain' ),
475 * 'page' => _n_noop( '%s pages', '%s pages', 'text-domain' ),
478 * $message = $messages[ $type ];
479 * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
483 * @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
484 * @param int $count Number of objects.
485 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
486 * a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
487 * @return string Either $single or $plural translated text.
489 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
490 if ( $nooped_plural['domain'] )
491 $domain = $nooped_plural['domain'];
493 if ( $nooped_plural['context'] )
494 return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
496 return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
500 * Load a .mo file into the text domain $domain.
502 * If the text domain already exists, the translations will be merged. If both
503 * sets have the same string, the translation from the original value will be taken.
505 * On success, the .mo file will be placed in the $l10n global by $domain
506 * and will be a MO object.
510 * @global array $l10n
512 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
513 * @param string $mofile Path to the .mo file.
514 * @return bool True on success, false on failure.
516 function load_textdomain( $domain, $mofile ) {
520 * Filter text domain and/or MO file path for loading translations.
524 * @param bool $override Whether to override the text domain. Default false.
525 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
526 * @param string $mofile Path to the MO file.
528 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
530 if ( true == $plugin_override ) {
535 * Fires before the MO translation file is loaded.
539 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
540 * @param string $mofile Path to the .mo file.
542 do_action( 'load_textdomain', $domain, $mofile );
545 * Filter MO file path for loading translations for a specific text domain.
549 * @param string $mofile Path to the MO file.
550 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
552 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
554 if ( !is_readable( $mofile ) ) return false;
557 if ( !$mo->import_from_file( $mofile ) ) return false;
559 if ( isset( $l10n[$domain] ) )
560 $mo->merge_with( $l10n[$domain] );
562 $l10n[$domain] = &$mo;
568 * Unload translations for a text domain.
572 * @global array $l10n
574 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
575 * @return bool Whether textdomain was unloaded.
577 function unload_textdomain( $domain ) {
581 * Filter the text domain for loading translation.
585 * @param bool $override Whether to override unloading the text domain. Default false.
586 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
588 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
590 if ( $plugin_override )
594 * Fires before the text domain is unloaded.
598 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
600 do_action( 'unload_textdomain', $domain );
602 if ( isset( $l10n[$domain] ) ) {
603 unset( $l10n[$domain] );
611 * Load default translated strings based on locale.
613 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
614 * The translated (.mo) file is named based on the locale.
616 * @see load_textdomain()
620 * @param string $locale Optional. Locale to load. Default is the value of {@see get_locale()}.
621 * @return bool Whether the textdomain was loaded.
623 function load_default_textdomain( $locale = null ) {
624 if ( null === $locale ) {
625 $locale = get_locale();
628 // Unload previously loaded strings so we can switch translations.
629 unload_textdomain( 'default' );
631 $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
633 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
634 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
638 if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
639 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
642 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
643 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
649 * Load a plugin's translated strings.
651 * If the path is not given then it will be the root of the plugin directory.
653 * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
657 * @param string $domain Unique identifier for retrieving translated strings
658 * @param string $deprecated Use the $plugin_rel_path parameter instead.
659 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
661 * @return bool True when textdomain is successfully loaded, false otherwise.
663 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
664 $locale = get_locale();
666 * Filter a plugin's locale.
670 * @param string $locale The plugin's current locale.
671 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
673 $locale = apply_filters( 'plugin_locale', $locale, $domain );
675 if ( false !== $plugin_rel_path ) {
676 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
677 } elseif ( false !== $deprecated ) {
678 _deprecated_argument( __FUNCTION__, '2.7' );
679 $path = ABSPATH . trim( $deprecated, '/' );
681 $path = WP_PLUGIN_DIR;
684 // Load the textdomain according to the plugin first
685 $mofile = $domain . '-' . $locale . '.mo';
686 if ( $loaded = load_textdomain( $domain, $path . '/'. $mofile ) )
689 // Otherwise, load from the languages directory
690 $mofile = WP_LANG_DIR . '/plugins/' . $mofile;
691 return load_textdomain( $domain, $mofile );
695 * Load the translated strings for a plugin residing in the mu-plugins directory.
699 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
700 * @param string $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which the .mo file resides.
701 * Default empty string.
702 * @return bool True when textdomain is successfully loaded, false otherwise.
704 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
705 /** This filter is documented in wp-includes/l10n.php */
706 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
707 $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) );
709 // Load the textdomain according to the plugin first
710 $mofile = $domain . '-' . $locale . '.mo';
711 if ( $loaded = load_textdomain( $domain, $path . $mofile ) )
714 // Otherwise, load from the languages directory
715 $mofile = WP_LANG_DIR . '/plugins/' . $mofile;
716 return load_textdomain( $domain, $mofile );
720 * Load the theme's translated strings.
722 * If the current locale exists as a .mo file in the theme's root directory, it
723 * will be included in the translated strings by the $domain.
725 * The .mo files must be named based on the locale exactly.
729 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
730 * @param string $path Optional. Path to the directory containing the .mo file.
732 * @return bool True when textdomain is successfully loaded, false otherwise.
734 function load_theme_textdomain( $domain, $path = false ) {
735 $locale = get_locale();
737 * Filter a theme's locale.
741 * @param string $locale The theme's current locale.
742 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
744 $locale = apply_filters( 'theme_locale', $locale, $domain );
747 $path = get_template_directory();
749 // Load the textdomain according to the theme
750 $mofile = untrailingslashit( $path ) . "/{$locale}.mo";
751 if ( $loaded = load_textdomain( $domain, $mofile ) )
754 // Otherwise, load from the languages directory
755 $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
756 return load_textdomain( $domain, $mofile );
760 * Load the child themes translated strings.
762 * If the current locale exists as a .mo file in the child themes
763 * root directory, it will be included in the translated strings by the $domain.
765 * The .mo files must be named based on the locale exactly.
769 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
770 * @param string $path Optional. Path to the directory containing the .mo file.
772 * @return bool True when the theme textdomain is successfully loaded, false otherwise.
774 function load_child_theme_textdomain( $domain, $path = false ) {
776 $path = get_stylesheet_directory();
777 return load_theme_textdomain( $domain, $path );
781 * Return the Translations instance for a text domain.
783 * If there isn't one, returns empty Translations instance.
787 * @global array $l10n
789 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
790 * @return NOOP_Translations A Translations instance.
792 function get_translations_for_domain( $domain ) {
794 if ( !isset( $l10n[$domain] ) ) {
795 $l10n[$domain] = new NOOP_Translations;
797 return $l10n[$domain];
801 * Whether there are translations for the text domain.
805 * @global array $l10n
807 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
808 * @return bool Whether there are translations.
810 function is_textdomain_loaded( $domain ) {
812 return isset( $l10n[$domain] );
816 * Translates role name.
818 * Since the role names are in the database and not in the source there
819 * are dummy gettext calls to get them into the POT file and this function
820 * properly translates them back.
822 * The before_last_bar() call is needed, because older installs keep the roles
823 * using the old context format: 'Role name|User role' and just skipping the
824 * content after the last bar is easier than fixing them in the DB. New installs
825 * won't suffer from that problem.
829 * @param string $name The role name.
830 * @return string Translated role name on success, original name on failure.
832 function translate_user_role( $name ) {
833 return translate_with_gettext_context( before_last_bar($name), 'User role' );
837 * Get all available languages based on the presence of *.mo files in a given directory.
839 * The default directory is WP_LANG_DIR.
843 * @param string $dir A directory to search for language files.
844 * Default WP_LANG_DIR.
845 * @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.
847 function get_available_languages( $dir = null ) {
848 $languages = array();
850 $lang_files = glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' );
852 foreach ( $lang_files as $lang_file ) {
853 $lang_file = basename( $lang_file, '.mo' );
854 if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
855 0 !== strpos( $lang_file, 'admin-' ) ) {
856 $languages[] = $lang_file;
865 * Get installed translations.
867 * Looks in the wp-content/languages directory for translations of
872 * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
873 * @return array Array of language data.
875 function wp_get_installed_translations( $type ) {
876 if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' )
879 $dir = 'core' === $type ? '' : "/$type";
881 if ( ! is_dir( WP_LANG_DIR ) )
884 if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) )
887 $files = scandir( WP_LANG_DIR . $dir );
891 $language_data = array();
893 foreach ( $files as $file ) {
894 if ( '.' === $file[0] || is_dir( $file ) ) {
897 if ( substr( $file, -3 ) !== '.po' ) {
900 if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
903 if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) {
907 list( , $textdomain, $language ) = $match;
908 if ( '' === $textdomain ) {
909 $textdomain = 'default';
911 $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
913 return $language_data;
917 * Extract headers from a PO file.
921 * @param string $po_file Path to PO file.
922 * @return array PO file headers.
924 function wp_get_pomo_file_data( $po_file ) {
925 $headers = get_file_data( $po_file, array(
926 'POT-Creation-Date' => '"POT-Creation-Date',
927 'PO-Revision-Date' => '"PO-Revision-Date',
928 'Project-Id-Version' => '"Project-Id-Version',
929 'X-Generator' => '"X-Generator',
931 foreach ( $headers as $header => $value ) {
932 // Remove possible contextual '\n' and closing double quote.
933 $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
942 * @since 4.3.0 Introduced the `echo` argument.
944 * @see get_available_languages()
945 * @see wp_get_available_translations()
947 * @param string|array $args {
948 * Optional. Array or string of arguments for outputting the language selector.
950 * @type string $id ID attribute of the select element. Default empty.
951 * @type string $name Name attribute of the select element. Default empty.
952 * @type array $languages List of installed languages, contain only the locales.
953 * Default empty array.
954 * @type array $translations List of available translations. Default result of
955 * wp_get_available_translations().
956 * @type string $selected Language which should be selected. Default empty.
957 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their
958 * bool equivalents. Default 1.
959 * @type bool $show_available_translations Whether to show available translations. Default true.
961 * @return string HTML content only if 'echo' argument is 0.
963 function wp_dropdown_languages( $args = array() ) {
965 $args = wp_parse_args( $args, array(
968 'languages' => array(),
969 'translations' => array(),
972 'show_available_translations' => true,
975 $translations = $args['translations'];
976 if ( empty( $translations ) ) {
977 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
978 $translations = wp_get_available_translations();
982 * $args['languages'] should only contain the locales. Find the locale in
983 * $translations to get the native name. Fall back to locale.
985 $languages = array();
986 foreach ( $args['languages'] as $locale ) {
987 if ( isset( $translations[ $locale ] ) ) {
988 $translation = $translations[ $locale ];
989 $languages[] = array(
990 'language' => $translation['language'],
991 'native_name' => $translation['native_name'],
992 'lang' => current( $translation['iso'] ),
995 // Remove installed language from available translations.
996 unset( $translations[ $locale ] );
998 $languages[] = array(
999 'language' => $locale,
1000 'native_name' => $locale,
1006 $translations_available = ( ! empty( $translations ) && $args['show_available_translations'] );
1008 $output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
1010 // Holds the HTML markup.
1011 $structure = array();
1013 // List installed languages.
1014 if ( $translations_available ) {
1015 $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
1017 $structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
1018 foreach ( $languages as $language ) {
1019 $structure[] = sprintf(
1020 '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
1021 esc_attr( $language['language'] ),
1022 esc_attr( $language['lang'] ),
1023 selected( $language['language'], $args['selected'], false ),
1024 esc_html( $language['native_name'] )
1027 if ( $translations_available ) {
1028 $structure[] = '</optgroup>';
1031 // List available translations.
1032 if ( $translations_available ) {
1033 $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
1034 foreach ( $translations as $translation ) {
1035 $structure[] = sprintf(
1036 '<option value="%s" lang="%s"%s>%s</option>',
1037 esc_attr( $translation['language'] ),
1038 esc_attr( current( $translation['iso'] ) ),
1039 selected( $translation['language'], $args['selected'], false ),
1040 esc_html( $translation['native_name'] )
1043 $structure[] = '</optgroup>';
1046 $output .= join( "\n", $structure );
1048 $output .= '</select>';
1050 if ( $args['echo'] ) {