Skip to content

I18N: Switching language should update date and time formats - #16243

Open
faisalahammad wants to merge 93 commits into
WordPress:trunkfrom
faisalahammad:fix/36259-locale-date-time-formats
Open

faisalahammad wants to merge 93 commits into
WordPress:trunkfrom
faisalahammad:fix/36259-locale-date-time-formats

Conversation

@faisalahammad

Copy link
Copy Markdown

When the site language is changed in Settings > General, the date and time formats now switch to the new language's defaults. A format is only changed when it still matches the old language's default, so custom formats are kept.

Trac ticket: https://core.trac.wordpress.org/ticket/36259

What changed

  • New function wp_update_date_time_formats_on_locale_change( $old_locale, $new_locale ) in wp-admin/includes/options.php. It loads the default translations of both site locales, compares the saved date_format and time_format options with the old locale's defaults, and updates the ones that match. Then it restores the translations for the current user.
  • wp-admin/options.php stores get_locale() before saving and calls the new function after the General settings are saved.
  • load_default_textdomain() is used instead of switch_to_locale(), because switch_to_locale() does not know about a language pack downloaded during the same request.

Compared to the earlier patches on the ticket:

  • It compares the site locales, not the user locale, so it works when the admin's profile language differs from the site language.
  • It compares the saved option values, not the slashed $_POST values, so formats with backslashes like G\hi (French) or j \d\e F \d\e Y (Spanish) are matched.
  • The logic is in its own function, so it has unit tests.

Scope

  • In scope: the Settings > General form.
  • Not in scope: changing WPLANG through the REST API (/wp/v2/settings) or the abilities API. These can be handled in a follow-up.
  • links_updated_date_format is not changed.

Testing

Unit tests:

npm run test:php -- --filter WpUpdateDateTimeFormatsOnLocaleChange

Manual test:

  1. Go to Settings > General with the default formats (F j, Y / g:i a).
  2. Change Site Language to Deutsch and save.
  3. The Date Format changes to j. F Y and the Time Format to G:i.
  4. Set a custom Date Format like Y-m-d, switch to another language and save. The custom date format stays, and only the time format changes.
  5. Switch from Français (j F Y / G\hi) back to English. The formats change to F j, Y / g:i a.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigating the ticket, drafting the fix and unit tests. I reviewed, tested and edited the final code.


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.

- Add wp_update_date_time_formats_on_locale_change() to wp-admin/includes/options.php.
- Call it from wp-admin/options.php after the General settings are saved.
- Add date and time format strings to the de_DE and es_ES test translations.
- Add unit tests.

Why: the date and time formats stayed on the old language defaults after switching the site language. A format is only updated when it still matches the previous locale default, so custom formats are kept.

See #36259.
@github-actions

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 faisalahammad.

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

  • 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.

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.

🟢 Approval recommended

No active review issues block the locale-aware date and time format update.

No open issues 🎉

What changed in this PR

Updates WordPress General settings so changing the site language also updates date and time formats that match the previous language’s defaults, while preserving custom formats.

Changes:

  • Adds a helper to compare locale defaults, update matching formats, and restore the current user’s translations.
  • Calls the helper after General settings are saved.
  • Adds unit tests and translation fixtures for locale changes, custom formats, and differing user languages.
File Description
tests/​phpunit/​tests/​admin/​includes/​options/​wpUpdateDateTimeFormatsOnLocaleChange.php Tests format updates, custom-format preservation, and translation restoration.
tests/​phpunit/​data/​languages/​es_ES.po Adds Spanish default date and time translations.
tests/​phpunit/​data/​languages/​es_ES.l10n.php Adds matching Spanish runtime translation fixtures.
tests/​phpunit/​data/​languages/​de_DE.po Adds German default date and time translations.
tests/​phpunit/​data/​languages/​de_DE.l10n.php Adds matching German runtime translation fixtures.
src/​wp-admin/​options.php Captures the previous site locale and invokes format updates after saving.
src/​wp-admin/​includes/​options.php Implements locale-aware date and time format updates.

Review effort: Balanced

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.

2 participants