Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
296960a
Parse `Accept-Language` header in `wp_load_translations_early`
swissspidy May 24, 2024
862ea0d
Merge branch 'trunk' into fix/30049
swissspidy May 29, 2024
8da9e2e
Disallow arrays
swissspidy May 29, 2024
ee89cb8
Merge branch 'trunk' into fix/30049
swissspidy May 29, 2024
f64bb7b
Check priority and transient
swissspidy May 29, 2024
3736aab
Wrap in `empty`
swissspidy May 29, 2024
a16e55d
Ensure stable sort, always drop `en`
swissspidy May 29, 2024
d22e516
Add ky / kir test case
swissspidy May 29, 2024
8515209
Merge branch 'trunk' into fix/30049
swissspidy May 29, 2024
858e9cd
Adjust comment
swissspidy May 29, 2024
e0c08ef
Merge branch 'trunk' into fix/30049
swissspidy Jun 3, 2024
a816dc8
Merge branch 'trunk' into fix/30049
swissspidy Jun 10, 2024
fee8884
Change version
swissspidy Jun 17, 2024
601bfaa
Add more test cases
swissspidy Jun 18, 2024
6a33abb
Merge branch 'trunk' into fix/30049
swissspidy Jun 18, 2024
d9457e8
Merge branch 'trunk' into fix/30049
swissspidy Jun 18, 2024
ffe58bd
Add end-to-end test
swissspidy Jun 18, 2024
2605624
Test maintenance mode instead
swissspidy Jun 18, 2024
00bd7aa
Add hardening
swissspidy Jun 18, 2024
07c164c
Start object cache if needed
swissspidy Jun 18, 2024
3d42d79
Merge branch 'trunk' into fix/30049
swissspidy Jun 18, 2024
b233b05
Apply suggestions from code review
swissspidy Jun 18, 2024
4a9cddf
Merge branch 'trunk' into fix/30049
swissspidy Jun 18, 2024
81f6961
Merge branch 'trunk' into fix/30049
swissspidy Jun 25, 2024
3f34e28
Merge branch 'trunk' into fix/30049
swissspidy Aug 22, 2024
fff8dc1
Merge branch 'trunk' into fix/30049
swissspidy Sep 3, 2024
e52d38a
Merge branch 'trunk' into fix/30049
swissspidy Sep 13, 2025
39ebe00
Merge branch 'trunk' into fix/30049
swissspidy Jun 29, 2026
6d806bd
Apply suggestions from code review
swissspidy Jun 29, 2026
fa79c39
Merge branch 'trunk' into fix/30049
swissspidy Aug 21, 2026
c045bc0
Merge branch 'trunk' into fix/30049
swissspidy Oct 5, 2026
b726b74
Fix bugs in `Accept-Language` parsing and early translation loading
swissspidy Dec 3, 2026
4402670
Merge remote-tracking branch 'origin/trunk' into fix/30049
swissspidy Dec 3, 2026
ddd9d3f
Prefer the site's locale from the database when loading translations …
swissspidy Dec 3, 2026
2877e54
Remove site locale lookup from early translation loading
swissspidy Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -2239,3 +2239,216 @@ function wp_get_word_count_type() {
function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool {
return WP_Translation_Controller::get_instance()->has_translation( $singular, $textdomain, $locale );
}

/**
* Determines whether options can be safely retrieved during early bootstrap.
*
* Translations can be loaded very early, e.g. while handling database errors,
* before the object cache is started or before the database connection is ready.
*
* @since 7.3.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return bool Whether options can be retrieved.
*/
function wp_can_query_options_early() {
global $wpdb;

return $wpdb instanceof wpdb
&& $wpdb->ready
&& function_exists( 'get_option' )
// The options API relies on the object cache, which is started after the database connection.
&& function_exists( 'wp_cache_get' );
}

/**
* Parses the `Accept-Language` header to get a list of locales.
*
* The locales are returned in the format WordPress expects,
* so "fr-CH" becomes "fr_CH" and "fr" becomes "fr_FR".
*
* If the list of available translations is already cached,
* it is used to map language codes to actually existing locales.
* Otherwise, a best-effort approximation is returned.
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
* @link https://www.rfc-editor.org/rfc/rfc9110#field.accept-language
*
* @since 7.3.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return string[] Locales list, ordered by preference.
*/
function get_locales_from_accept_language_header() {
global $wpdb;

if ( empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) || ! is_string( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
return array();
}

/*
* Parse a header value such as "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5".
*
* Only a limited number of entries is considered, which is more than enough
* for any real browser and protects against excessively long headers.
*/
$entries = array_slice( explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ), 0, 20 );

$weighted_codes = array();

foreach ( $entries as $index => $entry ) {
$params = explode( ';', $entry );
$code = trim( array_shift( $params ) );

// Language range as per RFC 4647, e.g. "de", "de-CH", "ca-valencia", or the "*" wildcard.
if ( '*' !== $code && ! preg_match( '/^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$/', $code ) ) {
continue;
}

// An absent weight defaults to 1.
$weight = 1.0;

foreach ( $params as $param ) {
$param = trim( $param );

if ( ! preg_match( '/^q\s*=\s*(.*)$/i', $param, $param_matches ) ) {
continue;
}

if ( ! preg_match( '/^(?:1(?:\.0{0,3})?|0(?:\.\d{0,3})?)$/', $param_matches[1] ) ) {
// Ignore entries with an invalid weight.
continue 2;
}

$weight = (float) $param_matches[1];
}

// A weight of 0 means "not acceptable".
if ( $weight <= 0 ) {
continue;
}

$weighted_codes[] = array(
'code' => $code,
'weight' => $weight,
'index' => $index,
);
}

// Sort codes by weight, keeping the original order for equal weights.
usort(
$weighted_codes,
static function ( $a, $b ) {
$cmp = $b['weight'] <=> $a['weight'];
return 0 !== $cmp ? $cmp : ( $a['index'] <=> $b['index'] );
}
);

$translations = array();

/*
* Get list of available translations without potentially deleting an expired transient and causing an HTTP request.
* Only works if either the object cache or the database are already available.
* This function can run very early, e.g. while handling database errors,
* so make sure not to trigger any new errors along the way.
*/
if ( wp_using_ext_object_cache() ) {
if ( function_exists( 'wp_cache_get' ) ) {
$translations = wp_cache_get( 'available_translations', 'site-transient' );
}
} elseif ( wp_can_query_options_early() ) {
$suppress = $wpdb->suppress_errors();
$translations = get_site_option( '_site_transient_available_translations' );
$wpdb->suppress_errors( $suppress );
}

$has_available_translations = is_array( $translations ) && ! empty( $translations );

$locales = array();

foreach ( $weighted_codes as $weighted_code ) {
$code = $weighted_code['code'];

if ( '*' === $code ) {
// Ignore anything after the wildcard, as we can then just default to en_US.
break;
}

// Language tags are case-insensitive, normalize them to WordPress' format, e.g. "de-ch" -> "de_CH".
$subtags = explode( '-', strtolower( $code ) );
$language = array_shift( $subtags );
$parts = array( $language );

foreach ( $subtags as $subtag ) {
if ( 4 === strlen( $subtag ) && ctype_alpha( $subtag ) ) {
// Script subtags such as "Latn" or "Hant" are not part of WordPress locales.
continue;
}

if ( 2 === strlen( $subtag ) && ctype_alpha( $subtag ) ) {
// Region subtags are uppercase, e.g. "CH".
$subtag = strtoupper( $subtag );
}

$parts[] = $subtag;
}

$locale = implode( '_', $parts );

// If English is accepted, then there is no point in adding any other locales after it.
if ( 'en' === $locale || 'en_US' === $locale ) {
break;
}

if ( $has_available_translations ) {
$found = array();

foreach ( $translations as $translation_locale => $translation ) {
$translation_locale = (string) $translation_locale;

if ( $locale === $translation_locale ) {
$found[] = $translation_locale;
} elseif (
// Language-only codes such as "de" match all locales for that language.
$locale === $language &&
isset( $translation['iso'] ) &&
is_array( $translation['iso'] ) &&
in_array( $language, $translation['iso'], true )
) {
$found[] = $translation_locale;
}
}

/*
* Prefer exact matches, then the "main" locale for a language (e.g. "fr_FR" for "fr"),
* then everything else in alphabetical order.
*/
$main_locale = $language . '_' . strtoupper( $language );

usort(
$found,
static function ( $a, $b ) use ( $locale, $main_locale ) {
$rank_a = $locale === $a ? 0 : ( $main_locale === $a ? 1 : 2 );
$rank_b = $locale === $b ? 0 : ( $main_locale === $b ? 1 : 2 );

return 0 !== $rank_a - $rank_b ? $rank_a - $rank_b : strcmp( $a, $b );
}
);

array_push( $locales, ...$found );
} else {
$locales[] = $locale;

// Fallback approximation, supporting cases like "el", but also "fr" -> "fr_FR".
if ( 2 === strlen( $locale ) ) {
$locales[] = $locale . '_' . strtoupper( $locale );
}
}
}

return array_values( array_unique( $locales ) );
}
3 changes: 3 additions & 0 deletions src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,9 @@ function wp_load_translations_early() {
$locales[] = $wp_local_package;
}

// Try the browser's locale.
$locales = array_merge( $locales, get_locales_from_accept_language_header() );

if ( ! $locales ) {
break;
}
Expand Down
25 changes: 20 additions & 5 deletions tests/e2e/specs/maintenance-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@ test.describe( 'Maintenance mode', () => {
}, 'should leave maintenance mode after the lock file is deleted' ).toPass( { timeout: 10_000 } );
} );

test( 'should display maintenance mode page', async ( { page } ) => {
await page.goto( '/' );
await expect(
page.getByText( /Briefly unavailable for scheduled maintenance\. Check back in a minute\./ )
).toBeVisible();
test.describe( 'Default (en_US)', () => {
test( 'should display maintenance mode page', async ( { page } ) => {
await page.goto( '/' );
await expect(
page.getByText( /Briefly unavailable for scheduled maintenance\. Check back in a minute\./ )
).toBeVisible();
} );
} );

test.describe( 'Localized (de_DE)', () => {
test.use( { locale: 'de-DE' } ); // Sets the Accept-Language header.

test( 'should display maintenance mode page', async ( { page } ) => {
await page.goto( '/' );
// Only match a part of the string to be resilient against translation changes.
await expect( page.getByText( /Wartungsarbeiten/ ) ).toBeVisible();
await expect(
page.getByText( /Briefly unavailable for scheduled maintenance/ )
).toBeHidden();
} );
} );
} );
Loading
Loading