WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/26/2014 07:58:33 PM (6 years ago)
Author:
ocean90
Message:

Language packs: No WPLANG anymore.

  • The WPLANG constant is no longer needed. Remove define('WPLANG', ); from wp-config-sample.php. Populate WPLANG option based on the WPLANG constant. When get_option('WPLANG') is an empty string it will override WPLANG.
  • Introduce translations_api() which is available to communicate with the translation API. Move translation install related functions to a new file.
  • Replace mu_dropdown_languages() with wp_dropdown_languages(). wp_dropdown_languages() is now populated by the translation API.
  • Remove wp_install_load_language() and allow load_default_textdomain() to switch a core translation.

fixes #13069, #15677, #19760, #28730, #29281.

File:
1 edited

Legend:

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

    r29372 r29630  
    437437    if ( $wp_current_db_version < 26691 )
    438438        upgrade_380();
     439
     440    if ( $wp_current_db_version < 29630 )
     441        upgrade_400();
    439442
    440443    maybe_disable_link_manager();
     
    13051308    }
    13061309}
     1310
     1311/**
     1312 * Execute changes made in WordPress 4.0.0.
     1313 *
     1314 * @since 4.0.0
     1315 */
     1316function upgrade_400() {
     1317    global $wp_current_db_version;
     1318    if ( $wp_current_db_version < 29630 ) {
     1319        if ( ! is_multisite() && false === get_option( 'WPLANG' ) ) {
     1320            if ( defined( 'WPLANG' ) && ( '' !== WPLANG ) && in_array( WPLANG, get_available_languages() ) ) {
     1321                update_option( 'WPLANG', WPLANG );
     1322            } else {
     1323                update_option( 'WPLANG', '' );
     1324            }
     1325        }
     1326    }
     1327}
     1328
    13071329/**
    13081330 * Execute network level changes
     
    14201442function maybe_create_table($table_name, $create_ddl) {
    14211443    global $wpdb;
    1422    
     1444
    14231445    $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $table_name ) );
    14241446
     
    21932215}
    21942216endif;
    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 }
Note: See TracChangeset for help on using the changeset viewer.