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:
parent
8422a51b98
commit
dc8e01fa9b
|
@ -767,6 +767,14 @@ table.form-table td .updated p {
|
||||||
margin: -3px 3px;
|
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
|
/* =Media Queries
|
||||||
-------------------------------------------------------------- */
|
-------------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
/** Load WordPress Administration Bootstrap */
|
/** Load WordPress Administration Bootstrap */
|
||||||
require_once( dirname( __FILE__ ) . '/admin.php' );
|
require_once( dirname( __FILE__ ) . '/admin.php' );
|
||||||
|
|
||||||
|
/** WordPress Translation Install API */
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
|
||||||
|
|
||||||
if ( ! is_multisite() )
|
if ( ! is_multisite() )
|
||||||
wp_die( __( 'Multisite support is not enabled.' ) );
|
wp_die( __( 'Multisite support is not enabled.' ) );
|
||||||
|
|
||||||
|
@ -19,6 +22,29 @@ if ( ! current_user_can( 'manage_network_options' ) )
|
||||||
$title = __( 'Network Settings' );
|
$title = __( 'Network Settings' );
|
||||||
$parent_file = 'settings.php';
|
$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(
|
get_current_screen()->add_help_tab( array(
|
||||||
'id' => 'overview',
|
'id' => 'overview',
|
||||||
'title' => __('Overview'),
|
'title' => __('Overview'),
|
||||||
|
@ -58,6 +84,14 @@ if ( $_POST ) {
|
||||||
'illegal_names', 'limited_email_domains', 'banned_email_domains', 'WPLANG', 'admin_email',
|
'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 ) {
|
foreach ( $options as $option_name ) {
|
||||||
if ( ! isset($_POST[$option_name]) )
|
if ( ! isset($_POST[$option_name]) )
|
||||||
continue;
|
continue;
|
||||||
|
@ -275,7 +309,8 @@ if ( isset( $_GET['updated'] ) ) {
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
$languages = get_available_languages();
|
$languages = get_available_languages();
|
||||||
if ( ! empty( $languages ) ) {
|
$translations = wp_get_available_translations();
|
||||||
|
if ( ! empty( $languages ) || ! empty( $translations ) ) {
|
||||||
?>
|
?>
|
||||||
<h3><?php _e( 'Language Settings' ); ?></h3>
|
<h3><?php _e( 'Language Settings' ); ?></h3>
|
||||||
<table class="form-table">
|
<table class="form-table">
|
||||||
|
@ -293,6 +328,8 @@ if ( isset( $_GET['updated'] ) ) {
|
||||||
'id' => 'WPLANG',
|
'id' => 'WPLANG',
|
||||||
'selected' => $lang,
|
'selected' => $lang,
|
||||||
'languages' => $languages,
|
'languages' => $languages,
|
||||||
|
'translations' => $translations,
|
||||||
|
'show_available_translations' => wp_can_install_language_pack(),
|
||||||
) );
|
) );
|
||||||
?>
|
?>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -9,6 +9,9 @@
|
||||||
/** WordPress Administration Bootstrap */
|
/** WordPress Administration Bootstrap */
|
||||||
require_once( dirname( __FILE__ ) . '/admin.php' );
|
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' ) )
|
if ( ! current_user_can( 'manage_options' ) )
|
||||||
wp_die( __( 'You do not have sufficient permissions to manage options for this site.' ) );
|
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()
|
date : format.val()
|
||||||
}, function(d) { format.siblings('.spinner').hide(); format.siblings('.example').text(d); } );
|
}, 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>
|
</script>
|
||||||
|
@ -318,10 +330,11 @@ endfor;
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
$languages = get_available_languages();
|
$languages = get_available_languages();
|
||||||
|
$translations = wp_get_available_translations();
|
||||||
if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG && ! in_array( WPLANG, $languages ) ) {
|
if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG && ! in_array( WPLANG, $languages ) ) {
|
||||||
$languages[] = WPLANG;
|
$languages[] = WPLANG;
|
||||||
}
|
}
|
||||||
if ( $languages ) {
|
if ( ! empty( $languages ) || ! empty( $translations ) ) {
|
||||||
?>
|
?>
|
||||||
<tr>
|
<tr>
|
||||||
<th width="33%" scope="row"><label for="WPLANG"><?php _e( 'Site Language' ); ?></label></th>
|
<th width="33%" scope="row"><label for="WPLANG"><?php _e( 'Site Language' ); ?></label></th>
|
||||||
|
@ -337,6 +350,8 @@ if ( $languages ) {
|
||||||
'id' => 'WPLANG',
|
'id' => 'WPLANG',
|
||||||
'selected' => $locale,
|
'selected' => $locale,
|
||||||
'languages' => $languages,
|
'languages' => $languages,
|
||||||
|
'translations' => $translations,
|
||||||
|
'show_available_translations' => ( ! is_multisite() || is_super_admin() ) && wp_can_install_language_pack(),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Add note about deprecated WPLANG constant.
|
// Add note about deprecated WPLANG constant.
|
||||||
|
|
|
@ -151,8 +151,8 @@ if ( 'update' == $action ) {
|
||||||
$options = $whitelist_options[ $option_page ];
|
$options = $whitelist_options[ $option_page ];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle custom date/time formats.
|
|
||||||
if ( 'general' == $option_page ) {
|
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'] ) )
|
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'];
|
$_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'] ) )
|
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['gmt_offset'] = preg_replace('/UTC\+?/', '', $_POST['gmt_offset']);
|
||||||
$_POST['timezone_string'] = '';
|
$_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 ) {
|
if ( $options ) {
|
||||||
|
|
|
@ -849,23 +849,35 @@ function wp_get_pomo_file_data( $po_file ) {
|
||||||
* @see get_available_languages()
|
* @see get_available_languages()
|
||||||
* @see wp_get_available_translations()
|
* @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() ) {
|
function wp_dropdown_languages( $args = array() ) {
|
||||||
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
|
|
||||||
|
|
||||||
$args = wp_parse_args( $args, array(
|
$args = wp_parse_args( $args, array(
|
||||||
'id' => '',
|
'id' => '',
|
||||||
'name' => '',
|
'name' => '',
|
||||||
'languages' => array(),
|
'languages' => array(),
|
||||||
'selected' => ''
|
'translations' => array(),
|
||||||
|
'selected' => '',
|
||||||
|
'show_available_translations' => true,
|
||||||
) );
|
) );
|
||||||
|
|
||||||
if ( empty( $args['languages'] ) ) {
|
$translations = $args['translations'];
|
||||||
return false;
|
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
|
* $args['languages'] should only contain the locales. Find the locale in
|
||||||
|
@ -880,6 +892,9 @@ function wp_dropdown_languages( $args = array() ) {
|
||||||
'native_name' => $translation['native_name'],
|
'native_name' => $translation['native_name'],
|
||||||
'lang' => $translation['iso'][1],
|
'lang' => $translation['iso'][1],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Remove installed language from available translations.
|
||||||
|
unset( $translations[ $locale ] );
|
||||||
} else {
|
} else {
|
||||||
$languages[] = array(
|
$languages[] = array(
|
||||||
'language' => $locale,
|
'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'] ) );
|
printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
|
||||||
|
|
||||||
|
// Holds the HTML markup.
|
||||||
|
$structure = array();
|
||||||
|
|
||||||
// List installed languages.
|
// 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 ) {
|
foreach ( $languages as $language ) {
|
||||||
$selected = selected( $language['language'], $args['selected'], false );
|
$structure[] = sprintf(
|
||||||
printf(
|
'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
|
||||||
'<option value="%s" lang="%s"%s>%s</option>',
|
|
||||||
esc_attr( $language['language'] ),
|
esc_attr( $language['language'] ),
|
||||||
esc_attr( $language['lang'] ),
|
esc_attr( $language['lang'] ),
|
||||||
$selected,
|
selected( $language['language'], $args['selected'], false ),
|
||||||
esc_html( $language['native_name'] )
|
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>';
|
echo '</select>';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue