Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getting browser current locale preference using javascript?

Does anyone know how to obtain the browser culture from Firefox and Google Chrome using javascript? Note: This is an asp.net 3.5 web application.

The requirement is to try and set the application's display culture based on the browser culture. I have found very few bits and pieces of information for the other browsers but they do not seem to work.

I am able to get it in IE with the following snippet of code:

var browserCulture = this.clientInformation.browserLanguage; 

Any info would be great!

like image 788
The Sheek Geek Avatar asked Apr 20 '10 20:04

The Sheek Geek


People also ask

How do I know my browser language preferences?

Chrome. Open the browser settings, and in the advanced section scroll down to find Languages . Open Language and Input Settings and add the language or language+region choice you want from the list available. Order the resulting list so that it is in descending order of preference.

What is my current locale?

Find the System Locale with the System Information appPress the Win + R hotkeys together on the keyboard and type the following command in your Run box: msinfo32 . Click the System Summary section on the left. On the right, see the Locale value.

What is locale in JavaScript?

Locale (Runtime - JavaScript) A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.


1 Answers

The following properties exist on the navigator object (which can also be known as clientInformation on IE but there's no reason ever to use that name):

  • language (non-IE, browser install language)
  • browserLanguage (IE, browser install language)
  • userLanguage (IE, user-level OS-wide language setting)
  • systemLanguage (IE, OS installation language)

But! You should never use any of these properties! They will be the wrong language in many cases.

None of them reflect the language settings the user actually gets to configure in the browser's ‘preferred languages’ UI, and they are difficult-to-impossible for users to change. You will cause big frustration by using any of these values without an additional easy manual way to switch languages.

The correct place you should sniff to decide what language to use by default, as configured by the normal browser UI, is the Accept-Language header passed to your server in the HTTP request. This is a ranked list of preferred languages from which you can pick, and it's what ASP.NET uses to guess an automatic client Culture, if you use that.

Unfortunately, this property is not available from JavaScript!

What you typically do is use your server side to parse the Accept-Language header and choose a single appropriate language to use from it. In ASP.NET you can get a pre-sorted list from HttpRequest.UserLanguages and pick the first that you like.

You then spit that language name out into a <script> element to pass the language information to the client side.

like image 119
bobince Avatar answered Sep 29 '22 17:09

bobince