Site Language: Install translations on the fly.

The language dropdown now includes installed languages and all available translations when the filesystem is writable by WordPress.
Go to wp-admin/options-general.php, select one of the available translations, submit the form and let WordPress handle the rest.
Works for Multisite's Default Language too.

see #29395.
Built from https://develop.svn.wordpress.org/trunk@30335


git-svn-id: http://core.svn.wordpress.org/trunk@30334 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling
2014-11-13 17:01:24 +00:00
parent 4a1cb796fc
commit 8fb6f317ff
11 changed files with 153 additions and 37 deletions

View File

@@ -849,24 +849,36 @@ function wp_get_pomo_file_data( $po_file ) {
* @see get_available_languages()
* @see wp_get_available_translations()
*
* @param array $args Optional arguments. Default empty array.
* @param string|array $query {
* Optional. Array of arguments.
*
* @type string $id ID attribute of the select element. Default empty.
* @type string $name Name attribute of the select element. Default empty.
* @type array $languages List of installed languages, contain only the locales.
* Default empty array.
* @type array $translations List of available translations. Default result of
* {@see wp_get_available_translations()}.
* @type string $selected Language which should be selected. Default empty.
* @type bool $show_available_translations Whether to show available translations. Default true.
* }
*/
function wp_dropdown_languages( $args = array() ) {
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
$args = wp_parse_args( $args, array(
'id' => '',
'name' => '',
'languages' => array(),
'selected' => ''
'id' => '',
'name' => '',
'languages' => array(),
'translations' => array(),
'selected' => '',
'show_available_translations' => true,
) );
if ( empty( $args['languages'] ) ) {
return false;
$translations = $args['translations'];
if ( empty( $translations ) ) {
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
$translations = wp_get_available_translations();
}
$translations = wp_get_available_translations();
/*
* $args['languages'] should only contain the locales. Find the locale in
* $translations to get the native name. Fall back to locale.
@@ -880,6 +892,9 @@ function wp_dropdown_languages( $args = array() ) {
'native_name' => $translation['native_name'],
'lang' => $translation['iso'][1],
);
// Remove installed language from available translations.
unset( $translations[ $locale ] );
} else {
$languages[] = array(
'language' => $locale,
@@ -891,18 +906,39 @@ function wp_dropdown_languages( $args = array() ) {
printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
// Holds the HTML markup.
$structure = array();
// List installed languages.
echo '<option value="" lang="en">English (United States)</option>';
$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
$structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
foreach ( $languages as $language ) {
$selected = selected( $language['language'], $args['selected'], false );
printf(
'<option value="%s" lang="%s"%s>%s</option>',
$structure[] = sprintf(
'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
esc_attr( $language['language'] ),
esc_attr( $language['lang'] ),
$selected,
selected( $language['language'], $args['selected'], false ),
esc_html( $language['native_name'] )
);
}
$structure[] = '</optgroup>';
// List available translations.
if ( ! empty( $translations ) && $args['show_available_translations'] ) {
$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['language'] ),
esc_attr( $translation['iso'][1] ),
selected( $translation['language'], $args['selected'], false ),
esc_html( $translation['native_name'] )
);
}
$structure[] = '</optgroup>';
}
echo join( "\n", $structure );
echo '</select>';
}