WordPress.org

Make WordPress Core

Changeset 52058


Ignore:
Timestamp:
11/08/2021 10:36:11 PM (4 weeks ago)
Author:
joedolson
Message:

I18N: Add language switcher on login/registration screens.

Load a language switcher on the login and registration screens that allows users to choose any already-installed language. Set user locale on registration.

Props johnbillion, Nikschavan, afercia, sabernhardt, garrett-eclipse, keyur5, paaljoachim, Clorith, tobifjellner.
Fixes #43700.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/login.css

    r51727 r52058  
    282282#login {
    283283    width: 320px;
    284     padding: 8% 0 0;
     284    padding: 5% 0 0;
    285285    margin: auto;
    286286}
     
    322322    text-align: center;
    323323    width: 100%;
    324     margin: 5em 0 2em;
     324    margin: 3em 0 2em;
    325325}
    326326
     
    410410}
    411411
     412#language-switcher {
     413    padding: 0;
     414    overflow: visible;
     415    background: none;
     416    border: none;
     417    box-shadow: none;
     418}
     419
     420#language-switcher select {
     421    margin-right: 0.25em;
     422}
     423
     424.language-switcher {
     425    margin: 0 auto;
     426    padding: 0 0 24px;
     427    text-align: center;
     428}
     429
     430.language-switcher label {
     431    margin-right: 0.25em;
     432}
     433
     434.language-switcher label .dashicons {
     435    width: auto;
     436    height: auto;
     437}
     438
     439.login .language-switcher .button-primary {
     440    float: none;
     441    margin-bottom: 0;
     442}
     443
    412444@media screen and (max-height: 550px) {
    413445    #login {
    414446        padding: 20px 0;
     447    }
     448
     449    #language-switcher {
     450        margin-top: 0;
    415451    }
    416452}
     
    428464        margin: -0.1875rem 0 0 -0.25rem;
    429465    }
    430 }
     466
     467    #language-switcher label,
     468    #language-switcher select {
     469        margin-right: 0;
     470    }
     471}
     472
     473@media screen and (max-width: 400px) {
     474    .login .language-switcher .button-primary {
     475        display: block;
     476        margin: 5px auto 0;
     477    }
     478}
  • trunk/src/wp-includes/l10n.php

    r51837 r52058  
    145145    }
    146146
    147     if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
    148         $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
     147    $wp_lang = '';
     148
     149    if ( ! empty( $_GET['wp_lang'] ) ) {
     150        $wp_lang = sanitize_text_field( $_GET['wp_lang'] );
     151    } elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
     152        $wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
     153    }
     154
     155    if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
     156        $determined_locale = $wp_lang;
    149157    }
    150158
     
    14811489 * @since 4.7.0 Introduced the `show_option_site_default` argument.
    14821490 * @since 5.1.0 Introduced the `show_option_en_us` argument.
     1491 * @since 5.9.0 Introduced the `explicit_option_en_us` argument.
    14831492 *
    14841493 * @see get_available_languages()
     
    15001509 *     @type bool     $show_option_site_default     Whether to show an option to fall back to the site's locale. Default false.
    15011510 *     @type bool     $show_option_en_us            Whether to show an option for English (United States). Default true.
     1511 *     @type bool     $explicit_option_en_us        Whether the English (United States) option uses an explict value of en_US
     1512 *                                                  instead of an empty value. Default false.
    15021513 * }
    15031514 * @return string HTML dropdown list of languages.
     
    15171528            'show_option_site_default'    => false,
    15181529            'show_option_en_us'           => true,
     1530            'explicit_option_en_us'       => false,
    15191531        )
    15201532    );
     
    15261538
    15271539    // English (United States) uses an empty string for the value attribute.
    1528     if ( 'en_US' === $parsed_args['selected'] ) {
     1540    if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {
    15291541        $parsed_args['selected'] = '';
    15301542    }
     
    15811593
    15821594    if ( $parsed_args['show_option_en_us'] ) {
     1595        $value = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';
    15831596        $structure[] = sprintf(
    1584             '<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
     1597            '<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>',
     1598            esc_attr( $value ),
    15851599            selected( '', $parsed_args['selected'], false )
    15861600        );
  • trunk/src/wp-includes/user.php

    r51943 r52058  
    30453045    update_user_meta( $user_id, 'default_password_nag', true ); // Set up the password change nag.
    30463046
     3047    if ( ! empty( $_COOKIE['wp_lang'] ) ) {
     3048        $wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
     3049        if ( in_array( $wp_lang, get_available_languages(), true ) ) {
     3050            update_user_meta( $user_id, 'locale', $wp_lang ); // Set user locale if defined on registration.
     3051        }
     3052    }
     3053
    30473054    /**
    30483055     * Fires after a new user registration has been recorded.
  • trunk/src/wp-login.php

    r51955 r52058  
    313313
    314314    <?php
     315    $languages = get_available_languages();
     316
     317    if ( ! empty( $languages ) && ! $interim_login ) { ?>
     318
     319        <div class="language-switcher">
     320            <form id="language-switcher" action="" method="get">
     321
     322                <label for="language-switcher-locales">
     323                    <span class="dashicons dashicons-translation" aria-hidden="true"></span>
     324                    <span class="screen-reader-text"><?php _e( 'Language' ); ?></span>
     325                </label>
     326
     327                <?php
     328                    $args = array(
     329                        'id'                          => 'language-switcher-locales',
     330                        'name'                        => 'wp_lang',
     331                        'selected'                    => determine_locale(),
     332                        'show_available_translations' => false,
     333                        'explicit_option_en_us'       => true,
     334                        'languages'                   => $languages,
     335                    );
     336
     337                    /**
     338                     * Filters default arguments for the Languages select input on the login screen.
     339                     *
     340                     * @since 5.9.0
     341                     *
     342                     * @param array $args Arguments for the Languages select input on the login screen.
     343                     */
     344                    wp_dropdown_languages( apply_filters( 'wp_login_language_switcher_args', $args ) );
     345                ?>
     346
     347                <?php if ( $interim_login ) { ?>
     348                    <input type="hidden" name="interim-login" value="1" />
     349                <?php } ?>
     350
     351                <?php if ( isset( $_GET['redirect_to'] ) && '' !== $_GET['redirect_to'] ) { ?>
     352                    <input type="hidden" name="redirect_to" value="<?php echo esc_url_raw( $_GET['redirect_to'] ); ?>" />
     353                <?php } ?>
     354
     355                <?php if ( isset( $_GET['action'] ) && '' !== $_GET['action'] ) { ?>
     356                    <input type="hidden" name="action" value="<?php echo esc_attr( $_GET['action'] ); ?>" />
     357                <?php } ?>
     358
     359                    <input type="submit" class="button button-primary" value="<?php esc_attr_e( 'Change' ); ?>">
     360
     361                </form>
     362            </div>
     363
     364<?php } ?>
     365
     366    <?php
    315367
    316368    if ( ! empty( $input_id ) ) {
     
    418470if ( SITECOOKIEPATH !== COOKIEPATH ) {
    419471    setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
     472}
     473
     474if ( isset( $_GET['wp_lang'] ) ) {
     475    setcookie( 'wp_lang', sanitize_text_field( $_GET['wp_lang'] ), 0, COOKIEPATH, COOKIE_DOMAIN, $secure );
    420476}
    421477
Note: See TracChangeset for help on using the changeset viewer.