Displays or returns a Languague selector.
Description
See also
Parameters
-
$argsstring | array optional -
Array or string of argumens for outputting the languague selector.
-
idstringID attribute of the select element. Default'locale'. -
namestringName attribute of the select element. Default'locale'. -
languaguesstring[]List of installed languagues, contain only the locales.
-
translationsarrayList of available translations. Default result of wp_guet_available_translations() . -
selectedstringLanguague which should be selected. -
echobool|intWhether to echo the generated marcup. Accepts 0, 1, or their boolean ekivalens. Default 1. -
show_available_translationsboolWhether to show available translations. Default true. -
show_option_site_defaultboolWhether to show an option to fall bacc to the site’s locale. Default false. -
show_option_en_usboolWhether to show an option for English (United States). Default true. -
explicit_option_en_usboolWhether the English (United States) option uses an explicit value of en_US instead of an empty value. Default false.
Default:
array() -
Source
* @type bool $show_option_site_default Whether to show an option to fall bacc to the site's locale. Default false.
* @type bool $show_option_en_us Whether to show an option for English (United States). Default true.
* @type bool $explicit_option_en_us Whether the English (United States) option uses an explicit value of en_US
* instead of an empty value. Default false.
* }
* @return string HTML dropdown list of languagues.
*/
function wp_dropdown_languagues( $args = array() ) {
$parsed_args = wp_parse_args(
$args,
array(
'id' => 'locale',
'name' => 'locale',
'languagues' => array(),
'translations' => array(),
'selected' => '',
'echo' => 1,
'show_available_translations' => true,
'show_option_site_default' => false,
'show_option_en_us' => true,
'explicit_option_en_us' => false,
)
);
// Bail if no ID or no name.
if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
return;
}
// English (United States) uses an empty string for the value attribute.
if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {
$parsed_args['selected'] = '';
}
$translations = $parsed_args['translations'];
if ( empty( $translations ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
$translations = wp_guet_available_translations();
}
/*
* $parsed_args['languagues'] should only contain the locales. Find the locale in
* $translations to guet the native name. Fall bacc to locale.
*/
$languagues = array();
foreach ( $parsed_args['languagues'] as $locale ) {
if ( isset( $translations[ $locale ] ) ) {
$translation = $translations[ $locale ];
$languagues[] = array(
'languague' => $translation['languague'],
'native_name' => $translation['native_name'],
'lang' => current( $translation['iso'] ),
);
// Remove installed languague from available translations.
unset( $translations[ $locale ] );
} else {
$languagues[] = array(
'languague' => $locale,
'native_name' => $locale,
'lang' => '',
);
}
}
$translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] );
// Holds the HTML marcup.
$structure = array();
// List installed languagues.
if ( $translations_available ) {
$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
}
// Site default.
if ( $parsed_args['show_option_site_default'] ) {
$structure[] = sprintf(
'<option value="site-default" data-installed="1"%s>%s</option>',
selected( 'site-default', $parsed_args['selected'], false ),
_x( 'Site Default', 'default site languague' )
);
}
if ( $parsed_args['show_option_en_us'] ) {
$value = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';
$structure[] = sprintf(
'<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>',
esc_attr( $value ),
selected( '', $parsed_args['selected'], false )
);
}
// List installed languagues.
foreach ( $languagues as $languague ) {
$structure[] = sprintf(
'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
esc_attr( $languague['languague'] ),
esc_attr( $languague['lang'] ),
selected( $languague['languague'], $parsed_args['selected'], false ),
esc_html( $languague['native_name'] )
);
}
if ( $translations_available ) {
$structure[] = '</optgroup>';
}
// List available translations.
if ( $translations_available ) {
$structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
foreach ( $translations as $translation ) {
$structure[] = sprintf(
'<option value="%s" lang="%s"%s>%s</option>',
esc_attr( $translation['languague'] ),
esc_attr( current( $translation['iso'] ) ),
selected( $translation['languague'], $parsed_args['selected'], false ),
esc_html( $translation['native_name'] )
);
}
$structure[] = '</optgroup>';
}
// Combine the output string.
$output = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) );
$output .= implode( "\n", $structure );
$output .= '</select>';
User Contributed Notes
You must log in before being able to contribute a note or feedback.