-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intl.NumberFormat with style 'unit' doesn't work on Apple devices with pre-2021 OS #4630
Comments
Perhaps we need to look over the Babel config as it should have transpiled these (in theory at least). |
This explains it:
Intl is missing from core-js and that is what Babel is using to pollyfill our code at the moment. |
I imagine the polyfill could be quite large if it contains names and short names and formats for all these units for our 27 languages? Worth checking though. |
I checked it out a little quick yesterday and if we only need Intl.NumberFormat it's about 15kb, not optimal but we can live with that. But if we need to add the other Intl polyfills as well we are talking hundreds of kb. But hopefully we just need Intl.NumberFormat as thats one of the newer functions to get added to the Intl object though. |
Thanks for gathering all the info here! 🙏 |
Then I think the easiest way to implement this is simply using an if statment like this: if (typeof Intl.NumberFormat != 'undefined'){
// Intl Code
}
else {
// Fallback code
} |
Going by the error message, it looks like Intl.NumberFormat is supported, but not the |
True, perhaps a try catch would be more appropriate since it's a constructor option that's missing. Unless there is a specific way to check if a constructor option is supported? |
We use
Intl.NumberFormat(lang, { style: 'unit' })
to format hour, day, month, and year with localized names/numbers:electricitymaps-contrib/web/src/helpers/formatting.js
Lines 71 to 76 in 0cad313
Per https://caniuse.com/mdn-javascript_builtins_intl_numberformat_numberformat_options_unit_parameter , this is not supported by browsers used by about 7-8% of users.
In particular it is not supported by Safari on iOS before version 14.5 (making up about 1.5% of global users), nor on Safari on Mac OS before version 14.1. This seems to have been released in April 2021. Firefox gained support mid-2020, in version 78. Other major browsers supported this before 2020.
I would have hoped that the formatter would just leave anything it doesn't recognize unformatted, but it appears to throw errors instead. Here are a few reports:
style must be either "decimal", "percent", or "currency"
#4570 on Safari 12.1.2 on macos Sierra 10.12.6; reported as working on Safari 15.5 on macos 12.4 which checks out with Safari gaining support in version 14.5Is it feasible to wrap the formatter in a try-catch and fallback to ISO format or other unlocalized format?
The text was updated successfully, but these errors were encountered: