0

I'm using Chrome for my development, but I don't think there's anything browser-specific in my question.

I'm writing a Javascript app that needs to know the language the browser is using. According to this, I shouldn't use navigator.language; I should instead perform some dummy ajax request and see what the browser put in the request's Accept-Language header.

As far as I can tell, there's no way to access an ajax request's request headers programmatically. The xhr object I get back in my request's success callback has a method called getAllResponseHeaders(), but mentions nothing about request headers.

If this is the correct methodology for getting the user's language preference, I must be missing something obvious. How do I access the Accept-Language header from within Javascript? Or, if what I'm doing is as roundabout as it seems to me, what's an easier way to find out the user's language preference?

Community
  • 1
  • 1
dshapiro
  • 1,075
  • 14
  • 24
  • I read that one. It tells you how to access information that would be used in certain request headers. That only works if the information you are looking for is stored in `document` or `navigator` or some other such object. – dshapiro Feb 27 '13 at 21:59
  • It also says: *"As others have indicated, the HTTP headers are not available, but you specifically asked about the referer and user-agent, which are available via Javascript."* – ThinkingStiff Feb 27 '13 at 22:03
  • Blah. Someone in the answer to that question does say that there's no way to access the request headers from Javascript without some special server-side code. Is there no easier way to know the language the user wants? – dshapiro Feb 27 '13 at 22:03
  • Yeah, you are right. This is a little depressing. – dshapiro Feb 27 '13 at 22:04
  • This has some ideas on language detection: http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – ThinkingStiff Feb 27 '13 at 22:06
  • That's the one I cited in my question. The answer makes it sound so easy, but he assumes you have access to the request headers in the success callback. I guess one solution is to implement something like what he did on one of our servers. I'm still in disbelief that there's no simple way to just ask the browser what language it wants. But, anyway, it's nice to have that in my back pocket. Thanks! – dshapiro Feb 27 '13 at 22:09

0 Answers0