JavaScript for detecting browser language preference [duplicate]

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:

Leave a Comment