From 39cf46851b15145e72845791ad6d3c5cdcca55db Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Wed, 18 Jun 2014 19:57:21 +0000 Subject: [PATCH] Allow a language to be chosen before installing WordPress. First pass. * Checks WordPress.org for available languages. * In get_locale(), starts using the WPLANG option that has existed in multisite since the MU days. * Adds new argument to wp_install() for setting WPLANG. see #28577. git-svn-id: https://develop.svn.wordpress.org/trunk@28774 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/css/install.css | 35 +++++++++++++ src/wp-admin/includes/upgrade.php | 7 ++- src/wp-admin/install.php | 85 +++++++++++++++++++++++++++++-- src/wp-includes/l10n.php | 8 ++- 4 files changed, 128 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/css/install.css b/src/wp-admin/css/install.css index a2e19a1cc2..8d379a7630 100644 --- a/src/wp-admin/css/install.css +++ b/src/wp-admin/css/install.css @@ -311,3 +311,38 @@ body.rtl, } } + +.language-chooser select { + margin: 1px; + padding: 8px; + width: 100%; + display: block; + border: 1px solid #ddd; + -webkit-border-radius: 0; + border-radius: 0; /* Reset mobile webkit's default element styling */ + -webkit-transition: .05s border-color ease-in-out; + transition: .05s border-color ease-in-out; + outline: 0; + -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.07); + box-shadow: inset 0 1px 2px rgba(0,0,0,0.07); + background-color: #fff; + color: #333; + font-size: 16px; + font-family: inherit; + font-weight: inherit; +} + +.language-chooser select:focus { + border-color: #5b9dd9; + -webkit-box-shadow: 0 0 2px rgba(30,140,190,0.8); + box-shadow: 0 0 2px rgba(30,140,190,0.8); +} + +.wp-core-ui .language-chooser .button.button-hero { + font-size: 30px; + line-height: 30px; +} + +.language-chooser p { + text-align: right; +} \ No newline at end of file diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 72c67b4e9d..9e9441e084 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -32,9 +32,10 @@ if ( !function_exists('wp_install') ) : * @param bool $public Whether blog is public. * @param null $deprecated Optional. Not used. * @param string $user_password Optional. User's chosen password. Will default to a random password. + * @param string $language Optional. Language chosen. * @return array Array keys 'url', 'user_id', 'password', 'password_message'. */ -function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) { +function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '2.6' ); @@ -48,6 +49,10 @@ function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated update_option('admin_email', $user_email); update_option('blog_public', $public); + if ( $language ) { + update_option( 'WPLANG', $language ); + } + $guessurl = wp_guess_url(); update_option('siteurl', $guessurl); diff --git a/src/wp-admin/install.php b/src/wp-admin/install.php index 4f03e92511..5b863b0cc7 100644 --- a/src/wp-admin/install.php +++ b/src/wp-admin/install.php @@ -43,6 +43,28 @@ require_once( ABSPATH . 'wp-includes/wp-db.php' ); $step = isset( $_GET['step'] ) ? (int) $_GET['step'] : 0; +/** + * @todo rename, move + */ +function wp_get_available_translations() { + $url = 'http://api.wordpress.org/translations/core/1.0/'; + if ( wp_http_supports( array( 'ssl' ) ) ) { + $url = set_url_scheme( $url, 'https' ); + } + + $options = array( + 'timeout' => 3, + 'body' => array( 'version' => $GLOBALS['wp_version'] ), + ); + + $response = wp_remote_post( $url, $options ); + $body = wp_remote_retrieve_body( $response ); + if ( $body && $body = json_decode( $body, true ) ) { + return $body; + } + return false; +} + /** * Display install header. * @@ -136,6 +158,7 @@ function display_setup_form( $error = null ) {

+ base_prefix ) || '' === $wpdb->base_prefix ) { } switch($step) { - case 0: // Step 1 - case 1: // Step 1, direct link. - display_header(); + case 0: // Step 0 + if ( $body = wp_get_available_translations() ) { + display_header(); + + echo '
'; + echo '
'; + echo '\n"; + echo '

'; + echo '
'; + echo '
'; + break; + } + // Deliberately fall through if we can't reach the translations API. + + case 1: // Step 1, direct link or from language chooser. + if ( ! empty( $_POST['language'] ) ) { + $body = wp_get_available_translations(); + foreach ( $body['languages'] as $language ) { + if ( $language['language'] === $_POST['language'] ) { + $loading_language = $_POST['language']; + break; + } + } + if ( ! empty( $loading_language ) ) { + require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; + $skin = new Automatic_Upgrader_Skin; + $upgrader = new Language_Pack_Upgrader( $skin ); + $options = array( 'clear_update_cache' => false ); + $language['type'] = 'core'; + $language = (object) $language; + /** + * @todo failures (such as non-direct FS) + */ + $upgrader->upgrade( $language, array( 'clear_update_cache' => false ) ); + load_textdomain( 'default', WP_LANG_DIR . "/{$loading_language}.mo" ); + load_textdomain( 'default', WP_LANG_DIR . "/admin-{$loading_language}.mo" ); + } + } + + display_header(); ?>

ReadMe documentation at your leisure. Otherwise, just fill in the information below and you’ll be on your way to using the most extendable and powerful personal publishing platform in the world.' ), '../readme.html' ); ?>

@@ -183,6 +248,16 @@ switch($step) { display_setup_form(); break; case 2: + $loading_language = ''; + if ( ! empty( $_POST['language'] ) ) { + $available_languages = get_available_languages(); + if ( isset( $available_languages[ $_POST['language'] ] ) ) { + $loading_language = $_POST['language']; + load_textdomain( 'default', WP_LANG_DIR . "/{$loading_language}.mo" ); + load_textdomain( 'default', WP_LANG_DIR . "/admin-{$loading_language}.mo" ); + } + } + if ( ! empty( $wpdb->error ) ) wp_die( $wpdb->error->get_error_message() ); @@ -219,7 +294,7 @@ switch($step) { if ( $error === false ) { $wpdb->show_errors(); - $result = wp_install( $weblog_title, $user_name, $admin_email, $public, '', wp_slash( $admin_password ) ); + $result = wp_install( $weblog_title, $user_name, $admin_email, $public, '', wp_slash( $admin_password ), $loading_language ); ?>

@@ -250,7 +325,7 @@ switch($step) { } if ( !wp_is_mobile() ) { ?> - + diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 541cb15f99..05aa52afcc 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -26,7 +26,7 @@ function get_locale() { global $locale; - if ( isset( $locale ) ) + if ( isset( $locale ) ) { /** * Filter WordPress install's locale ID. * @@ -35,6 +35,7 @@ function get_locale() { * @param string $locale The locale ID. */ return apply_filters( 'locale', $locale ); + } // WPLANG is defined in wp-config. if ( defined( 'WPLANG' ) ) @@ -48,6 +49,11 @@ function get_locale() { if ( $ms_locale !== false ) $locale = $ms_locale; + } elseif ( ! defined( 'WP_INSTALLING' ) ) { + $db_locale = get_option( 'WPLANG' ); + if ( $db_locale ) { + $locale = $db_locale; + } } if ( empty( $locale ) )