react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Uncaught RangeError: invalid language tag: "C"

Open jzarca01 opened this issue 5 years ago โ€ข 9 comments

๐Ÿ› 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

jzarca01 avatar Oct 08 '21 09:10 jzarca01

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.

snowystinger avatar Oct 08 '21 16:10 snowystinger

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

jzarca01 avatar Oct 12 '21 08:10 jzarca01

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.

snowystinger avatar Oct 20 '21 17:10 snowystinger

You could still fallback to en or en-US like 99% of the internet

jzarca01 avatar Oct 26 '21 15:10 jzarca01

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

BartoszGamza avatar Apr 11 '22 09:04 BartoszGamza

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 avatar Apr 11 '22 17:04 snowystinger

@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:

image

I can create a PR with try-catch approach later that should cover all the weird scenarios ๐Ÿ˜‰

BartoszGamza avatar Apr 12 '22 08:04 BartoszGamza

@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?

BartoszGamza avatar Apr 12 '22 09:04 BartoszGamza

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

snowystinger avatar Apr 12 '22 16:04 snowystinger