WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/18/2014 07:57:21 PM (7 years ago)
Author:
nacin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/install.php

    r28759 r28774  
    4545
    4646/**
     47 * @todo rename, move
     48 */
     49function wp_get_available_translations() {
     50    $url = 'http://api.wordpress.org/translations/core/1.0/';
     51    if ( wp_http_supports( array( 'ssl' ) ) ) {
     52        $url = set_url_scheme( $url, 'https' );
     53    }
     54
     55    $options = array(
     56        'timeout' => 3,
     57        'body' => array( 'version' => $GLOBALS['wp_version'] ),
     58    );
     59
     60    $response = wp_remote_post( $url, $options );
     61    $body = wp_remote_retrieve_body( $response );
     62    if ( $body && $body = json_decode( $body, true ) ) {
     63        return $body;
     64    }
     65    return false;
     66}
     67
     68/**
    4769 * Display install header.
    4870 *
     
    137159    </table>
    138160    <p class="step"><input type="submit" name="Submit" value="<?php esc_attr_e( 'Install WordPress' ); ?>" class="button button-large" /></p>
     161    <input type="hidden" name="language" value="<?php echo isset( $_POST['language'] ) ? esc_attr( $_POST['language'] ) : ''; ?>" />
    139162</form>
    140163<?php
     
    170193
    171194switch($step) {
    172     case 0: // Step 1
    173     case 1: // Step 1, direct link.
    174       display_header();
     195    case 0: // Step 0
     196        if ( $body = wp_get_available_translations() ) {
     197            display_header();
     198
     199            echo '<form id="setup" method="post" action="install.php?step=1">';
     200            echo '<div class="language-chooser">';
     201            echo '<select name="language" id="language-chooser" size="15">';
     202            echo '<option selected="selected" value="">English (United States)</option>';
     203            foreach ( $body['languages'] as $language ) {
     204                echo '<option value="' . esc_attr( $language['language'] ) . '">' . esc_html( $language['native_name'] ) . "</option>\n";
     205            }
     206            echo "</select>\n";
     207            echo '<p class="step"><input type="submit" class="button button-primary button-hero" value="&raquo;" /></p>';
     208            echo '</div>';
     209            echo '</form>';
     210            break;
     211        }
     212        // Deliberately fall through if we can't reach the translations API.
     213
     214    case 1: // Step 1, direct link or from language chooser.
     215        if ( ! empty( $_POST['language'] ) ) {
     216            $body = wp_get_available_translations();
     217            foreach ( $body['languages'] as $language ) {
     218                if ( $language['language'] === $_POST['language'] ) {
     219                    $loading_language = $_POST['language'];
     220                    break;
     221                }
     222            }
     223            if ( ! empty( $loading_language ) ) {
     224                require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     225                $skin = new Automatic_Upgrader_Skin;
     226                $upgrader = new Language_Pack_Upgrader( $skin );
     227                $options = array( 'clear_update_cache' => false );
     228                $language['type'] = 'core';
     229                $language = (object) $language;
     230                /**
     231                 * @todo failures (such as non-direct FS)
     232                 */
     233                $upgrader->upgrade( $language, array( 'clear_update_cache' => false ) );
     234                load_textdomain( 'default', WP_LANG_DIR . "/{$loading_language}.mo" );
     235                load_textdomain( 'default', WP_LANG_DIR . "/admin-{$loading_language}.mo" );
     236            }
     237        }
     238
     239        display_header();
    175240?>
    176241<h1><?php _ex( 'Welcome', 'Howdy' ); ?></h1>
     
    184249        break;
    185250    case 2:
     251        $loading_language = '';
     252        if ( ! empty( $_POST['language'] ) ) {
     253            $available_languages = get_available_languages();
     254            if ( isset( $available_languages[ $_POST['language'] ] ) ) {
     255                $loading_language = $_POST['language'];
     256                load_textdomain( 'default', WP_LANG_DIR . "/{$loading_language}.mo" );
     257                load_textdomain( 'default', WP_LANG_DIR . "/admin-{$loading_language}.mo" );
     258            }
     259        }
     260
    186261        if ( ! empty( $wpdb->error ) )
    187262            wp_die( $wpdb->error->get_error_message() );
     
    220295        if ( $error === false ) {
    221296            $wpdb->show_errors();
    222             $result = wp_install( $weblog_title, $user_name, $admin_email, $public, '', wp_slash( $admin_password ) );
     297            $result = wp_install( $weblog_title, $user_name, $admin_email, $public, '', wp_slash( $admin_password ), $loading_language );
    223298?>
    224299
     
    251326if ( !wp_is_mobile() ) {
    252327?>
    253 <script type="text/javascript">var t = document.getElementById('weblog_title'); if (t){ t.focus(); }</script>
     328<script type="text/javascript">var t = document.getElementById('weblog_title') || document.getElementById('language-chooser'); if (t){ t.focus(); }</script>
    254329<?php } ?>
    255330<?php wp_print_scripts( 'user-profile' ); ?>
Note: See TracChangeset for help on using the changeset viewer.