Make WordPress Core

Changeset 38705


Ignore:
Timestamp:
10/03/2016 07:03:41 AM (10 years ago)
Author:
swissspidy
Message:

I18N: Introduce a user-specific language setting.

By enabling the user to select their preferred locale when editing the profile, we allow for greater personalization of the WordPress admin and therefore a better user experience.

The back end will be displayed in the user's individual locale while the locale used on the front end equals the one set for the whole site. If the user didn't specify a locale, the site's locale will be used as a fallback. The new locale property of the WP_User class can be used to retrieve the user's locale setting.

Props ocean90, ipm-frommen, swissspidy.
Fixes #29783.

Location:
trunk
Files:
1 added
19 edited

Legend:

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

    r38459 r38705  
    157157$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', get_bloginfo( 'version' ) ) );
    158158$admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    159 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     159$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    160160
    161161if ( wp_is_mobile() )
  • trunk/src/wp-admin/includes/class-wp-plugin-install-list-table.php

    r38672 r38705  
    135135                        ),
    136136                        // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
    137                         'locale' => get_locale(),
     137                        'locale' => get_user_locale(),
    138138                        'installed_plugins' => $this->get_installed_plugin_slugs(),
    139139                );
  • trunk/src/wp-admin/includes/class-wp-press-this.php

    r38468 r38705  
    13131313        $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
    13141314        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    1315         $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     1315        $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    13161316
    13171317        /** This filter is documented in wp-admin/admin-header.php */
  • trunk/src/wp-admin/includes/credits.php

    r38459 r38705  
    1717function wp_credits() {
    1818        $wp_version = get_bloginfo( 'version' );
    19         $locale = get_locale();
     19        $locale = get_user_locale();
    2020
    2121        $results = get_site_transient( 'wordpress_credits_' . $locale );
  • trunk/src/wp-admin/includes/dashboard.php

    r38672 r38705  
    13541354
    13551355                $browsehappy = 'http://browsehappy.com/';
    1356                 $locale = get_locale();
     1356                $locale = get_user_locale();
    13571357                if ( 'en_US' !== $locale )
    13581358                        $browsehappy = add_query_arg( 'locale', $locale, $browsehappy );
  • trunk/src/wp-admin/includes/import.php

    r38075 r38705  
    126126        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
    127127
    128         $locale = get_locale();
     128        $locale = get_user_locale();
    129129        $cache_key = 'popular_importers_' . md5( $locale . $wp_version );
    130130        $popular_importers = get_site_transient( $cache_key );
     
    132132        if ( ! $popular_importers ) {
    133133                $url = add_query_arg( array(
    134                         'locale'  => get_locale(),
     134                        'locale'  => get_user_locale(),
    135135                        'version' => $wp_version,
    136136                ), 'http://api.wordpress.org/core/importers/1.1/' );
  • trunk/src/wp-admin/includes/plugin-install.php

    r38672 r38705  
    110110
    111111        if ( ! isset( $args->locale ) ) {
    112                 $args->locale = get_locale();
     112                $args->locale = get_user_locale();
    113113        }
    114114
  • trunk/src/wp-admin/includes/template.php

    r38672 r38705  
    16231623do_action( 'admin_head' );
    16241624
    1625 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     1625$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    16261626
    16271627if ( is_rtl() )
  • trunk/src/wp-admin/includes/theme.php

    r38623 r38705  
    413413
    414414        if ( ! isset( $args->locale ) ) {
    415                 $args->locale = get_locale();
     415                $args->locale = get_user_locale();
    416416        }
    417417
  • trunk/src/wp-admin/includes/user.php

    r38369 r38705  
    9595                $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
    9696                $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
     97                $user->locale = '';
     98
     99                if ( isset( $_POST['locale'] ) ) {
     100                        $locale = sanitize_text_field( $_POST['locale'] );
     101                        if ( ! in_array( $locale, get_available_languages(), true ) ) {
     102                                $locale = '';
     103                        }
     104
     105                        $user->locale = ( '' === $locale ) ? 'en_US' : $locale;
     106                }
    97107        }
    98108
  • trunk/src/wp-admin/options.php

    r38032 r38705  
    210210                        if ( isset( $_POST[ $option ] ) ) {
    211211                                $value = $_POST[ $option ];
    212                                 if ( ! is_array( $value ) )
     212                                if ( ! is_array( $value ) ) {
    213213                                        $value = trim( $value );
     214                                }
    214215                                $value = wp_unslash( $value );
    215216                        }
     
    218219
    219220                // Switch translation in case WPLANG was changed.
    220                 $language = get_option( 'WPLANG' );
    221                 if ( $language ) {
    222                         load_default_textdomain( $language );
    223                 } else {
    224                         unload_textdomain( 'default' );
     221                $language      = get_option( 'WPLANG' );
     222                $user_language = get_user_locale();
     223                if ( $language === $user_language ) {
     224                        if ( $language ) {
     225                                load_default_textdomain( $language );
     226                        } else {
     227                                unload_textdomain( 'default' );
     228                        }
    225229                }
    226230        }
  • trunk/src/wp-admin/plugin-editor.php

    r38672 r38705  
    261261                </div>
    262262                <?php if ( !empty( $docs_select ) ) : ?>
    263                 <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" /></div>
     263                <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" /></div>
    264264                <?php endif; ?>
    265265<?php if ( is_writeable($real_file) ) : ?>
  • trunk/src/wp-admin/theme-editor.php

    r38672 r38705  
    264264                <label for="docs-list"><?php _e('Documentation:') ?></label>
    265265                <?php echo $docs_select; ?>
    266                 <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" />
     266                <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ); ?>" onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_user_locale() ) ?>&amp;version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&amp;redirect=true'); }" />
    267267                </div>
    268268        <?php endif; ?>
  • trunk/src/wp-admin/user-edit.php

    r38672 r38705  
    270270</td>
    271271</tr>
     272
     273<?php
     274$languages = get_available_languages();
     275if ( $languages ) : ?>
     276<tr class="user-language-wrap">
     277        <th scope="row">
     278                <label for="site_language"><?php _e( 'Site Language' ); ?></label>
     279        </th>
     280        <td>
     281                <?php
     282                $user_locale = get_user_option( 'locale', $profileuser->ID );
     283
     284                if ( 'en_US' === $user_locale ) { // en_US
     285                        $user_locale = false;
     286                } elseif ( ! in_array( $user_locale, $languages, true ) ) {
     287                        $user_locale = get_locale();
     288                }
     289
     290                wp_dropdown_languages( array(
     291                        'name'                        => 'locale',
     292                        'id'                          => 'locale',
     293                        'selected'                    => $user_locale,
     294                        'languages'                   => $languages,
     295                        'show_available_translations' => false
     296                ) );
     297                ?>
     298        </td>
     299</tr>
     300<?php
     301endif;
     302?>
     303
    272304<?php
    273305/**
  • trunk/src/wp-includes/class-wp-editor.php

    r38459 r38705  
    352352                                self::$baseurl = includes_url( 'js/tinymce' );
    353353
    354                                 $mce_locale = get_locale();
     354                                $mce_locale = get_user_locale();
    355355                                self::$mce_locale = $mce_locale = empty( $mce_locale ) ? 'en' : strtolower( substr( $mce_locale, 0, 2 ) ); // ISO 639-1
    356356
     
    673673                        }
    674674
    675                         $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     675                        $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    676676
    677677                        if ( !empty($set['tinymce']['body_class']) ) {
  • trunk/src/wp-includes/class-wp-theme.php

    r38121 r38705  
    13961396         */
    13971397        public static function sort_by_name( &$themes ) {
    1398                 if ( 0 === strpos( get_locale(), 'en_' ) ) {
     1398                if ( 0 === strpos( get_user_locale(), 'en_' ) ) {
    13991399                        uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
    14001400                } else {
  • trunk/src/wp-includes/class-wp-user.php

    r38319 r38705  
    3232 * @property string $spam
    3333 * @property string $deleted
     34 * @property string $locale
    3435 */
    3536class WP_User {
  • trunk/src/wp-includes/l10n.php

    r38438 r38705  
    7777
    7878/**
     79 * Retrieves the locale of the current user.
     80 *
     81 * If the user has a locale set to a non-empty string then it will be
     82 * returned. Otherwise it returns the locale of get_locale().
     83 *
     84 * @since 4.7.0
     85 *
     86 * @return string The locale of the current user.
     87 */
     88function get_user_locale() {
     89        $user = wp_get_current_user();
     90
     91        $locale = $user->locale;
     92        return ( '' === $locale ) ? get_locale() : $locale;
     93}
     94
     95/**
    7996 * Retrieve the translation of $text.
    8097 *
     
    634651function load_default_textdomain( $locale = null ) {
    635652        if ( null === $locale ) {
    636                 $locale = get_locale();
     653                $locale = is_admin() ? get_user_locale() : get_locale();
    637654        }
    638655
  • trunk/src/wp-includes/user.php

    r38682 r38705  
    13591359 * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact
    13601360 *              methods for new installs. See wp_get_user_contact_methods().
     1361 * @since 4.7.0 The user's locale can be passed to `$userdata`.
    13611362 *
    13621363 * @global wpdb $wpdb WordPress database abstraction object.
     
    13931394 *                                             site's front end. Default true.
    13941395 *     @type string      $role                 User's role.
     1396 *     @type string      $locale               User's locale. Default empty.
    13951397 * }
    13961398 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
     
    16061608
    16071609        $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
     1610
     1611        $meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : '';
    16081612
    16091613        $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
     
    19661970 */
    19671971function _get_additional_user_keys( $user ) {
    1968         $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
     1972        $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' );
    19691973        return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
    19701974}
Note: See TracChangeset for help on using the changeset viewer.