1

Possible Duplicate:
JavaScript for detecting browser language preference

I tried using the following js code for detecting a users language, but each method results in undefined. My users use the system in two languages EN & FA.

Can I detect a users language by javascript or jQuery? If yes, how?

Demo: http://jsfiddle.net/AqXsp/1/

$('input').live('keyup', function(){
    alert(navigator.userLanguage); // Output this is "undefined"
    alert(navigator.systemLanguage); // Output this is "undefined"
    alert(navigator.browserLanguage); // Output this is "undefined"
})
6
  • This is not trivial and may need server-side scripting - see the link above Commented Dec 22, 2011 at 9:33
  • No, i read it, but it doesn't help me. Commented Dec 22, 2011 at 9:35
  • why not? As said, according to the top answer there, there simply is no simple way for this. Commented Dec 22, 2011 at 9:36
  • I don't know how Should use from if in keyup for alert output, please give me example in jsfiddle.net. Commented Dec 22, 2011 at 9:46
  • This is nothing you can (nor should) do in the keyup event. As said, it's a bit more complex than that. Commented Dec 22, 2011 at 9:48

1 Answer 1

6

The answer @Pekka pointed to is probably your best bet. It's actually an elegant solution, considering there is no such facility in JavaScript itself.

The author of the solution provides a very clear example:

var language;
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        language = headers['Accept-Language'];
        nowDoSomethingWithIt(language);
    }
});

Of course, you could set language to some default value. And the result is an unparsed string, so there would need to be some more logic in nowDoSomethingWithIt().

Have a look at this working demo: http://jsfiddle.net/seYLA/

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

5 Comments

This url: ajaxhttpheaders.appspot.com for my users have error "403. That’s an error. Your client does not have permission to get URL / from this server.You are accessing this page from a forbidden country. That’s all we know." How fix it?
I don't know what country you're in, but it's hosted on Google App Engine. Works fine here in the Netherlands. Anyway, if you've read the previously mentioned answer as you say you have, you know that it's just a Python script, which you can run on your own server if needed. So, again: read the previously mentioned answer: stackoverflow.com/a/3335420/990877
Where is Python script for download and upload it in server?
Ehm... the code is in the answer. Right underneath "Edit 2: As requested here is the code that is running on AppEngine". If you don't know how to run Python scripts on your server, or you don't have a server... This is not the place to ask for help. Extended discussions belong on a forum.
The simplw way is a short bit of javascript, as answered by Marco (2nd answer) on the 'duplicate' link, see link below var userLang = navigator.language || navigator.userLanguage || navigator.browserLanguage || navigator.systemLanguage; gu.illau.me/posts/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.