Make WordPress Core

Changeset 43776


Ignore:
Timestamp:
10/21/2018 03:02:49 PM (8 years ago)
Author:
danielbachhuber
Message:

REST API: Render response in user locale with ?_locale=user.

Introduces new determine_locale() function for deciding the proper locale to use for a response. Default value is get_user_locale() in the admin, and get_locale() on the frontend. Because REST API requests are considered frontend requests, ?_locale=user can be used to render the response in the user's locale.

Also updates wp-login.php?wp_lang implementation to benefit from this abstraction.

Props flixos90, mnelson4, swissspidy, TimothyBlynJacobs.
Fixes #44758.

Location:
branches/5.0/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/class-wp-locale-switcher.php

    r41289 r43776  
    4646         */
    4747        public function __construct() {
    48                 $this->original_locale     = is_admin() ? get_user_locale() : get_locale();
     48                $this->original_locale     = determine_locale();
    4949                $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() );
    5050        }
     
    6868         */
    6969        public function switch_to_locale( $locale ) {
    70                 $current_locale = is_admin() ? get_user_locale() : get_locale();
     70                $current_locale = determine_locale();
    7171                if ( $current_locale === $locale ) {
    7272                        return false;
  • branches/5.0/src/wp-includes/general-template.php

    r43761 r43776  
    704704                        $output = __( 'html_lang_attribute' );
    705705                        if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
    706                                 $output = is_admin() ? get_user_locale() : get_locale();
     706                                $output = determine_locale();
    707707                                $output = str_replace( '_', '-', $output );
    708708                        }
  • branches/5.0/src/wp-includes/l10n.php

    r43759 r43776  
    106106
    107107/**
     108 * Determine the current locale desired for the request.
     109 *
     110 * @since 5.0.0
     111 *
     112 * @global string $pagenow
     113 *
     114 * @return string The determined locale.
     115 */
     116function determine_locale() {
     117        /**
     118         * Filters the locale for the current request prior to the default determination process.
     119         *
     120         * Using this filter allows to override the default logic, effectively short-circuiting the function.
     121         *
     122         * @since 5.0.0
     123         *
     124         * @param string|null The locale to return and short-circuit, or null as default.
     125         */
     126        $determined_locale = apply_filters( 'pre_determine_locale', null );
     127        if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
     128                return $determined_locale;
     129        }
     130
     131        $determined_locale = get_locale();
     132
     133        if ( is_admin() ) {
     134                $determined_locale = get_user_locale();
     135        }
     136
     137        if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() && is_user_logged_in() ) {
     138                $determined_locale = get_user_locale();
     139        }
     140
     141        if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
     142                $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
     143        }
     144
     145        /**
     146         * Filters the locale for the current request.
     147         *
     148         * @since 5.0.0
     149         *
     150         * @param string $locale The locale.
     151         */
     152        return apply_filters( 'determine_locale', $determined_locale );
     153}
     154
     155/**
    108156 * Retrieve the translation of $text.
    109157 *
     
    664712function load_default_textdomain( $locale = null ) {
    665713        if ( null === $locale ) {
    666                 $locale = is_admin() ? get_user_locale() : get_locale();
     714                $locale = determine_locale();
    667715        }
    668716
     
    712760         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    713761         */
    714         $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     762        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    715763
    716764        $mofile = $domain . '-' . $locale . '.mo';
     
    746794function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
    747795        /** This filter is documented in wp-includes/l10n.php */
    748         $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     796        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    749797
    750798        $mofile = $domain . '-' . $locale . '.mo';
     
    785833         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    786834         */
    787         $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     835        $locale = apply_filters( 'theme_locale', determine_locale(), $domain );
    788836
    789837        $mofile = $domain . '-' . $locale . '.mo';
     
    916964        }
    917965
    918         $locale = is_admin() ? get_user_locale() : get_locale();
     966        $locale = determine_locale();
    919967        $mofile = "{$domain}-{$locale}.mo";
    920968
     
    12111259        );
    12121260
    1213         // List installed languages. 
     1261        // List installed languages.
    12141262        foreach ( $languages as $language ) {
    12151263                $structure[] = sprintf(
     
    13481396                '' => array(
    13491397                        'domain' => $domain,
    1350                         'lang'   => is_admin() ? get_user_locale() : get_locale(),
     1398                        'lang'   => determine_locale(),
    13511399                ),
    13521400        );
  • branches/5.0/src/wp-includes/script-loader.php

    r43760 r43776  
    906906
    907907        did_action( 'init' ) && $scripts->add_inline_script( 'mediaelement-core', sprintf( 'var mejsL10n = %s;', wp_json_encode( array(
    908                 'language' => strtolower( strtok( is_admin() ? get_user_locale() : get_locale(), '_-' ) ),
     908                'language' => strtolower( strtok( determine_locale(), '_-' ) ),
    909909                'strings'  => array(
    910910                        'mejs.install-flash'       => __( 'You are using a browser that does not have Flash player enabled or installed. Please turn on your Flash player plugin or download the latest version from https://get.adobe.com/flashplayer/' ),
  • branches/5.0/src/wp-login.php

    r43458 r43776  
    438438        setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
    439439
    440 $lang            = ! empty( $_GET['wp_lang'] ) ? sanitize_text_field( $_GET['wp_lang'] ) : '';
    441 $switched_locale = switch_to_locale( $lang );
    442 
    443440/**
    444441 * Fires when the login form is initialized.
     
    501498        setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
    502499
    503         if ( $switched_locale ) {
    504             restore_previous_locale();
    505         }
    506 
    507500        wp_safe_redirect( wp_get_referer() );
    508501        exit();
     
    520513                $redirect_to = 'wp-login.php?loggedout=true';
    521514                $requested_redirect_to = '';
    522         }
    523 
    524         if ( $switched_locale ) {
    525             restore_previous_locale();
    526515        }
    527516
     
    618607<?php
    619608login_footer('user_login');
    620 
    621 if ( $switched_locale ) {
    622     restore_previous_locale();
    623 }
    624609
    625610break;
     
    748733login_footer('user_pass');
    749734
    750 if ( $switched_locale ) {
    751     restore_previous_locale();
    752 }
    753 
    754735break;
    755736
     
    834815<?php
    835816login_footer('user_login');
    836 
    837 if ( $switched_locale ) {
    838     restore_previous_locale();
    839 }
    840817
    841818break;
     
    11201097login_footer();
    11211098
    1122 if ( $switched_locale ) {
    1123     restore_previous_locale();
    1124 }
    1125 
    11261099break;
    11271100} // end action switch
Note: See TracChangeset for help on using the changeset viewer.