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.

git-svn-id: https://develop.svn.wordpress.org/trunk@30335 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2014-11-13 17:00:08 +00:00
parent 8422a51b98
commit dc8e01fa9b
5 changed files with 137 additions and 29 deletions

View File

@ -611,9 +611,9 @@ table.form-table td .updated {
font-size: 13px;
}
table.form-table td .updated p {
font-size: 13px;
margin: 0.3em 0;
table.form-table td .updated p {
font-size: 13px;
margin: 0.3em 0;
}
/*------------------------------------------------------------------------------
@ -767,6 +767,14 @@ table.form-table td .updated p {
margin: -3px 3px;
}
.settings-php .language-install-spinner,
.options-general-php .language-install-spinner {
display: inline-block;
float: none;
margin: -3px 5px 0;
vertical-align: middle;
}
/* =Media Queries
-------------------------------------------------------------- */

View File

@ -10,6 +10,9 @@
/** Load WordPress Administration Bootstrap */
require_once( dirname( __FILE__ ) . '/admin.php' );
/** WordPress Translation Install API */
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
if ( ! is_multisite() )
wp_die( __( 'Multisite support is not enabled.' ) );
@ -19,6 +22,29 @@ if ( ! current_user_can( 'manage_network_options' ) )
$title = __( 'Network Settings' );
$parent_file = 'settings.php';
/**
* Display JavaScript on the page.
*
* @since 4.1.0
*/
function network_settings_add_js() {
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
var languageSelect = $( '#WPLANG' );
$( 'form' ).submit( function() {
// Don't show a spinner for English and installed languages,
// as there is nothing to download.
if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
$( '#submit', this ).after( '<span class="spinner language-install-spinner" />' );
}
});
});
</script>
<?php
}
add_action( 'admin_head', 'network_settings_add_js' );
get_current_screen()->add_help_tab( array(
'id' => 'overview',
'title' => __('Overview'),
@ -58,6 +84,14 @@ if ( $_POST ) {
'illegal_names', 'limited_email_domains', 'banned_email_domains', 'WPLANG', 'admin_email',
);
// Handle translation install.
if ( ! empty( $_POST['WPLANG'] ) && wp_can_install_language_pack() ) { // @todo: Skip if already installed
$language = wp_download_language_pack( $_POST['WPLANG'] );
if ( $language ) {
$_POST['WPLANG'] = $language;
}
}
foreach ( $options as $option_name ) {
if ( ! isset($_POST[$option_name]) )
continue;
@ -275,7 +309,8 @@ if ( isset( $_GET['updated'] ) ) {
<?php
$languages = get_available_languages();
if ( ! empty( $languages ) ) {
$translations = wp_get_available_translations();
if ( ! empty( $languages ) || ! empty( $translations ) ) {
?>
<h3><?php _e( 'Language Settings' ); ?></h3>
<table class="form-table">
@ -289,10 +324,12 @@ if ( isset( $_GET['updated'] ) ) {
}
wp_dropdown_languages( array(
'name' => 'WPLANG',
'id' => 'WPLANG',
'selected' => $lang,
'languages' => $languages,
'name' => 'WPLANG',
'id' => 'WPLANG',
'selected' => $lang,
'languages' => $languages,
'translations' => $translations,
'show_available_translations' => wp_can_install_language_pack(),
) );
?>
</td>

View File

@ -9,6 +9,9 @@
/** WordPress Administration Bootstrap */
require_once( dirname( __FILE__ ) . '/admin.php' );
/** WordPress Translation Install API */
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
if ( ! current_user_can( 'manage_options' ) )
wp_die( __( 'You do not have sufficient permissions to manage options for this site.' ) );
@ -63,6 +66,15 @@ function options_general_add_js() {
date : format.val()
}, function(d) { format.siblings('.spinner').hide(); format.siblings('.example').text(d); } );
});
var languageSelect = $( '#WPLANG' );
$( 'form' ).submit( function() {
// Don't show a spinner for English and installed languages,
// as there is nothing to download.
if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
$( '#submit', this ).after( '<span class="spinner language-install-spinner" />' );
}
});
});
//]]>
</script>
@ -318,10 +330,11 @@ endfor;
<?php
$languages = get_available_languages();
$translations = wp_get_available_translations();
if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG && ! in_array( WPLANG, $languages ) ) {
$languages[] = WPLANG;
}
if ( $languages ) {
if ( ! empty( $languages ) || ! empty( $translations ) ) {
?>
<tr>
<th width="33%" scope="row"><label for="WPLANG"><?php _e( 'Site Language' ); ?></label></th>
@ -333,10 +346,12 @@ if ( $languages ) {
}
wp_dropdown_languages( array(
'name' => 'WPLANG',
'id' => 'WPLANG',
'selected' => $locale,
'languages' => $languages,
'name' => 'WPLANG',
'id' => 'WPLANG',
'selected' => $locale,
'languages' => $languages,
'translations' => $translations,
'show_available_translations' => ( ! is_multisite() || is_super_admin() ) && wp_can_install_language_pack(),
) );
// Add note about deprecated WPLANG constant.

View File

@ -151,8 +151,8 @@ if ( 'update' == $action ) {
$options = $whitelist_options[ $option_page ];
}
// Handle custom date/time formats.
if ( 'general' == $option_page ) {
// Handle custom date/time formats.
if ( !empty($_POST['date_format']) && isset($_POST['date_format_custom']) && '\c\u\s\t\o\m' == wp_unslash( $_POST['date_format'] ) )
$_POST['date_format'] = $_POST['date_format_custom'];
if ( !empty($_POST['time_format']) && isset($_POST['time_format_custom']) && '\c\u\s\t\o\m' == wp_unslash( $_POST['time_format'] ) )
@ -163,6 +163,18 @@ if ( 'update' == $action ) {
$_POST['gmt_offset'] = preg_replace('/UTC\+?/', '', $_POST['gmt_offset']);
$_POST['timezone_string'] = '';
}
// Handle translation install.
if ( ! empty( $_POST['WPLANG'] ) && ( ! is_multisite() || is_super_admin() ) ) { // @todo: Skip if already installed
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
if ( wp_can_install_language_pack() ) {
$language = wp_download_language_pack( $_POST['WPLANG'] );
if ( $language ) {
$_POST['WPLANG'] = $language;
}
}
}
}
if ( $options ) {

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>';
}