Add numberFormatI18n Port to @wordpress/i18n#71214
Add numberFormatI18n Port to @wordpress/i18n#71214USERSATOSHI wants to merge 5 commits intoWordPress:trunkfrom
@wordpress/i18n#71214Conversation
@wordpress/i18n@wordpress/i18n
| const wpLocale = localeData.lang || 'en_US'; | ||
| let jsLocale; | ||
| if ( ! I18N_WP_LOCALE_REGEXP.test( wpLocale ) ) { | ||
| // If the locale is not in the expected format, default to 'en-US'. | ||
| jsLocale = 'en-US'; | ||
| } else { | ||
| jsLocale = wpLocale.replace( '_', '-' ); | ||
| } |
There was a problem hiding this comment.
Let's make sure this works for all the locales listed at https://make.wordpress.org/polyglots/teams/
I just tested them with Intl.getCanonicalLocales() and here are my findings:
de-DE-formalare actually supported (I thought it wasn't), so we should make it work too.art-xemojiis supported toopt-PT-ao90doesn't work, andIntl.getCanonicalLocales()throws
So a regex is too overkill, maybe simply replacing the underscores is enough. And then perhaps try { Intl.getCanonicalLocales() } catch ... to prevent errors.
We should have tests for such "special" locales too.
There was a problem hiding this comment.
Oh, thanks for informing, updating to reflect this.
There was a problem hiding this comment.
pt-PT-ao90 doesn't work, and Intl.getCanonicalLocales() throws
Seems likes this is not yet supported in both browsers and Node.js Intl so either we have to make it fallback to en-US ( current behaviour ) or write an if statement to make it fallback to pt-PT.
Other than that, I have changed to use getCanonicalLocales() to test locale and I have updated the tests to include both special locales and all the wp Locales mentioned in make.wordpress.org/polyglots/teams
cc: @swissspidy
- added tests for special locales and all polygot locales - switched form regex to a getCanonicalLocale() test to check locale validity
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@wordpress/i18n@wordpress/i18n
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
| /** | ||
| * Range limit of `maximumFractionDigits` in Node.js is 0-20 | ||
| * Max limit of `maximumFractionDigits` in Browsers is 0-100 ( {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#maximumfractiondigits} ) | ||
| * | ||
| * Hence we limit the decimals to 20 to ensure compatibility across environments. | ||
| */ | ||
| const validDecimals = Math.max( 0, Math.min( decimals, 20 ) ); |
There was a problem hiding this comment.
What's the motivation for this? Is it really our responsibility to clamp this value? Seems more like the consumer's job.
There was a problem hiding this comment.
The reason behind this was that during testing I found out that while Browser supported upto 100 decimals , Nodejs was limited to around 20 decimal points and anything above that it would throw an error saying invalid range.
Hence I thought of adding clamp to this to prevent this error.
What?
Closes #22628
This PR adds
numberFormatI18nfunction to the@wordpress/i18npackage that formats numbers according to the current locale settings, providing a JavaScript equivalent to WordPress's PHPnumber_format_i18nfunctionWhy?
WordPress translation functions have had
number_format_i18nfor formatting numbers in a locale-appropriate way for some time. This functionality was missing from the JavaScript@wordpress/i18npackage, making it difficult for developers to format numbers consistently with the site's locale in JavaScript/React components.How?
The implementation adds a
numberFormatI18nfunction that:langproperty from existing locale data rather than adding dependencies on other packagesde_DE) to JavaScript BCP 47 format (e.g.,de-DE)en-USfor invalid or unrecognized localesSyntax
Testing Instructions
Unit Tests for this function in
create-i18ntest file should suffice.Screenshots or screencast