Repository navigation
number-formatters: Resolve the WordPress locale to its nearest supported tag - #55479
Conversation
…ted tag The fallback locale kept only the language of a WordPress locale name, so `zh_TW` formatted as `zh`, Simplified Chinese, and regional conventions were lost everywhere. Subtags now drop from the right until Intl supports the rest, so `zh_TW` becomes `zh-TW` and `pt_PT_ao90` becomes `pt-PT`. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Backup plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Boost plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Search plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Social plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Protect plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Videopress plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Premium Analytics plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Stats Data plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
…s whose screens change Every number formats through the resolution, and a WordPress variant such as `pt_PT_ao90` threw once per call before settling, so the result is kept by name. Backup, Boost, Search and Social format figures of their own with the package, so they get the changelog entry too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A lookup that fails while Intl is unavailable, as a test stubs it, must not stick to the name; a miss is cheap to repeat. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Code Coverage SummaryCoverage changed in 1 file.
|
There was a problem hiding this comment.
🟡 Changes recommended
Locale resolution repeats avoidable validation work on every formatted value, and forceLatin creates invalid tags for locales with existing Unicode extensions, causing POSIX formatting to fall back to English.
2 open issues
What changed in this PR
This PR updates the shared number-formatters package to preserve WordPress locale regions and variants where supported, improving Traditional Chinese and regional number formatting.
Changes:
- Resolve locale names to supported tags, falling back through configured, WordPress, browser, and default locales.
- Add tests for regional formatting, WordPress variants,
setLocale(), and unsupported locales. - Add package and Stats-related plugin changelog entries.
| File | Description |
|---|---|
projects/plugins/stats/changelog/fix-number-formatters-wp-locale-tag |
Document the Stats formatting fix. |
projects/plugins/premium-analytics/changelog/fix-number-formatters-wp-locale-tag |
Document the Premium Analytics formatting fix. |
projects/plugins/jetpack/changelog/fix-number-formatters-wp-locale-tag |
Document improvements for Stats and My Jetpack. |
projects/js-packages/number-formatters/src/test/create-number-formatters-locale.test.ts |
Cover locale resolution and fallback behavior. |
projects/js-packages/number-formatters/src/create-number-formatters.ts |
Normalize locale tags and resolve supported candidates. |
projects/js-packages/number-formatters/changelog/fix-number-formatters-wp-locale-tag |
Record the shared package’s formatting improvements. |
Review effort: Balanced
…al form Canonicalizing turns `en-US-posix` into `en-US-u-va-posix`, which the `-u-nu-latn` suffix for `forceLatin` cannot follow, so such a locale fell back to English. `supportedLocalesOf()` validates the tag on its own. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
chihsuan
left a comment
There was a problem hiding this comment.
Nice catch on the 万/萬 mismatch! @dognose24 LGTM. Before this merges, could we add changelog entries for plugins/protect and plugins/videopress? Protect's firewall stat cards and the VideoPress dashboard's views chart both format with this package on their own screens.
Protect's firewall stat cards format with StatCard and the VideoPress views chart with @automattic/charts, both of which use this package. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Added the two entries in 5aad2de. Both reach the package the way you said: Protect through |
chihsuan
left a comment
There was a problem hiding this comment.
Thanks for the updates! @dognose24 LGTM 🚀
| 1234567, | ||
| '1\u00a0234\u00a0567', | ||
| ], | ||
| [ 'de_DE_formal', 'keeps a variant Intl accepts', 1234567, '1.234.567' ], |
There was a problem hiding this comment.
nit: Would it be worth dropping the de_DE_formal row? de and de-DE-formal both format as 1.234.567, so it still passes with the old .split( '_' )[ 0 ].
There was a problem hiding this comment.
Good catch, that row proved nothing. Swapped it for de_CH_informal in 503cedd6a rather than dropping it, so a WordPress name with a variant Intl accepts is still covered. Plain de gives 1.234.567 and de-CH-informal gives 1'234'567, and the row fails on the old lookup.
de_DE_formal formats like plain de, so its row passed on the old language-only lookup too. de_CH_informal groups with an apostrophe, which only the region gives. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Proposed changes
getBrowserSafeLocale()kept only the language of a WordPress locale name (zh_TW→zh), so every number went through Simplified Chinese on a Traditional Chinese site, and regional conventions were lost everywhere else. It now turns the name into a BCP 47 tag and drops subtags from the right untilIntl.NumberFormatsupports the rest:zh_TW→zh-TW,pt_PT_ao90→pt-PT,de_DE_formal→de-DE-formal. A name nothing in Intl supports (ary) falls through to the next source, the browser's language, and then toen, as before.setLocale()values go through the same resolution, sosetLocale( 'zh_TW' )works too.pt_PT_ao90threw once per call before settling, so the result is kept by name.What a visitor sees, formatting 1,620,000 in compact notation and 1,234,567 in full:
zh_TWzh_HKen_GBes_MXpt_PT_ao90pt, Brazilian)The dashboard's dates already resolve the locale this way in
@jetpack-premium-analytics/formatters, which is how the mismatch showed: Stats v2 on azh_TWsite wrote dates in Traditional Chinese and every figure in Simplified.Changelog entries go to the plugins whose own screens format figures with the package:
plugins/jetpack(Stats, My Jetpack, Forms, Newsletter, Monetize),plugins/stats,plugins/premium-analytics,plugins/backup(storage sizes and prices),plugins/boost(performance history),plugins/search(record counts and prices) andplugins/social(upgrade prices). The remaining bundling plugins reach it only through My Jetpack cards.Related product discussion/links
zh_TWsite.Does this pull request change what data or activity we track or use?
No.
Testing instructions
jp test js js-packages/number-formatters: the locale suite has the new cases.162萬, not162万.1.6m, as the browser would format them foren-GB.What I ran:
tsc --noEmitand ESLint on the changed files.zh_TWcase in Chrome on a local Jetpack site with Stats v2.🤖 Generated with Claude Code