From 0d109bda84fa9f6cf5fa60ccf8fc36a0700e81f7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 15 Jan 2024 18:55:59 +0000 Subject: [PATCH] Upgrade/Install: Fix JavaScript localization on install page. Blocks registration causes scripts to be initialized and localized very early, before the current locale has been properly set on the installation page. This changes `determine_locale()` so that the locale chosen during installation is recognized and loaded earlier, ensuring proper script localization. Props sabernhardt, NekoJonez, jornp, costdev. Fixes #58696 git-svn-id: https://develop.svn.wordpress.org/trunk@57286 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/install.php | 2 +- src/wp-includes/l10n.php | 9 +++++ tests/phpunit/tests/l10n/determineLocale.php | 37 +++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/install.php b/src/wp-admin/install.php index bf102893ff..34d5d4c7f7 100644 --- a/src/wp-admin/install.php +++ b/src/wp-admin/install.php @@ -329,7 +329,7 @@ if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { */ $language = ''; if ( ! empty( $_REQUEST['language'] ) ) { - $language = preg_replace( '/[^a-zA-Z0-9_]/', '', $_REQUEST['language'] ); + $language = sanitize_locale_name( $_REQUEST['language'] ); } elseif ( isset( $GLOBALS['wp_local_package'] ) ) { $language = $GLOBALS['wp_local_package']; } diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 67bfb1a26e..598a485468 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -150,6 +150,15 @@ function determine_locale() { ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ) { $determined_locale = get_user_locale(); + } elseif ( + ( ! empty( $_REQUEST['language'] ) || isset( $GLOBALS['wp_local_package'] ) ) + && wp_installing() + ) { + if ( ! empty( $_REQUEST['language'] ) ) { + $determined_locale = sanitize_locale_name( $_REQUEST['language'] ); + } else { + $determined_locale = $GLOBALS['wp_local_package']; + } } if ( ! $determined_locale ) { diff --git a/tests/phpunit/tests/l10n/determineLocale.php b/tests/phpunit/tests/l10n/determineLocale.php index 51ab1c28b9..3a675dc515 100644 --- a/tests/phpunit/tests/l10n/determineLocale.php +++ b/tests/phpunit/tests/l10n/determineLocale.php @@ -20,7 +20,15 @@ class Tests_L10n_DetermineLocale extends WP_UnitTestCase { } public function tear_down() { - unset( $_SERVER['CONTENT_TYPE'], $_GET['_locale'], $_COOKIE['wp_lang'], $GLOBALS['pagenow'] ); + unset( + $_SERVER['CONTENT_TYPE'], + $_GET['_locale'], + $_COOKIE['wp_lang'], + $GLOBALS['pagenow'], + $GLOBALS['wp_local_package'], + $_REQUEST['language'] + ); + wp_installing( false ); parent::tear_down(); } @@ -273,4 +281,31 @@ class Tests_L10n_DetermineLocale extends WP_UnitTestCase { $this->assertSame( 'siteLocale', determine_locale() ); } + + public function test_language_param_not_installing() { + $_REQUEST['language'] = 'de_DE'; + $this->assertSame( 'en_US', determine_locale() ); + } + + public function test_language_param_installing() { + $_REQUEST['language'] = 'de_DE'; + wp_installing( true ); + $this->assertSame( 'de_DE', determine_locale() ); + } + + public function test_language_param_installing_incorrect_string() { + $_REQUEST['language'] = '####'; // Something sanitize_locale_name() strips away. + wp_installing( true ); + $this->assertSame( 'en_US', determine_locale() ); + } + + public function test_wp_local_package_global_not_installing() { + $_REQUEST['language'] = 'de_DE'; + $this->assertSame( 'en_US', determine_locale() ); + } + public function test_wp_local_package_global_installing() { + $_REQUEST['language'] = 'de_DE'; + wp_installing( true ); + $this->assertSame( 'de_DE', determine_locale() ); + } }