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 steps to access the content.
FYI, I cannot use server-scripts due to proxy restrictions, so I guess JavaScript or ActionScript would be appropriate to solve the problem.
Questions:
What would be the best method to 'guess' the user's locale?
Are there any existing simple classes/functions that could help me out (no complex localization bundles)? Specially to break down all the possible languages into a smaller number (the translations I have) on a smart way.
To which point can I trust such a solution?
Any other workarounds or suggestions?
This question is related to
javascript
flash
browser
localization
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.browserLanguage
(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.
You said your website has Flash, then, as another option, you can get operation system's language with flash.system.Capabilities.language
— see How to determine OS language within browser to guess an operation system locale.
You can also try to get the language from the document should might be your first port of call, then falling back to other means as often people will want their JS language to match the document language.
HTML5:
document.querySelector('html').getAttribute('lang')
Legacy:
document.querySelector('meta[http-equiv=content-language]').getAttribute('content')
No real source is necessarily 100% reliable as people can simply put in the wrong language.
There are language detection libraries that might let you determine the language by content.
I did a bit of research regarding this & I have summarised my findings so far in below table
So the recommended solution is to write a a server side script to parse the Accept-Language
header & pass it to client for setting the language of the website. It's weird that why the server would be needed to detect the language preference of client but that's how it is as of now There are other various hacks available to detect the language but reading the Accept-Language
header is the recommended solution as per my understanding.
I used all the answers and created a single line solution:
const getLanguage = () => navigator.userLanguage || (navigator.languages && navigator.languages.length && navigator.languages[0]) || navigator.language || navigator.browserLanguage || navigator.systemLanguage || 'en';
console.log(getLanguage());
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
On Chrome and Firefox 32+, navigator.languages contains an array of locales in order of user preference, and is more accurate than navigator.language, however to make it backwards-compatible (Tested Chrome / IE / Firefox / Safari), then use this:
function getLang()
{
if (navigator.languages != undefined)
return navigator.languages[0];
else
return navigator.language;
}
Source: Stackoverflow.com