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
You can use http or https.
https://ip2c.org/XXX.XXX.XXX.XXX or https://ip2c.org/?ip=XXX.XXX.XXX.XXX |
https://ip2c.org/s or https://ip2c.org/self or https://ip2c.org/?self |
Reference: https://about.ip2c.org/#inputs
Combining the multiple ways browsers are using to store the user's language, you get this function :
const getNavigatorLanguage = () => {
if (navigator.languages && navigator.languages.length) {
return navigator.languages[0];
} else {
return navigator.userLanguage || navigator.language || navigator.browserLanguage || 'en';
}
}
We first check the navigator.languages
array for its first element.
Then we get either navigator.userLanguage
or navigator.language
.
If this fails we get navigator.browserLanguage
.
Finally, we set it to 'en'
if everything else failed.
And here's the sexy one-liner :
const getNavigatorLanguage = () => (navigator.languages && navigator.languages.length) ? navigator.languages[0] : navigator.userLanguage || navigator.language || navigator.browserLanguage || 'en';
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 <input type="date">
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.
This article suggests the following properties of the browser's navigator object:
navigator.language
(Netscape - Browser Localization)navigator.browserLanguag
e (IE-Specific - Browser Localized Language)navigator.systemLanguage
(IE-Specific - Windows OS - Localized Language)navigator.userLanguage
Roll these into a javascript function and you should be able to guess the right language, in most circumstances. Be sure to degrade gracefully, so have a div containing your language choice links, so that if there is no javascript or the method doesn't work, the user can still decide. If it does work, just hide the div.
The only problem with doing this on the client side is that either you serve up all the languages to the client, or you have to wait until the script has run and detected the language before requesting the right version. Perhaps serving up the most popular language version as a default would irritate the fewest people.
Edit: I'd second Ivan's cookie suggestion, but make sure the user can always change the language later; not everyone prefers the language their browser defaults to.