In order to determine the current locale I found different approaches:
Accept-Language
)navigator.language
process.env
On the other side, the ECMAScript Internationalization API defines the locales
argument to each of the Intl
constructors as optional:
If the
locales
argument is not provided or is undefined, the runtime's default locale is used.
So, it seems as if there should be a browser-independent and OS independent way to get "the runtime's default locale".
Is there a more straight forward way to get the runtime's default locale than
new Intl.NumberFormat().resolvedOptions().locale
?
The question How/Where does JavaScript detect the default locale? is different as it asks for the implementation of detecting the default locale (in a browser host). In contrast to that, my question is not about implementation but about the existence of a standard API.
I'm not aware of a more straight-forward approach, but as you have already pointed out,
new Intl.NumberFormat().resolvedOptions().locale
is a solution
DefaultLocale
is an abstract operation, which might or might not be implemented in the future.
Hence, I would argue for polyfilling this function
as:
if (typeof DefaultLocale === "undefined") {
function DefaultLocale() {
return new Intl.NumberFormat().resolvedOptions().locale;
}
}
So, your code will reasonably assume the existence and call this function
. In some point in the future, when this function
is implemented in a standard manner, you will be able to clean up by removing the chunk from above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With