Make WordPress Core


Ignore:
Timestamp:
12/14/2018 01:31:27 AM (6 years ago)
Author:
jeremyfelt
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.

Merges [43776] from the 5.0 branch to trunk.

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/l10n.php

    r43571 r44134  
    103103    $locale = $user->locale;
    104104    return $locale ? $locale : get_locale();
     105}
     106
     107/**
     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 );
    105153}
    106154
     
    686734function load_default_textdomain( $locale = null ) {
    687735    if ( null === $locale ) {
    688         $locale = is_admin() ? get_user_locale() : get_locale();
     736        $locale = determine_locale();
    689737    }
    690738
     
    735783     * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    736784     */
    737     $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     785    $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    738786
    739787    $mofile = $domain . '-' . $locale . '.mo';
     
    769817function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
    770818    /** This filter is documented in wp-includes/l10n.php */
    771     $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     819    $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    772820
    773821    $mofile = $domain . '-' . $locale . '.mo';
     
    808856     * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    809857     */
    810     $locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
     858    $locale = apply_filters( 'theme_locale', determine_locale(), $domain );
    811859
    812860    $mofile = $domain . '-' . $locale . '.mo';
     
    942990    }
    943991
    944     $locale = is_admin() ? get_user_locale() : get_locale();
     992    $locale = determine_locale();
    945993    $mofile = "{$domain}-{$locale}.mo";
    946994
Note: See TracChangeset for help on using the changeset viewer.