WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/21/2018 03:02:49 PM (11 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    );
Note: See TracChangeset for help on using the changeset viewer.