2

I'm developing a web application that support multiple language, and I was asking myself which should be the best tier (if I can name it like that) to check for browser's language.

Should I check it on PHP using $_SERVER["HTTP_ACCEPT_LANGUAGE"] or will be better if I check it in Javascript using navigator.language?

Which and why, stackers?

2
  • Whatever you choose I would just make it the default language, I would always let the user change the language after the fact. Commented Apr 26, 2013 at 18:49
  • Of course I am doing it, but I just need to set a language to start seeing the page, and I thought that would be very closely if I choose it from the user's browser. Commented Apr 26, 2013 at 18:50

2 Answers 2

2

Actually they will both capture the language set in the browser. The main difference is that the $_SERVER supports multiple languages in order of preference, whereas navigator.language will only be able to select the first language the browser prefers.

Another difference is that one is captured at client side using JavaScript, and the other at the server side using HTTP headers. It is fair to say (even though headers can be spoofed) that capturing the value at the server side is more stable.

Say in Firefox using tools->options->content->language. If I set language to say Chinese

Then when I make a request and examine the HTTP headers it will say:

Accept-Language: zh,en-us;q=0.7,en;q=0.3 //zh is the two letter Chinese representation

and $_SERVER["HTTP_ACCEPT_LANGUAGE"] will give me zh,en-us;q=0.7,en;q=0.3

navigator.language will give me only zh

So in the header you can see that you can support multiple language options in order. So if for some reason Chinese cannot be rendered, you can check for the second language of preference.

If you use navigator.language you cannot do that..

Sign up to request clarification or add additional context in comments.

2 Comments

I did that before I posted my answer :) I have a US english installation. And without any changes alert(navigator.language) will show en. Now, if I set Chinese as my language in the browser using the steps I mentioned and move it to the top of the language order then alert(navigator.language) will show zh. You can try that too, since it is obvious you don't believe me either :)
I've just removed my embarrassing comments. I was mislead by MDN and a poor testing on my side. My excuses! (To clarify: Starting in Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), this property's value is based on the value of the Accept-Language HTTP header.)
2

You should use the $_SERVER["HTTP_ACCEPT_LANGUAGE"]. Note that this is a weighted list of preferred languages and you should use it accordingly (ie. don't just take the first language off the list)

navigator.language doesn't tell you which is the preferred language set in the browser, but the actual language of the browser application itself.

Edit: from quick testing, the navigator.language returns the preferred language in Firefox, browser language in Opera and Chrome and nothing in IE.

3 Comments

About navigator.language, apparently it isn't that way. In most modern browsers, it takes the value from the Accept-Language HTTP header. It can return fr even if your browser is in English.
@Álvaro G. Vicario: Which most modern browsers do you have in mind? Actually it seems to work differently in most browsers (user language in FF, browser language in Chrome and Opera, nothing in IE)... But in any case, the important thing here is that the HTTP_ACCEPT_LANGUAGE is a list of preferred languages, while the navigator.language at best returns just one of them.
The MDN article talks about Chrome 0.3.154+ and Firefox 5.0+; IE does not support it and there aren't details about the exact behaviour in Opera and Safari. Of course, proper parsing of HTTP_ACCEPT_LANGUAGE is the right way to go as you already state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.