Annibigi Annibigi - 1 year ago 105
Javascript Question

JavaScript for detecting browser language preference

I have been trying to detect the browser language preference using JavaScript.

If I set the browser language in IE in

Tools>Internet Options>General>Languages
, how do I read this value using JavaScript?

Same problem for Firefox. I'm not able to detect the setting for

, it detects the setting done thru
Start>ControlPanel>RegionalandLanguageOptions>Regional Options

I have tested with
but neither returns the value for the first setting(

I found a link which discusses this in detail, but the question remains unanswered :(

Answer Source

I think the main problem here is that the browser settings don't actually affect the navigator.language property that is obtained via javascript.

What they do affect is the HTTP 'Accept-Language' header, but it appears this value is not available through javascript at all. (Probably why @anddoutoi states he can't find a reference for it that doesn't involve server side.)

I have coded a workaround: I've knocked up a google app engine script at that will return you the HTTP request headers via JSONP.

(Note: this is a hack only to be used if you do not have a back end available that can do this for you. In general you should not be making calls to third party hosted javascript files in your pages unless you have a very high level of trust in the host.)

I intend to leave it there in perpetuity so feel free to use it in your code.

Here's some example code (in jQuery) for how you might use it

    url: "", 
    dataType: 'jsonp', 
    success: function(headers) {
        language = headers['Accept-Language'];

Hope someone finds this useful.

Edit: I have written a small jQuery plugin on github that wraps this functionality:

Edit 2: As requested here is the code that is running on AppEngine (super trivial really):

class MainPage(webapp.RequestHandler):
    def get(self):
        headers = self.request.headers
        callback = self.request.get('callback')

        if callback:
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(callback + "(")
          self.response.headers['Content-Type'] = 'text/plain'
          self.response.out.write("I need a callback=")

application = webapp.WSGIApplication(
                                     [('/', MainPage)],

def main():

if __name__ == "__main__":

Edit3: Have open sourced the app engine code here: