Repository navigation
Parse Accept-Language header in wp_load_translations_early - #6623
swissspidy wants to merge 35 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
Adds early-l10n fallback behavior by parsing the browser’s Accept-Language header when WordPress can’t yet determine the locale from the database, so the maintenance screen (and other early output) can be localized sooner.
Changes:
- Add
get_locales_from_accept_language_header()helper to derive candidate locales fromAccept-Language. - Use that helper in
wp_load_translations_early()to expand the locale candidates list. - Add PHPUnit coverage for parsing behavior and expand the maintenance-mode E2E spec to assert a localized variant.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/wp-includes/load.php |
Adds formatting helpers and merges locales derived from Accept-Language into early-translation locale candidates. |
src/wp-includes/l10n.php |
Introduces get_locales_from_accept_language_header() to parse/sort language tags and map them to WordPress locales. |
tests/phpunit/tests/l10n.php |
Adds data-driven tests for get_locales_from_accept_language_header(). |
tests/e2e/specs/maintenance-mode.test.js |
Adds a localized maintenance-mode assertion using a German locale. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
- Parse the header per entry instead of with a single global regex, so that malformed weights or other parameters no longer leak in as bogus language codes (e.g. "fr;q=2" previously yielded "fr" and "q"). - Honor `q=0`, which means "not acceptable", and drop entries with invalid weights. - Normalize case (language tags are case-insensitive) and strip script subtags, so "de-ch" maps to "de_CH" and "zh-Hant-TW" to "zh_TW". - Treat "en-US" like "en" and stop processing further locales. - Prefer exact matches and the main locale of a language (e.g. "de_DE" for "de") over alphabetical order (which picked "de_AT" first). - Remove duplicate locales from the result. - Validate language ranges strictly, which makes `sanitize_locale_name()` and loading `formatting.php` early unnecessary. Loading it early could cause a fatal "cannot redeclare" error later in `wp-settings.php`. - Do not (re-)start the object cache from within this function, and only query the database if it is ready and the options API is loaded, with errors suppressed. The function runs while handling database errors. - Update `@since` version. - Use a fixed fixture instead of the live translations API in tests, and add test cases for the fixed edge cases. - Fix the German maintenance mode string asserted in the e2e test, which no longer matched the current translation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oQwoq4Z3pcJMrikcf
…early The `Accept-Language` header should only be used as a fallback when the site's locale cannot be retrieved from the database, e.g. in maintenance mode or when the database connection fails. Adds `wp_get_locale_from_database_early()`, which mirrors the logic of `get_locale()` without caching the result, applying filters, or causing database errors. Also adds `wp_can_query_options_early()`. Checking for `get_option()` alone is not sufficient, as `option.php` is loaded together with `functions.php` before the object cache is started. Calling the options API at that point, e.g. when the table prefix is invalid, would cause a fatal error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oQwoq4Z3pcJMrikcf
`wp_load_translations_early()` is meant for edge cases where the database is typically not available, so there is no need to look up the site's locale there. Keeps the `wp_can_query_options_early()` guard, which is still needed for safely retrieving the cached list of available translations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oQwoq4Z3pcJMrikcf
Parse
Accept-Languageheader when loading translations early where the current locale can't be fetched from the database, e.g. in maintenance mode, on database connection errors, or duringsetup-config.php.get_locales_from_accept_language_header()function that returns a list of WordPress locales from the header, ordered by preference.q=0("not acceptable") and entries with invalid weights are dropped.de-ch→de_CH,zh-Hant-TW→zh_TW.en/en-USor the*wildcard, since English is the default anyway.de→de_DE, thende_AT, …). Otherwise falls back to an approximation (de→de,de_DE).wp_load_translations_early()tries the header locales afterWPLANGand$wp_local_package.Trac ticket: https://core.trac.wordpress.org/ticket/30049
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.