Uncaught RangeError: invalid language tag: "C"
๐ Bug Report
Uncaught RangeError: invalid language tag: "C"
๐ค Expected Behavior
It should use a default language if the one provided isn't valid
๐ฏ Current Behavior
function isRTL crashes when trying to instantiate new Intl.Locale with bogus language "C"
๐ Possible Solution
It should check if locale provided is valid
๐ฆ Context
Open Firefox and browse to about:config
Change intl.accept_languages to C
Browse to https://shop.gandi.net and look in the console
Uncaught RangeError: invalid language tag: "C"
ht utils.ts:26
gt useDefaultLocale.ts:33
<anonymous> useDefaultLocale.ts:37
Webpack 15
utils.ts:26:17
๐ป Code Sample
๐ Your Environment
| Software | Version(s) |
|---|---|
| react-spectrum | @react-aria/i18n@^3.3.2 |
| Browser | Firefox 92.0.1 |
| Operating System | Arch Linux Manjaro, Gnome 40.4.0 |
๐งข Your Company/Team
Gandi
Hey! Thanks for the issue. I have to say, this seems like FF shouldn't accept invalid languages? What is the actual use case here? I'm asking because deciding on what 'default language' to use is non-obvious.
Yes, you are right Firefox should'nt accept invalid languages
However, you could check that they are supported https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf
If we checked for 'supported', what would we do in unsupported cases? Given that it's not apparent what fallback we should use, we think it makes more sense for applications to handle this logic as they'll have a better idea of the usage.
You could still fallback to en or en-US like 99% of the internet
I've also experienced this problem. I have logs for navigator.language returning en-US@posix and it is also not accepted by supportedLocalesOf so best option would be to actually try-catch on Intl.Locale(locale) and fallback to en-US in case of error
Thanks for the extra info @BartoszGamza, this clarifies a lot that I was unaware of. Do you know what the steps are to get the navigator.language to return that string?
@snowystinger Im not sure whether we can do anything here, this is just risky property. We got very weird outputs in the logs, such as:

I can create a PR with try-catch approach later that should cover all the weird scenarios ๐
@snowystinger in general, we cannot write to navigator.language, I wasn't able to reproduce the error on my own. Maybe @jzarca01 will be able to confirm whether it works?
If we can figure out how to get some of these other values, it might be worth seeing if there is a browser bug here. For instance, I'm not seeing any part of the syntax allowing for the "@" character in the specification https://datatracker.ietf.org/doc/html/rfc5646 which I found through MDN https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language