Make WordPress Core

Ticket #15677: 15677.12.patch

File 15677.12.patch, 26.9 KB (added by ocean90, 9 years ago)
  • src/wp-admin/includes/translation-install.php

     
     1<?php
     2/**
     3 * WordPress Translation Install Administration API
     4 *
     5 * @package WordPress
     6 * @subpackage Administration
     7 */
     8
     9
     10/**
     11 * Retrieve translations from WordPress Translation API.
     12 *
     13 * @since 4.0.0
     14 *
     15 * @param string       $type Type of translations. Accepts 'plugins', 'themes', 'core'.
     16 * @param array|object $args Translation API arguments. Optional.
     17 * @return object|WP_Error On success an object of translations, WP_Error on failure.
     18 */
     19function translations_api( $type, $args = null ) {
     20        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
     21
     22        if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ) ) ) {
     23                return  new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
     24        }
     25
     26        /**
     27         * Allows a plugin to override the WordPress.org Translation Install API entirely.
     28         *
     29         * @since 4.0.0
     30         *
     31         * @param bool|array  $result The result object. Default false.
     32         * @param string      $type   The type of translations being requested.
     33         * @param object      $args   Translation API arguments.
     34         */
     35        $res = apply_filters( 'translations_api', false, $type, $args );
     36
     37        if ( false === $res ) {
     38                $url = $http_url = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
     39                if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
     40                        $url = set_url_scheme( $url, 'https' );
     41                }
     42
     43                $options = array(
     44                        'timeout' => 3,
     45                        'body' => array(
     46                                'wp_version' => $wp_version,
     47                                'locale'     => get_locale(),
     48                                'version'    => $args['version'], // Version of plugin, theme or core
     49                        ),
     50                );
     51
     52                if ( 'core' !== $type ) {
     53                        $options['body']['slug'] = $args['slug']; // Plugin or theme slug
     54                }
     55
     56                $request = wp_remote_post( $url, $options );
     57
     58                if ( $ssl && is_wp_error( $request ) ) {
     59                        trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
     60
     61                        $request = wp_remote_post( $http_url, $options );
     62                }
     63
     64                if ( is_wp_error( $request ) ) {
     65                        $res = new WP_Error( 'translations_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
     66                } else {
     67                        $res = json_decode( wp_remote_retrieve_body( $request ), true );
     68                        if ( ! is_object( $res ) && ! is_array( $res ) ) {
     69                                $res = new WP_Error( 'translations_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
     70                        }
     71                }
     72        }
     73
     74        /**
     75         * Filter the Translation Install API response results.
     76         *
     77         * @since 4.0.0
     78         *
     79         * @param object|WP_Error $res  Response object or WP_Error.
     80         * @param string          $type The type of translations being requested.
     81         * @param object          $args Translation API arguments.
     82         */
     83        return apply_filters( 'translations_api_result', $res, $type, $args );
     84}
     85
     86/**
     87 * Get available translations from the WordPress.org API.
     88 *
     89 * @since 4.0.0
     90 *
     91 * @see translations_api()
     92 *
     93 * @return array Array of translations, each an array of data. If the API response results
     94 *               in an error, an empty array will be returned.
     95 */
     96function wp_get_available_translations() {
     97        if ( ! defined( 'WP_INSTALLING' ) && false !== ( $translations = get_site_transient( 'available_translations' ) ) ) {
     98                return $translations;
     99        }
     100
     101        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
     102
     103        $api = translations_api( 'core', array( 'version' => $wp_version ) );
     104
     105        if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
     106                return array();
     107        }
     108
     109        $translations = array();
     110        // Key the array with the language code for now
     111        foreach ( $api['translations'] as $translation ) {
     112                $translations[ $translation['language'] ] = $translation;
     113        }
     114
     115        if ( ! defined( 'WP_INSTALLING' ) ) {
     116                set_site_transient( 'available_translations', $translations, 3 * HOUR_IN_SECONDS );
     117        }
     118
     119        return $translations;
     120}
     121
     122/**
     123 * Output the select form for the language selection on the installation screen.
     124 *
     125 * @since 4.0.0
     126 *
     127 * @param array $languages Array of available languages (populated via the Translation API).
     128 */
     129function wp_install_language_form( $languages ) {
     130        $installed_languages = get_available_languages();
     131
     132        echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n";
     133        echo "<select size='14' name='language' id='language'>\n";
     134        echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>';
     135        echo "\n";
     136
     137        if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && ( 'en_US' !== WPLANG ) ) {
     138                if ( isset( $languages[ WPLANG ] ) ) {
     139                        $language = $languages[ WPLANG ];
     140                        echo '<option value="' . esc_attr( $language['language'] ) . '" lang="' . esc_attr( $language['iso'][1] ) . '">' . esc_html( $language['native_name'] ) . "</option>\n";
     141                        unset( $languages[ WPLANG ] );
     142                }
     143        }
     144
     145        foreach ( $languages as $language ) {
     146                printf( '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
     147                        esc_attr( $language['language'] ),
     148                        esc_attr( $language['iso'][1] ),
     149                        esc_attr( $language['strings']['continue'] ),
     150                        in_array( $language['language'], $installed_languages ) ? ' data-installed="1"' : '',
     151                        esc_html( $language['native_name'] ) );
     152        }
     153        echo "</select>\n";
     154        echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
     155}
     156
     157/**
     158 * Download a language pack.
     159 *
     160 * @since 4.0.0
     161 *
     162 * @see wp_get_available_translations()
     163 *
     164 * @param string $download Language code to download.
     165 * @return string|bool Returns the language code if successfully downloaded
     166 *                     (or already installed), or false on failure.
     167 */
     168function wp_download_language_pack( $download ) {
     169        // Check if the translation is already installed.
     170        if ( in_array( $download, get_available_languages() ) ) {
     171                return $download;
     172        }
     173
     174        if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
     175                return false;
     176        }
     177
     178        // Confirm the translation is one we can download.
     179        $translations = wp_get_available_translations();
     180        if ( ! $translations ) {
     181                return false;
     182        }
     183        foreach ( $translations as $translation ) {
     184                if ( $translation['language'] === $download ) {
     185                        $translation_to_load = true;
     186                        break;
     187                }
     188        }
     189
     190        if ( empty( $translation_to_load ) ) {
     191                return false;
     192        }
     193        $translation = (object) $translation;
     194
     195        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     196        $skin = new Automatic_Upgrader_Skin;
     197        $upgrader = new Language_Pack_Upgrader( $skin );
     198        $translation->type = 'core';
     199        /**
     200         * @todo failures (such as non-direct FS)
     201         */
     202        $result = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
     203        return $translation->language;
     204}
  • src/wp-admin/includes/upgrade.php

     
    14191419 */
    14201420function maybe_create_table($table_name, $create_ddl) {
    14211421        global $wpdb;
    1422        
     1422
    14231423        $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) );
    14241424
    14251425        if ( $wpdb->get_var( $query ) == $table_name ) {
     
    21922192        dbDelta( $ms_queries );
    21932193}
    21942194endif;
    2195 
    2196 /**
    2197  * Output the input fields for the language selection form on the installation screen.
    2198  *
    2199  * @since 4.0.0
    2200  *
    2201  * @see wp_get_available_translations_from_api()
    2202  *
    2203  * @param array $languages Array of available languages (populated via the Translations API).
    2204  */
    2205 function wp_install_language_form( $languages ) {
    2206         $installed_languages = get_available_languages();
    2207 
    2208         echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n";
    2209         echo "<select size='14' name='language' id='language'>\n";
    2210         echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>';
    2211         echo "\n";
    2212 
    2213         if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && ( 'en_US' !== WPLANG ) ) {
    2214                 if ( isset( $languages[ WPLANG ] ) ) {
    2215                         $language = $languages[ WPLANG ];
    2216                         echo '<option value="' . esc_attr( $language['language'] ) . '" lang="' . esc_attr( $language['iso'][1] ) . '">' . esc_html( $language['native_name'] ) . "</option>\n";
    2217                 }
    2218         }
    2219 
    2220         foreach ( $languages as $language ) {
    2221                 printf( '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
    2222                         esc_attr( $language['language'] ),
    2223                         esc_attr( $language['iso'][1] ),
    2224                         esc_attr( $language['strings']['continue'] ),
    2225                         in_array( $language['language'], $installed_languages ) ? ' data-installed="1"' : '',
    2226                         esc_html( $language['native_name'] ) );
    2227         }
    2228         echo "</select>\n";
    2229         echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
    2230 }
    2231 
    2232 /**
    2233  * Get available translations from the WordPress.org API.
    2234  *
    2235  * @since 4.0.0
    2236  *
    2237  * @see wp_remote_post()
    2238  *
    2239  * @return array Array of translations, each an array of data.
    2240  */
    2241 function wp_get_available_translations_from_api() {
    2242         $url = 'http://api.wordpress.org/translations/core/1.0/';
    2243         if ( wp_http_supports( array( 'ssl' ) ) ) {
    2244                 $url = set_url_scheme( $url, 'https' );
    2245         }
    2246 
    2247         $options = array(
    2248                 'timeout' => 3,
    2249                 'body' => array( 'version' => $GLOBALS['wp_version'] ),
    2250         );
    2251 
    2252         $response = wp_remote_post( $url, $options );
    2253         $body = wp_remote_retrieve_body( $response );
    2254         if ( $body && $body = json_decode( $body, true ) ) {
    2255                 $translations = array();
    2256                 // Key the array with the language code for now
    2257                 foreach ( $body['translations'] as $translation ) {
    2258                         $translations[ $translation['language'] ] = $translation;
    2259                 }
    2260                 return $translations;
    2261         }
    2262         return false;
    2263 }
    2264 
    2265 /**
    2266  * Download a language pack.
    2267  *
    2268  * @since 4.0.0
    2269  *
    2270  * @see wp_get_available_translations_from_api()
    2271  *
    2272  * @param string $download Language code to download.
    2273  * @return string|bool Returns the language code if successfully downloaded
    2274  *                     (or already installed), or false on failure.
    2275  */
    2276 function wp_install_download_language_pack( $download ) {
    2277         // Check if the translation is already installed.
    2278         if ( in_array( $download, get_available_languages() ) ) {
    2279                 return $download;
    2280         }
    2281 
    2282         // Confirm the translation is one we can download.
    2283         $translations = wp_get_available_translations_from_api();
    2284         if ( ! $translations ) {
    2285                 return false;
    2286         }
    2287         foreach ( $translations as $translation ) {
    2288                 if ( $translation['language'] === $download ) {
    2289                         $translation_to_load = true;
    2290                         break;
    2291                 }
    2292         }
    2293 
    2294         if ( empty( $translation_to_load ) ) {
    2295                 return false;
    2296         }
    2297         $translation = (object) $translation;
    2298 
    2299         require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    2300         $skin = new Automatic_Upgrader_Skin;
    2301         $upgrader = new Language_Pack_Upgrader( $skin );
    2302         $translation->type = 'core';
    2303         /**
    2304          * @todo failures (such as non-direct FS)
    2305          */
    2306         $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
    2307         return $translation->language;
    2308 }
    2309 
    2310 /**
    2311  * Load a translation during the install process.
    2312  *
    2313  * @since 4.0.0
    2314  *
    2315  * @see load_textdomain()
    2316  *
    2317  * @param string $translation Translation to load.
    2318  * @return string|bool Returns the language code if successfully loaded,
    2319  *                     or false on failure.
    2320  */
    2321 function wp_install_load_language( $translation ) {
    2322         if ( ! empty( $translation ) ) {
    2323                 if ( in_array( $translation, get_available_languages() ) ) {
    2324                         $translation_to_load = $translation;
    2325                 }
    2326         }
    2327 
    2328         if ( empty( $translation_to_load ) ) {
    2329                 return false;
    2330         }
    2331 
    2332         unload_textdomain( 'default' ); // Start over.
    2333         load_textdomain( 'default', WP_LANG_DIR . "/{$translation_to_load}.mo" );
    2334         load_textdomain( 'default', WP_LANG_DIR . "/admin-{$translation_to_load}.mo" );
    2335         return $translation_to_load;
    2336 }
  • src/wp-admin/install.php

     
    3838/** Load WordPress Administration Upgrade API */
    3939require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    4040
     41/** Load WordPress Translation Install API */
     42require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     43
    4144/** Load wpdb */
    4245require_once( ABSPATH . WPINC . '/wp-db.php' );
    4346
     
    181184switch($step) {
    182185        case 0: // Step 0
    183186
    184                 if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations_from_api() ) ) {
     187                if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations() ) ) {
    185188                        display_header( 'language-chooser' );
    186189                        echo '<form id="setup" method="post" action="?step=1">';
    187190                        wp_install_language_form( $languages );
     
    193196
    194197        case 1: // Step 1, direct link or from language chooser.
    195198                if ( ! empty( $_REQUEST['language'] ) ) {
    196                         $loaded_language = wp_install_download_language_pack( $_REQUEST['language'] );
     199                        $loaded_language = wp_download_language_pack( $_REQUEST['language'] );
    197200                        if ( $loaded_language ) {
    198                                 wp_install_load_language( $loaded_language );
     201                                wp_switch_default_textdomain( $loaded_language );
    199202                        }
    200203                }
    201204
     
    212215                break;
    213216        case 2:
    214217                if ( !empty( $_REQUEST['language'] ) ) {
    215                         $loaded_language = wp_install_load_language( $_REQUEST['language'] );
     218                        $loaded_language = wp_switch_default_textdomain( $_REQUEST['language'] );
    216219                } else {
    217220                        $loaded_language = 'en_US';
    218221                }
  • src/wp-admin/network/settings.php

     
    102102                                        <input name="admin_email" type="email" id="admin_email" class="regular-text" value="<?php echo esc_attr( get_site_option( 'admin_email' ) ) ?>" />
    103103                                        <p class="description">
    104104                                                <?php _e( 'This email address will receive notifications. Registration and support emails will also come from this address.' ); ?>
    105                                         </p>   
     105                                        </p>
    106106                                </td>
    107107                        </tr>
    108108                </table>
     
    165165<?php echo esc_textarea( $limited_email_domains == '' ? '' : implode( "\n", (array) $limited_email_domains ) ); ?></textarea>
    166166                                        <p class="description">
    167167                                                <?php _e( 'If you want to limit site registrations to certain domains. One domain per line.' ) ?>
    168                                         </p>   
     168                                        </p>
    169169                                </td>
    170170                        </tr>
    171171
     
    231231<?php echo esc_textarea( get_site_option( 'first_comment' ) ) ?></textarea>
    232232                                        <p class="description">
    233233                                                <?php _e( 'The first comment on a new site.' ) ?>
    234                                         </p>   
     234                                        </p>
    235235                                </td>
    236236                        </tr>
    237237                        <tr>
     
    273273                        </tr>
    274274                </table>
    275275
    276 <?php
     276                <?php
    277277                $languages = get_available_languages();
    278278                if ( ! empty( $languages ) ) {
    279                         $lang = get_site_option( 'WPLANG' );
    280 ?>
    281                 <h3><?php _e( 'Language Settings' ); ?></h3>
    282                 <table class="form-table">
     279                        ?>
     280                        <h3><?php _e( 'Language Settings' ); ?></h3>
     281                        <table class="form-table">
    283282                                <tr>
    284283                                        <th><label for="WPLANG"><?php _e( 'Default Language' ); ?></label></th>
    285284                                        <td>
    286                                                 <select name="WPLANG" id="WPLANG">
    287                                                         <?php mu_dropdown_languages( $languages, get_site_option( 'WPLANG' ) ); ?>
    288                                                 </select>
     285                                                <?php
     286                                                $lang = get_site_option( 'WPLANG' );
     287                                                if ( ! in_array( $lang, $languages ) ) {
     288                                                        $lang = '';
     289                                                }
     290
     291                                                wp_dropdown_languages( array(
     292                                                        'name'      => 'WPLANG',
     293                                                        'id'        => 'WPLANG',
     294                                                        'selected'  => $lang,
     295                                                        'languages' => $languages,
     296                                                ) );
     297                                                ?>
    289298                                        </td>
    290299                                </tr>
    291                 </table>
    292 <?php
    293                 } // languages
    294 ?>
     300                        </table>
     301                        <?php
     302                }
     303                ?>
    295304
    296305                <h3><?php _e( 'Menu Settings' ); ?></h3>
    297306                <table id="menu" class="form-table">
     
    324333                        </tr>
    325334                </table>
    326335
    327                 <?php 
     336                <?php
    328337                /**
    329338                 * Fires at the end of the Network Settings form, before the submit button.
    330339                 *
  • src/wp-admin/options-general.php

     
    302302</select></td>
    303303</tr>
    304304<?php do_settings_fields('general', 'default'); ?>
     305
    305306<?php
    306         $languages = get_available_languages();
    307         if ( $languages ) :
    308 ?>
     307$languages = get_available_languages();
     308if ( ! empty( $languages ) ) {
     309        ?>
    309310        <tr>
    310                 <th width="33%" scope="row"><label for="WPLANG"><?php _e('Site Language') ?></label></th>
     311                <th width="33%" scope="row"><label for="WPLANG"><?php _e( 'Site Language' ); ?></label></th>
    311312                <td>
    312                         <?php wp_dropdown_languages( array(
     313                        <?php
     314                        $locale = get_locale();
     315                        // Check if current locale is still installed. en_US will fail this test, but that's okay.
     316                        if ( ! in_array( $locale, $languages ) ) {
     317                                $locale = '';
     318                        }
     319
     320                        wp_dropdown_languages( array(
    313321                                'name'      => 'WPLANG',
    314322                                'id'        => 'WPLANG',
    315                                 'selected'  => get_option( 'WPLANG' ),
     323                                'selected'  => $locale,
    316324                                'languages' => $languages,
    317                         ) ); ?>
     325                        ) );
     326
     327                        // Add note about deprecated WPLANG constant.
     328                        if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && $locale !== WPLANG && is_super_admin() ) {
     329                                ?>
     330                                <p class="description">
     331                                        <?php _e( '<strong>Note:</strong> The <code>WPLANG</code> constant in your <code>wp-config.php</code> file is deprecated.' ); ?>
     332                                </p>
     333                                <?php
     334                        }
     335                        ?>
    318336                </td>
    319337        </tr>
    320 <?php
    321         endif;
     338        <?php
     339}
    322340?>
    323341</table>
    324342
  • src/wp-admin/options.php

     
    151151                $options = $whitelist_options[ $option_page ];
    152152        }
    153153
    154         // Handle custom date/time formats
     154        // Handle custom date/time formats.
    155155        if ( 'general' == $option_page ) {
    156156                if ( !empty($_POST['date_format']) && isset($_POST['date_format_custom']) && '\c\u\s\t\o\m' == wp_unslash( $_POST['date_format'] ) )
    157157                        $_POST['date_format'] = $_POST['date_format_custom'];
     
    180180                        }
    181181                        update_option( $option, $value );
    182182                }
     183
     184                // Switch translation in case WPLANG was changed.
     185                $language = wp_unslash( trim( $_POST[ 'WPLANG' ] ) );
     186                if ( $language ) {
     187                        wp_switch_default_textdomain( $language );
     188                } else {
     189                        unload_textdomain( 'default' );
     190                }
    183191        }
    184192
    185193        /**
  • src/wp-admin/setup-config.php

     
    3232
    3333require( ABSPATH . 'wp-settings.php' );
    3434
    35 require( ABSPATH . 'wp-admin/includes/upgrade.php' );
     35/** Load WordPress Administration Upgrade API */
     36require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    3637
     38/** Load WordPress Translation Install API */
     39require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     40
    3741nocache_headers();
    3842
    3943// Support wp-config-sample.php one level up, for the develop repo.
     
    8892switch($step) {
    8993        case -1:
    9094
    91                 if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations_from_api() ) ) {
     95                if ( empty( $_GET['language'] ) && ( $languages = wp_get_available_translations() ) ) {
    9296                        setup_config_display_header( 'language-chooser' );
    9397                        echo '<form id="setup" method="post" action="?step=0">';
    9498                        wp_install_language_form( $languages );
     
    100104
    101105        case 0:
    102106                if ( ! empty( $_REQUEST['language'] ) ) {
    103                         $loaded_language = wp_install_download_language_pack( $_REQUEST['language'] );
     107                        $loaded_language = wp_download_language_pack( $_REQUEST['language'] );
    104108                        if ( $loaded_language ) {
    105                                 wp_install_load_language( $loaded_language );
     109                                wp_switch_default_textdomain( $loaded_language );
    106110                        }
    107111                }
    108112
     
    136140        break;
    137141
    138142        case 1:
    139                 $loaded_language = wp_install_load_language( $_REQUEST['language'] );
     143                $loaded_language = wp_switch_default_textdomain( $_REQUEST['language'] );
    140144                setup_config_display_header();
    141145        ?>
    142146<form method="post" action="setup-config.php?step=2">
     
    176180        break;
    177181
    178182        case 2:
    179         $loaded_language = wp_install_load_language( $_REQUEST['language'] );
     183        $loaded_language = wp_switch_default_textdomain( $_REQUEST['language'] );
    180184        $dbname = trim( wp_unslash( $_POST[ 'dbname' ] ) );
    181185        $uname = trim( wp_unslash( $_POST[ 'uname' ] ) );
    182186        $pwd = trim( wp_unslash( $_POST[ 'pwd' ] ) );
  • src/wp-includes/l10n.php

     
    2424 * @return string The locale of the blog or from the 'locale' hook.
    2525 */
    2626function get_locale() {
    27         global $locale;
     27        global $locale, $wp_local_package;
    2828
    2929        if ( isset( $locale ) ) {
    3030                /**
     
    3838        }
    3939
    4040        // WPLANG is defined in wp-config.
    41         if ( defined( 'WPLANG' ) )
     41        if ( defined( 'WPLANG' ) ) {
    4242                $locale = WPLANG;
     43        }
    4344
     45        if ( isset( $wp_local_package ) ) {
     46                $locale = $wp_local_package;
     47        }
     48
    4449        // If multisite, check options.
    4550        if ( is_multisite() ) {
    4651                // Don't check blog option when installing.
    47                 if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
    48                         $ms_locale = get_site_option('WPLANG');
     52                if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
     53                        $ms_locale = get_site_option( 'WPLANG' );
     54                }
    4955
    50                 if ( $ms_locale !== false )
     56                if ( $ms_locale !== false ) {
    5157                        $locale = $ms_locale;
    52         } elseif ( ! defined( 'WP_INSTALLING' ) ) {
     58                }
     59        } else {
    5360                $db_locale = get_option( 'WPLANG' );
    54                 if ( $db_locale ) {
     61                if ( $db_locale !== false ) {
    5562                        $locale = $db_locale;
    5663                }
    5764        }
    5865
    59         if ( empty( $locale ) )
     66        if ( empty( $locale ) ) {
    6067                $locale = 'en_US';
     68        }
    6169
    6270        /** This filter is documented in wp-includes/l10n.php */
    6371        return apply_filters( 'locale', $locale );
     
    523531 * @see load_textdomain()
    524532 *
    525533 * @since 1.5.0
     534 *
     535 * @param string $locale Optional. Locale to load. Defaults to get_locale().
    526536 */
    527 function load_default_textdomain() {
    528         $locale = get_locale();
     537function load_default_textdomain( $locale = null ) {
     538        if ( null === $locale ) {
     539                $locale = get_locale();
     540        }
    529541
    530         load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
     542        $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
    531543
    532544        if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists(  WP_LANG_DIR . "/admin-$locale.mo" ) ) {
    533545                load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
    534                 return;
     546                return $return;
    535547        }
    536548
    537549        if ( is_admin() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) )
     
    540552        if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
    541553                load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
    542554
     555        return $return;
    543556}
    544557
    545558/**
     
    818831}
    819832
    820833/**
    821  * Language selector. More to come.
     834 * Language selector.
    822835 *
    823836 * @since 4.0.0
    824837 *
    825838 * @see get_available_languages()
     839 * @see wp_get_available_translations()
    826840 *
    827841 * @param array $args Optional arguments. Default empty array.
    828842 */
    829843function wp_dropdown_languages( $args = array() ) {
    830         if ( isset( $args['languages'] ) ) {
    831                 $languages = $args['languages'];
    832         } else {
    833                 $languages = get_available_languages();
     844        require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     845
     846        $args = wp_parse_args( $args, array(
     847                'id'        => '',
     848                'name'      => '',
     849                'languages' => array(),
     850                'selected'  => ''
     851        ) );
     852
     853        if ( empty( $args['languages'] ) ) {
     854                return false;
    834855        }
    835856
     857        $translations = wp_get_available_translations();
     858
     859        /*
     860         * $args['languages'] should only contain the locales. Find the locale in
     861         * $translations to get the native name. Falls back to locale.
     862         */
     863        $languages = array();
     864        foreach ( $args['languages'] as $locale ) {
     865                if ( isset( $translations[ $locale ] ) ) {
     866                        $translation = $translations[ $locale ];
     867                        $languages[] = $translation;
     868                } else {
     869                        $languages[] = array(
     870                                'language'    => $locale,
     871                                'native_name' => $locale,
     872                                'lang'        => '',
     873                        );
     874                }
     875        }
     876
    836877        printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
    837         echo '<option value="">en_US</option>';
     878
     879        // List installed languages.
     880        echo '<option value="">English (United States)</option>';
    838881        foreach ( $languages as $language ) {
    839                 $selected = selected( $language, $args['selected'], false );
    840                 echo '<option value="' . esc_attr( $language ) .'"' . $selected . '>' . $language . '</option>';
     882                $selected = selected( $language['language'], $args['selected'], false );
     883                printf(
     884                        '<option value="%s" lang="%s"%s>%s</option>',
     885                        esc_attr( $language['language'] ),
     886                        esc_attr( $language['iso'][1] ),
     887                        $selected,
     888                        esc_html( $language['native_name'] )
     889                );
    841890        }
     891
    842892        echo '</select>';
    843893}
     894
     895/**
     896 * Switch translation for default translation.
     897 *
     898 * @since 4.0.0
     899 *
     900 * @see load_default_textdomain()
     901 *
     902 * @param string $translation Translation to load.
     903 *  string|bool Returns the language code if successfully loaded,
     904 *              or false on failure.
     905 */
     906function wp_switch_default_textdomain( $translation ) {
     907        if ( ! empty( $translation ) ) {
     908                if ( in_array( $translation, get_available_languages() ) ) {
     909                        $translation_to_load = $translation;
     910                }
     911        }
     912
     913
     914        if ( empty( $translation_to_load ) ) {
     915                return false;
     916        }
     917
     918        if ( $translation_to_load === get_locale() ) {
     919                return false;
     920        }
     921
     922        unload_textdomain( 'default' ); // Start over.
     923        $result = load_default_textdomain( $translation_to_load );
     924
     925        if ( ! $result ) {
     926                return false;
     927        }
     928
     929        return $translation_to_load;
     930}
  • wp-config-sample.php

     
    6262$table_prefix  = 'wp_';
    6363
    6464/**
    65  * WordPress Localized Language, defaults to English.
    66  *
    67  * Change this to localize WordPress. A corresponding MO file for the chosen
    68  * language must be installed to wp-content/languages. For example, install
    69  * de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
    70  * language support.
    71  */
    72 define('WPLANG', '');
    73 
    74 /**
    7565 * For developers: WordPress debugging mode.
    7666 *
    7767 * Change this to true to enable the display of notices during development.