I18N: Refactor determine_locale() for performance and readability.

Refactors the function to avoid unnecessary `get_locale()` calls and slightly improve performance, while keeping it readable.

Adds tests.

Props Cybr, spacedmonkey, swissspidy.
Fixes #58317.
Built from https://develop.svn.wordpress.org/trunk@55862


git-svn-id: http://core.svn.wordpress.org/trunk@55374 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2023-05-26 17:57:23 +00:00
parent 4e30de26c6
commit afd88c4ac7
2 changed files with 17 additions and 21 deletions

View File

@ -132,30 +132,26 @@ function determine_locale() {
*/
$determined_locale = apply_filters( 'pre_determine_locale', null );
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
if ( $determined_locale && is_string( $determined_locale ) ) {
return $determined_locale;
}
$determined_locale = get_locale();
if ( is_admin() ) {
if (
isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] &&
( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) )
) {
if ( ! empty( $_GET['wp_lang'] ) ) {
$determined_locale = sanitize_locale_name( $_GET['wp_lang'] );
} else {
$determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
}
} else if (
( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ||
is_admin()
) {
$determined_locale = get_user_locale();
}
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
$determined_locale = get_user_locale();
}
$wp_lang = '';
if ( ! empty( $_GET['wp_lang'] ) ) {
$wp_lang = sanitize_locale_name( wp_unslash( $_GET['wp_lang'] ) );
} elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
$wp_lang = sanitize_locale_name( wp_unslash( $_COOKIE['wp_lang'] ) );
}
if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
$determined_locale = $wp_lang;
} else {
$determined_locale = get_locale();
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-55861';
$wp_version = '6.3-alpha-55862';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.