mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-25 02:27:50 +01:00
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. Built from https://develop.svn.wordpress.org/branches/5.0@43776 git-svn-id: http://core.svn.wordpress.org/branches/5.0@43605 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
648850ea73
commit
d6895a1e65
@ -45,7 +45,7 @@ class WP_Locale_Switcher {
|
||||
* @since 4.7.0
|
||||
*/
|
||||
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() );
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ class WP_Locale_Switcher {
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
public function switch_to_locale( $locale ) {
|
||||
$current_locale = is_admin() ? get_user_locale() : get_locale();
|
||||
$current_locale = determine_locale();
|
||||
if ( $current_locale === $locale ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -703,7 +703,7 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
|
||||
*/
|
||||
$output = __( 'html_lang_attribute' );
|
||||
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 );
|
||||
}
|
||||
break;
|
||||
|
@ -104,6 +104,54 @@ function get_user_locale( $user_id = 0 ) {
|
||||
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.
|
||||
*
|
||||
@ -663,7 +711,7 @@ function unload_textdomain( $domain ) {
|
||||
*/
|
||||
function load_default_textdomain( $locale = null ) {
|
||||
if ( null === $locale ) {
|
||||
$locale = is_admin() ? get_user_locale() : get_locale();
|
||||
$locale = determine_locale();
|
||||
}
|
||||
|
||||
// Unload previously loaded strings so we can switch translations.
|
||||
@ -711,7 +759,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
|
||||
* @param string $locale The plugin's current locale.
|
||||
* @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';
|
||||
|
||||
@ -745,7 +793,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
|
||||
*/
|
||||
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
|
||||
/** 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';
|
||||
|
||||
@ -784,7 +832,7 @@ function load_theme_textdomain( $domain, $path = false ) {
|
||||
* @param string $locale The theme's current locale.
|
||||
* @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';
|
||||
|
||||
@ -915,7 +963,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";
|
||||
|
||||
$path = WP_LANG_DIR . '/plugins/' . $mofile;
|
||||
@ -1347,7 +1395,7 @@ function wp_get_jed_locale_data( $domain ) {
|
||||
$locale = array(
|
||||
'' => array(
|
||||
'domain' => $domain,
|
||||
'lang' => is_admin() ? get_user_locale() : get_locale(),
|
||||
'lang' => determine_locale(),
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -905,7 +905,7 @@ function wp_default_scripts( &$scripts ) {
|
||||
$scripts->add( 'mediaelement-migrate', "/wp-includes/js/mediaelement/mediaelement-migrate$suffix.js", array(), false, 1);
|
||||
|
||||
did_action( 'init' ) && $scripts->add_inline_script( 'mediaelement-core', sprintf( 'var mejsL10n = %s;', wp_json_encode( array(
|
||||
'language' => strtolower( strtok( is_admin() ? get_user_locale() : get_locale(), '_-' ) ),
|
||||
'language' => strtolower( strtok( determine_locale(), '_-' ) ),
|
||||
'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.fullscreen-off' => __( 'Turn off Fullscreen' ),
|
||||
|
@ -4,7 +4,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '5.0-alpha-43775';
|
||||
$wp_version = '5.0-alpha-43776';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
27
wp-login.php
27
wp-login.php
@ -437,9 +437,6 @@ setcookie( TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN, $secure
|
||||
if ( SITECOOKIEPATH != COOKIEPATH )
|
||||
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.
|
||||
*
|
||||
@ -500,10 +497,6 @@ case 'postpass' :
|
||||
}
|
||||
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() );
|
||||
exit();
|
||||
|
||||
@ -521,10 +514,6 @@ case 'logout' :
|
||||
$requested_redirect_to = '';
|
||||
}
|
||||
|
||||
if ( $switched_locale ) {
|
||||
restore_previous_locale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the log out redirect URL.
|
||||
*
|
||||
@ -618,10 +607,6 @@ endif;
|
||||
<?php
|
||||
login_footer('user_login');
|
||||
|
||||
if ( $switched_locale ) {
|
||||
restore_previous_locale();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'resetpass' :
|
||||
@ -747,10 +732,6 @@ endif;
|
||||
<?php
|
||||
login_footer('user_pass');
|
||||
|
||||
if ( $switched_locale ) {
|
||||
restore_previous_locale();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'register' :
|
||||
@ -834,10 +815,6 @@ case 'register' :
|
||||
<?php
|
||||
login_footer('user_login');
|
||||
|
||||
if ( $switched_locale ) {
|
||||
restore_previous_locale();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'confirmaction' :
|
||||
@ -1119,9 +1096,5 @@ try {
|
||||
<?php
|
||||
login_footer();
|
||||
|
||||
if ( $switched_locale ) {
|
||||
restore_previous_locale();
|
||||
}
|
||||
|
||||
break;
|
||||
} // end action switch
|
||||
|
Loading…
Reference in New Issue
Block a user