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-includes/l10n.php

    r29163 r29630  
    2525 */
    2626function get_locale() {
    27     global $locale;
     27    global $locale, $wp_local_package;
    2828
    2929    if ( isset( $locale ) ) {
     
    3838    }
    3939
    40     // WPLANG is defined in wp-config.
    41     if ( defined( 'WPLANG' ) )
     40    if ( isset( $wp_local_package ) ) {
     41        $locale = $wp_local_package;
     42    }
     43
     44    // WPLANG was defined in wp-config.
     45    if ( defined( 'WPLANG' ) ) {
    4246        $locale = WPLANG;
     47    }
    4348
    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');
    49 
    50         if ( $ms_locale !== false )
     52        if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
     53            $ms_locale = get_site_option( 'WPLANG' );
     54        }
     55
     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 */
     
    524532 *
    525533 * @since 1.5.0
    526  */
    527 function load_default_textdomain() {
    528     $locale = get_locale();
    529 
    530     load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
     534 *
     535 * @param string $locale Optional. Locale to load. Defaults to get_locale().
     536 */
     537function load_default_textdomain( $locale = null ) {
     538    if ( null === $locale ) {
     539        $locale = get_locale();
     540    }
     541
     542    // Unload previously loaded strings so we can switch translations.
     543    unload_textdomain( 'default' );
     544
     545    $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
    531546
    532547    if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists(  WP_LANG_DIR . "/admin-$locale.mo" ) ) {
    533548        load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
    534         return;
    535     }
    536 
    537     if ( is_admin() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) )
     549        return $return;
     550    }
     551
     552    if ( is_admin() || defined( 'WP_INSTALLING' ) || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
    538553        load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
     554    }
    539555
    540556    if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
    541557        load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
    542558
     559    return $return;
    543560}
    544561
     
    819836
    820837/**
    821  * Language selector. More to come.
     838 * Language selector.
    822839 *
    823840 * @since 4.0.0
    824841 *
    825842 * @see get_available_languages()
     843 * @see wp_get_available_translations()
    826844 *
    827845 * @param array $args Optional arguments. Default empty array.
    828846 */
    829847function wp_dropdown_languages( $args = array() ) {
    830     if ( isset( $args['languages'] ) ) {
    831         $languages = $args['languages'];
    832     } else {
    833         $languages = get_available_languages();
     848    require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     849
     850    $args = wp_parse_args( $args, array(
     851        'id'        => '',
     852        'name'      => '',
     853        'languages' => array(),
     854        'selected'  => ''
     855    ) );
     856
     857    if ( empty( $args['languages'] ) ) {
     858        return false;
     859    }
     860
     861    $translations = wp_get_available_translations();
     862
     863    /*
     864     * $args['languages'] should only contain the locales. Find the locale in
     865     * $translations to get the native name. Fall back to locale.
     866     */
     867    $languages = array();
     868    foreach ( $args['languages'] as $locale ) {
     869        if ( isset( $translations[ $locale ] ) ) {
     870            $translation = $translations[ $locale ];
     871            $languages[] = array(
     872                'language'    => $translation['language'],
     873                'native_name' => $translation['native_name'],
     874                'lang'        => $translation['iso'][1],
     875            );
     876        } else {
     877            $languages[] = array(
     878                'language'    => $locale,
     879                'native_name' => $locale,
     880                'lang'        => '',
     881            );
     882        }
    834883    }
    835884
    836885    printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
    837     echo '<option value="">en_US</option>';
     886
     887    // List installed languages.
     888    echo '<option value="" lang="en">English (United States)</option>';
    838889    foreach ( $languages as $language ) {
    839         $selected = selected( $language, $args['selected'], false );
    840         echo '<option value="' . esc_attr( $language ) .'"' . $selected . '>' . $language . '</option>';
    841     }
     890        $selected = selected( $language['language'], $args['selected'], false );
     891        printf(
     892            '<option value="%s" lang="%s"%s>%s</option>',
     893            esc_attr( $language['language'] ),
     894            esc_attr( $language['lang'] ),
     895            $selected,
     896            esc_html( $language['native_name'] )
     897        );
     898    }
     899
    842900    echo '</select>';
    843901}
Note: See TracChangeset for help on using the changeset viewer.