From dc8e01fa9b30321323db8bd1a534620dbe8c1976 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Thu, 13 Nov 2014 17:00:08 +0000 Subject: [PATCH] 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 --- src/wp-admin/css/forms.css | 14 +++++-- src/wp-admin/network/settings.php | 47 +++++++++++++++++++--- src/wp-admin/options-general.php | 25 +++++++++--- src/wp-admin/options.php | 14 ++++++- src/wp-includes/l10n.php | 66 ++++++++++++++++++++++++------- 5 files changed, 137 insertions(+), 29 deletions(-) diff --git a/src/wp-admin/css/forms.css b/src/wp-admin/css/forms.css index 250c31679a..59cc7c5abd 100644 --- a/src/wp-admin/css/forms.css +++ b/src/wp-admin/css/forms.css @@ -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 -------------------------------------------------------------- */ diff --git a/src/wp-admin/network/settings.php b/src/wp-admin/network/settings.php index 1736511181..abd15118f2 100644 --- a/src/wp-admin/network/settings.php +++ b/src/wp-admin/network/settings.php @@ -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() { +?> + +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'] ) ) {

@@ -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(), ) ); ?> diff --git a/src/wp-admin/options-general.php b/src/wp-admin/options-general.php index 24ec42ea29..28fe6be64a 100644 --- a/src/wp-admin/options-general.php +++ b/src/wp-admin/options-general.php @@ -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( '' ); + } + }); }); //]]> @@ -318,10 +330,11 @@ endfor; @@ -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. diff --git a/src/wp-admin/options.php b/src/wp-admin/options.php index 29a9344694..594a234ff4 100644 --- a/src/wp-admin/options.php +++ b/src/wp-admin/options.php @@ -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 ) { diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 071920110b..e299e71579 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -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( ''; }