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. Built from https://develop.svn.wordpress.org/trunk@44134 git-svn-id: http://core.svn.wordpress.org/trunk@43964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
313bc3fc73
commit
f93e24ca8e
|
@ -45,7 +45,7 @@ class WP_Locale_Switcher {
|
||||||
* @since 4.7.0
|
* @since 4.7.0
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->original_locale = is_admin() ? get_user_locale() : get_locale();
|
$this->original_locale = determine_locale();
|
||||||
$this->available_languages = array_merge( array( 'en_US' ), get_available_languages() );
|
$this->available_languages = array_merge( array( 'en_US' ), get_available_languages() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ class WP_Locale_Switcher {
|
||||||
* @return bool True on success, false on failure.
|
* @return bool True on success, false on failure.
|
||||||
*/
|
*/
|
||||||
public function switch_to_locale( $locale ) {
|
public function switch_to_locale( $locale ) {
|
||||||
$current_locale = is_admin() ? get_user_locale() : get_locale();
|
$current_locale = determine_locale();
|
||||||
if ( $current_locale === $locale ) {
|
if ( $current_locale === $locale ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -718,7 +718,7 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
|
||||||
*/
|
*/
|
||||||
$output = __( 'html_lang_attribute' );
|
$output = __( 'html_lang_attribute' );
|
||||||
if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
|
if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
|
||||||
$output = is_admin() ? get_user_locale() : get_locale();
|
$output = determine_locale();
|
||||||
$output = str_replace( '_', '-', $output );
|
$output = str_replace( '_', '-', $output );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -104,6 +104,54 @@ function get_user_locale( $user_id = 0 ) {
|
||||||
return $locale ? $locale : get_locale();
|
return $locale ? $locale : get_locale();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the current locale desired for the request.
|
||||||
|
*
|
||||||
|
* @since 5.0.0
|
||||||
|
*
|
||||||
|
* @global string $pagenow
|
||||||
|
*
|
||||||
|
* @return string The determined locale.
|
||||||
|
*/
|
||||||
|
function determine_locale() {
|
||||||
|
/**
|
||||||
|
* Filters the locale for the current request prior to the default determination process.
|
||||||
|
*
|
||||||
|
* Using this filter allows to override the default logic, effectively short-circuiting the function.
|
||||||
|
*
|
||||||
|
* @since 5.0.0
|
||||||
|
*
|
||||||
|
* @param string|null The locale to return and short-circuit, or null as default.
|
||||||
|
*/
|
||||||
|
$determined_locale = apply_filters( 'pre_determine_locale', null );
|
||||||
|
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
|
||||||
|
return $determined_locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
$determined_locale = get_locale();
|
||||||
|
|
||||||
|
if ( is_admin() ) {
|
||||||
|
$determined_locale = get_user_locale();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() && is_user_logged_in() ) {
|
||||||
|
$determined_locale = get_user_locale();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
|
||||||
|
$determined_locale = sanitize_text_field( $_GET['wp_lang'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the locale for the current request.
|
||||||
|
*
|
||||||
|
* @since 5.0.0
|
||||||
|
*
|
||||||
|
* @param string $locale The locale.
|
||||||
|
*/
|
||||||
|
return apply_filters( 'determine_locale', $determined_locale );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the translation of $text.
|
* Retrieve the translation of $text.
|
||||||
*
|
*
|
||||||
|
@ -685,7 +733,7 @@ function unload_textdomain( $domain ) {
|
||||||
*/
|
*/
|
||||||
function load_default_textdomain( $locale = null ) {
|
function load_default_textdomain( $locale = null ) {
|
||||||
if ( null === $locale ) {
|
if ( null === $locale ) {
|
||||||
$locale = is_admin() ? get_user_locale() : get_locale();
|
$locale = determine_locale();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload previously loaded strings so we can switch translations.
|
// Unload previously loaded strings so we can switch translations.
|
||||||
|
@ -734,7 +782,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
|
||||||
* @param string $locale The plugin's current locale.
|
* @param string $locale The plugin's current locale.
|
||||||
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
|
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
|
||||||
*/
|
*/
|
||||||
$locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
|
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
|
||||||
|
|
||||||
$mofile = $domain . '-' . $locale . '.mo';
|
$mofile = $domain . '-' . $locale . '.mo';
|
||||||
|
|
||||||
|
@ -768,7 +816,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
|
||||||
*/
|
*/
|
||||||
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
|
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
|
||||||
/** This filter is documented in wp-includes/l10n.php */
|
/** This filter is documented in wp-includes/l10n.php */
|
||||||
$locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
|
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
|
||||||
|
|
||||||
$mofile = $domain . '-' . $locale . '.mo';
|
$mofile = $domain . '-' . $locale . '.mo';
|
||||||
|
|
||||||
|
@ -807,7 +855,7 @@ function load_theme_textdomain( $domain, $path = false ) {
|
||||||
* @param string $locale The theme's current locale.
|
* @param string $locale The theme's current locale.
|
||||||
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
|
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
|
||||||
*/
|
*/
|
||||||
$locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
|
$locale = apply_filters( 'theme_locale', determine_locale(), $domain );
|
||||||
|
|
||||||
$mofile = $domain . '-' . $locale . '.mo';
|
$mofile = $domain . '-' . $locale . '.mo';
|
||||||
|
|
||||||
|
@ -941,7 +989,7 @@ function _get_path_to_translation_from_lang_dir( $domain ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$locale = is_admin() ? get_user_locale() : get_locale();
|
$locale = determine_locale();
|
||||||
$mofile = "{$domain}-{$locale}.mo";
|
$mofile = "{$domain}-{$locale}.mo";
|
||||||
|
|
||||||
$path = WP_LANG_DIR . '/plugins/' . $mofile;
|
$path = WP_LANG_DIR . '/plugins/' . $mofile;
|
||||||
|
|
|
@ -954,7 +954,7 @@ function wp_default_scripts( &$scripts ) {
|
||||||
'var mejsL10n = %s;',
|
'var mejsL10n = %s;',
|
||||||
wp_json_encode(
|
wp_json_encode(
|
||||||
array(
|
array(
|
||||||
'language' => strtolower( strtok( is_admin() ? get_user_locale() : get_locale(), '_-' ) ),
|
'language' => strtolower( strtok( determine_locale(), '_-' ) ),
|
||||||
'strings' => array(
|
'strings' => array(
|
||||||
'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/' ),
|
'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/' ),
|
||||||
'mejs.fullscreen-off' => __( 'Turn off Fullscreen' ),
|
'mejs.fullscreen-off' => __( 'Turn off Fullscreen' ),
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.1-alpha-44133';
|
$wp_version = '5.1-alpha-44134';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
27
wp-login.php
27
wp-login.php
|
@ -464,9 +464,6 @@ if ( SITECOOKIEPATH != COOKIEPATH ) {
|
||||||
setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
|
setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
|
||||||
}
|
}
|
||||||
|
|
||||||
$lang = ! empty( $_GET['wp_lang'] ) ? sanitize_text_field( $_GET['wp_lang'] ) : '';
|
|
||||||
$switched_locale = switch_to_locale( $lang );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fires when the login form is initialized.
|
* Fires when the login form is initialized.
|
||||||
*
|
*
|
||||||
|
@ -527,10 +524,6 @@ switch ( $action ) {
|
||||||
}
|
}
|
||||||
setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
|
setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
wp_safe_redirect( wp_get_referer() );
|
wp_safe_redirect( wp_get_referer() );
|
||||||
exit();
|
exit();
|
||||||
|
|
||||||
|
@ -548,10 +541,6 @@ switch ( $action ) {
|
||||||
$requested_redirect_to = '';
|
$requested_redirect_to = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters the log out redirect URL.
|
* Filters the log out redirect URL.
|
||||||
*
|
*
|
||||||
|
@ -649,10 +638,6 @@ switch ( $action ) {
|
||||||
<?php
|
<?php
|
||||||
login_footer( 'user_login' );
|
login_footer( 'user_login' );
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'resetpass':
|
case 'resetpass':
|
||||||
|
@ -780,10 +765,6 @@ switch ( $action ) {
|
||||||
<?php
|
<?php
|
||||||
login_footer( 'user_pass' );
|
login_footer( 'user_pass' );
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'register':
|
case 'register':
|
||||||
|
@ -867,10 +848,6 @@ switch ( $action ) {
|
||||||
<?php
|
<?php
|
||||||
login_footer( 'user_login' );
|
login_footer( 'user_login' );
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'confirmaction':
|
case 'confirmaction':
|
||||||
|
@ -1179,9 +1156,5 @@ switch ( $action ) {
|
||||||
<?php
|
<?php
|
||||||
login_footer();
|
login_footer();
|
||||||
|
|
||||||
if ( $switched_locale ) {
|
|
||||||
restore_previous_locale();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} // End action switch.
|
} // End action switch.
|
||||||
|
|
Loading…
Reference in New Issue