Skip to content

Parse Accept-Language header in wp_load_translations_early - #6623

Open
swissspidy wants to merge 35 commits into
WordPress:trunkfrom
swissspidy:fix/30049
Open

swissspidy wants to merge 35 commits into
WordPress:trunkfrom
swissspidy:fix/30049

Conversation

@swissspidy

@swissspidy swissspidy commented May 24, 2024 •

Copy link
Copy Markdown
Member

Parse Accept-Language header 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 during setup-config.php.

  • Adds a private get_locales_from_accept_language_header() function that returns a list of WordPress locales from the header, ordered by preference.
    • Entries are sorted by weight (stable). q=0 ("not acceptable") and entries with invalid weights are dropped.
    • Language tags are validated strictly and normalized, e.g. de-ch → de_CH, zh-Hant-TW → zh_TW.
    • Stops at en/en-US or the * wildcard, since English is the default anyway.
    • If the list of available translations is already cached (external object cache or site transient), it is used to map codes to existing locales, preferring exact matches and the main locale of a language (de → de_DE, then de_AT, …). Otherwise falls back to an approximation (de → de, de_DE).
    • The cache lookup is guarded so it never starts the object cache or causes database errors, since this can run while handling database errors.
  • wp_load_translations_early() tries the header locales after WPLANG and $wp_local_package.
  • Adds PHPUnit tests (with a fixed translations fixture, no HTTP requests) and an e2e test for a localized maintenance mode page.

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.

@github-actions

github-actions Bot commented May 24, 2024 •

Copy link
Copy Markdown

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:

Props swissspidy, ocean90, sabernhardt.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
@swissspidy
swissspidy requested a review from ocean90 May 29, 2024 10:55
@swissspidy
swissspidy requested a review from johnbillion May 29, 2024 15:43
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/load.php Resolved
@swissspidy
swissspidy requested a review from ocean90 June 18, 2024 18:04
Copilot AI review requested due to automatic review settings June 29, 2026 09:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from Accept-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.

Comment thread src/wp-includes/l10n.php Resolved
Comment thread src/wp-includes/l10n.php Resolved
Comment thread tests/phpunit/tests/l10n.php Resolved
Comment thread src/wp-includes/load.php Resolved
Comment thread tests/phpunit/tests/l10n.php Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
swissspidy and others added 6 commits August 21, 2026 12:28
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants