Friday, October 16, 2015

browser navigator language

The navigator object provides information about the browser, as there is no public standard applies to this object, there is quite different implementations for navigator in all major browsers, and so it is the same about browser language exposed in this object.

returns the language of the browser, but it is not user preference language through browser settings. Different browser has different implementation as of writing, like Chrome always returns browser language, while Firefox returns the first preference language.

This is new and well supported in Chrome and Firefox, which returns all browser preference languages. The value is an array.
For example, one return value is
Array [ "zh", "pt-BR", "es-AR", "fr", "zh-TW", "en", "pt", "ja", "en-US" ]
which maps to Http header Accept-Language

This new property added to navigator object gives javascript developer more power to do i18n for web application. We can quickly know user's preference language for this browser, then show translated version based on the first value appears in languages array.

Internet Explorer has the following three language related properties.
navigator.userLanguageRetrieves the operating system's natural language setting.

Retrieves the default language used by the operating system.

Retrieves the current operating system language.

Note that IE10 or early versions don't provide a navigator.language property like other major browsers. Check MSDN online document for details.

No comments:

Post a Comment