I have a website (Flash) localized into a dozen of languages and I want to auto-define a default value depending on the user\'s browser settings in order to minimize the ste
There's a difference between the user's preferred languages and the system/browser locale.
A user can configure preferred languages in the browser, and these will be used for navigator.language(s)
, and used when requesting resources from a server, to request content according to a list of language priorities.
However, the browser locale will decide how to render number, date, time and currency. This setting is likely the highest ranking language, but there is no guarantee. On Mac and Linux, the locale is decided by the system regardless of the user language preferences. On Windows is can be elected among the languages in the preferred list on Chrome.
By using Intl (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl), developers can override/set the locale to use to render these things, but there are elements that cannot be overridden, such as the format.
To properly extract this language, the only way I've found is:
(new Intl.NumberFormat()).resolvedOptions().locale
(Intl.NumberFormat().resolvedOptions().locale
also seems to work)
This will create a new NumberFormat instance for the default locale and then reading back the locale of those resolved options.