Language on install: Docs and naming cleanups. see #28577.

Built from https://develop.svn.wordpress.org/trunk@29018


git-svn-id: http://core.svn.wordpress.org/trunk@28806 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2014-07-07 21:49:15 +00:00
parent d7abad48e0
commit bc47136605
3 changed files with 62 additions and 50 deletions

View File

@ -2159,7 +2159,7 @@ CREATE TABLE $wpdb->sitecategories (
} }
endif; endif;
function wp_install_language_form( $body ) { function wp_install_language_form( $languages ) {
echo "<fieldset>\n"; echo "<fieldset>\n";
echo "<legend class='screen-reader-text'>Select a default language</legend>\n"; echo "<legend class='screen-reader-text'>Select a default language</legend>\n";
echo '<input type="radio" checked="checked" class="screen-reader-input" name="language" id="language_default" value="">'; echo '<input type="radio" checked="checked" class="screen-reader-input" name="language" id="language_default" value="">';
@ -2167,14 +2167,14 @@ function wp_install_language_form( $body ) {
echo "\n"; echo "\n";
if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && ( 'en_US' !== WPLANG ) ) { if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && ( 'en_US' !== WPLANG ) ) {
if ( isset( $body['languages'][WPLANG] ) ) { if ( isset( $languages[ WPLANG ] ) ) {
$language = $body['languages'][WPLANG]; $language = $languages[ WPLANG ];
echo '<input type="radio" name="language" checked="checked" class="' . esc_attr( $language['language'] ) . ' screen-reader-input" id="language_wplang" value="' . esc_attr( $language['language'] ) . '">'; echo '<input type="radio" name="language" checked="checked" class="' . esc_attr( $language['language'] ) . ' screen-reader-input" id="language_wplang" value="' . esc_attr( $language['language'] ) . '">';
echo '<label for="language_wplang">' . esc_html( $language['native_name'] ) . "</label>\n"; echo '<label for="language_wplang">' . esc_html( $language['native_name'] ) . "</label>\n";
} }
} }
foreach ( $body['languages'] as $language ) { foreach ( $languages as $language ) {
echo '<input type="radio" name="language" class="' . esc_attr( $language['language'] ) . ' screen-reader-input" id="language_'. esc_attr( $language['language'] ) .'" value="' . esc_attr( $language['language'] ) . '">'; echo '<input type="radio" name="language" class="' . esc_attr( $language['language'] ) . ' screen-reader-input" id="language_'. esc_attr( $language['language'] ) .'" value="' . esc_attr( $language['language'] ) . '">';
echo '<label for="language_' . esc_attr( $language['language'] ) . '">' . esc_html( $language['native_name'] ) . "</label>\n"; echo '<label for="language_' . esc_attr( $language['language'] ) . '">' . esc_html( $language['native_name'] ) . "</label>\n";
} }
@ -2183,9 +2183,13 @@ function wp_install_language_form( $body ) {
} }
/** /**
* @todo rename, move * Gets available translations from the WordPress.org API.
*
* @since 4.0.0
*
* @return array Array of translations, each an array of data.
*/ */
function wp_get_available_translations() { function wp_get_available_translations_from_api() {
$url = 'http://api.wordpress.org/translations/core/1.0/'; $url = 'http://api.wordpress.org/translations/core/1.0/';
if ( wp_http_supports( array( 'ssl' ) ) ) { if ( wp_http_supports( array( 'ssl' ) ) ) {
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
@ -2199,72 +2203,80 @@ function wp_get_available_translations() {
$response = wp_remote_post( $url, $options ); $response = wp_remote_post( $url, $options );
$body = wp_remote_retrieve_body( $response ); $body = wp_remote_retrieve_body( $response );
if ( $body && $body = json_decode( $body, true ) ) { if ( $body && $body = json_decode( $body, true ) ) {
$languages = array(); $translations = array();
// Key the language array with the language code // Key the array with the language code for now
foreach ( $body['languages'] as $language ) { foreach ( $body['translations'] as $translation ) {
$languages[$language['language']] = $language; $translations[ $translation['language'] ] = $translation;
} }
$body['languages'] = $languages; return $translations;
return $body;
} }
return false; return false;
} }
/** /**
* [wp_install_download_language_pack description] * Downloads a language pack.
* *
* @param string $lang [description] * @since 4.0.0
* @return string|false [description] *
* @param string $download Language code to download.
* @return string|false Returns the language code if successfully downloaded
* (or already installed), or false on failure.
*/ */
function wp_install_download_language_pack( $lang ) { function wp_install_download_language_pack( $download ) {
// Check if the language is already installed. // Check if the translation is already installed.
$available_languages = get_available_languages(); if ( in_array( $download, get_available_languages() ) ) {
if ( in_array( $lang, $available_languages ) ) { return $download;
return $lang;
} }
// Confirm the language is one we can download. // Confirm the translation is one we can download.
$body = wp_get_available_translations(); $translations = wp_get_available_translations_from_api();
$loading_language = false; if ( ! $translations ) {
if ( $body ) { return false;
foreach ( $body['languages'] as $language ) { }
if ( $language['language'] === $lang ) { foreach ( $translations as $translation ) {
$loading_language = $lang; if ( $translation['language'] === $download ) {
break; $translation_to_load = true;
} break;
} }
} }
if ( ! $loading_language ) { if ( empty( $translation_to_load ) ) {
return false; return false;
} }
$language = (object) $language; $translation = (object) $translation;
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$skin = new Automatic_Upgrader_Skin; $skin = new Automatic_Upgrader_Skin;
$upgrader = new Language_Pack_Upgrader( $skin ); $upgrader = new Language_Pack_Upgrader( $skin );
$language->type = 'core'; $translation->type = 'core';
/** /**
* @todo failures (such as non-direct FS) * @todo failures (such as non-direct FS)
*/ */
$upgrader->upgrade( $language, array( 'clear_update_cache' => false ) ); $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
return $language->language; return $translation->language;
} }
function wp_install_load_language( $request ) { /**
$loading_language = ''; * Load a translation during the install process.
if ( ! empty( $request ) ) { *
$available_languages = get_available_languages(); * @since 4.0.0
if ( in_array( $request, $available_languages ) ) { *
$loading_language = $request; * @param string $translation Translation to load.
* @return string|false Returns the language code if successfully loaded,
* or false on failure.
*/
function wp_install_load_language( $translation ) {
if ( ! empty( $translation ) ) {
if ( in_array( $translation, get_available_languages() ) ) {
$translation_to_load = $translation;
} }
} }
if ( $loading_language ) { if ( empty( $translation_to_load ) ) {
load_textdomain( 'default', WP_LANG_DIR . "/{$loading_language}.mo" ); return false;
load_textdomain( 'default', WP_LANG_DIR . "/admin-{$loading_language}.mo" );
return $loading_language;
} }
return false; load_textdomain( 'default', WP_LANG_DIR . "/{$translation_to_load}.mo" );
load_textdomain( 'default', WP_LANG_DIR . "/admin-{$translation_to_load}.mo" );
return $translation_to_load;
} }

View File

@ -179,10 +179,10 @@ if ( ! is_string( $wpdb->base_prefix ) || '' === $wpdb->base_prefix ) {
switch($step) { switch($step) {
case 0: // Step 0 case 0: // Step 0
if ( empty( $_GET['language'] ) && ( $body = wp_get_available_translations() ) ) { if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations_from_api() ) ) {
display_header( 'language-chooser' ); display_header( 'language-chooser' );
echo '<form id="setup" method="post" action="?step=1">'; echo '<form id="setup" method="post" action="?step=1">';
wp_install_language_form( $body ); wp_install_language_form( $languages );
echo '</form>'; echo '</form>';
break; break;
} }

View File

@ -86,10 +86,10 @@ function setup_config_display_header( $body_classes = array() ) {
switch($step) { switch($step) {
case -1: case -1:
if ( empty( $_GET['language'] ) && ( $body = wp_get_available_translations() ) ) { if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations_from_api() ) ) {
setup_config_display_header( 'language-chooser' ); setup_config_display_header( 'language-chooser' );
echo '<form id="setup" method="post" action="?step=0">'; echo '<form id="setup" method="post" action="?step=0">';
wp_install_language_form( $body ); wp_install_language_form( $languages );
echo '</form>'; echo '</form>';
break; break;
} }