Accept-Language header accessible via Javascript?

5,297 views
Skip to first unread message

Thorsten Panknin

unread,
Dec 24, 2009, 4:23:29 PM12/24/09
to mozilla-la...@googlegroups.com
Hello,

I'm aware that this is more of a general Javascript question. Is there a
way to sniff the HTTP Accept-Language header with Javascript?

I'd like to use the info to set the default source language in a
translation jetpack. Am I wrong in assuming that this approach, if
viable, would be more reliable than checking for "navigator.language"?

Stuff like "navigator.browserLanguage", "navigator.systemLanguage" and
"navigator.userLanguage", as seen in the w3schools.com website, spit out
"undefined" in FF, Opera and Chromium on Linux (haven't tested those
browsers/IE on Windows yet).

I know that Firebug is able to get the HTTP headers, not sure how they
do it though and if it's via pure Javascript.

Any thoughts or ideas? Help is appreciated.

- Thorsten

niknah

unread,
Dec 24, 2009, 10:18:54 PM12/24/09
to mozilla-labs-jetpack
It's "navigator.language" in firefox.

Thorsten Panknin

unread,
Dec 25, 2009, 2:28:59 AM12/25/09
to mozilla-la...@googlegroups.com
> It's "navigator.language" in firefox.
I know, I wrote it myself in my first post, thanks.

As far as I know, "navigator.language" refers to the UI language though,
not to the preferred/accepted language of page content. Those values
might differ.

Is "navigator.language" the way to go, folks? What's best practise here?

- Thorsten

M.Terada

unread,
Dec 25, 2009, 2:38:26 AM12/25/09
to mozilla-la...@googlegroups.com
Maybe, intl.accept_languages variable in Preferences (about:config)

but jetpack does'nt have API for asccess to the perferences....


----
teramako
http://d.hatena.ne.jp/teramako/


2009/12/25 Thorsten Panknin <t...@thorstenpeh.de>:

> --
>
> You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
> To post to this group, send email to mozilla-la...@googlegroups.com.
> To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mozilla-labs-jetpack?hl=en.
>
>
>

Pike

unread,
Dec 25, 2009, 6:25:42 PM12/25/09
to mozilla-labs-jetpack
navigator.language has nothing to do with the accept-language headers,
fwiw.

Note that accept-language is a weighted list of locale codes, not sure
what value you're getting out of it, and I admit, I haven't fully
understood the use case here.

Axel

Thorsten Panknin

unread,
Dec 26, 2009, 7:53:21 AM12/26/09
to mozilla-la...@googlegroups.com
> navigator.language has nothing to do with the accept-language headers,
> fwiw.
Yeah, I'm aware of that. Don't think I've mixed them up, just trying to
find out how to approach a solution. I'm trying to find out what
language the user is running on her system. For the use case, see the
following paragraph.

> Note that accept-language is a weighted list of locale codes, not sure
> what value you're getting out of it, and I admit, I haven't fully
> understood the use case here.

I'm working on a translation jetpack utilising the mobile version of
dict.cc. Recently, multi-lingual support has been added to dict.cc and I
was thinking it might be nice to find out the user's system language and
set the "source language" for translations in the jetpack accordingly.

While researching, I found "navigator.language" in Javascript and
"Accept-Language" headers in the HTTP protocol as possible ways to find
out the language used. I read though that "navigator.language" is set to
the *UI* language and does not, necessarily, reflect the preferred
language for web content. So I concluded that getting the
Accept-Language header might be the solution here. Seems you cannot
access headers with Javascript alone though.

I have, obviously, no experience with this stuff. Hence my question for
ideas and help. An alternative/addition would always be to have the user
set source and target language themselves, of course.

- Thorsten

M.Terada

unread,
Dec 26, 2009, 9:37:34 AM12/26/09
to mozilla-la...@googlegroups.com
Hi,
See following code;

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com", false);
xhr.onload = function(){
var httpChannel =
xhr.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
console.log("AccpetLanguages: " +
httpChannel.getRequestHeader("Accept-Language"));
};
xhr.send(null);

But maybe using "Components" is deprecated

Best regards.
----
teramako
http://d.hatena.ne.jp/teramako/


2009/12/26 Thorsten Panknin <t...@thorstenpeh.de>:

Thorsten Panknin

unread,
Dec 26, 2009, 12:29:01 PM12/26/09
to mozilla-la...@googlegroups.com
teramako,

> var xhr = new XMLHttpRequest();
> xhr.open("GET", "http://example.com", false);
> xhr.onload = function(){
> var httpChannel =
> xhr.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
> console.log("AccpetLanguages: " +
> httpChannel.getRequestHeader("Accept-Language"));
> };
> xhr.send(null);

Thanks a lot for the code. I'll try it sometime these days! :)

- Thorsten

Byron Adams

unread,
Dec 26, 2009, 8:59:49 PM12/26/09
to mozilla-la...@googlegroups.com
Perhaps this will be usefull, http://kb.mozillazine.org/General.useragent.locale you could always look for existing firefox extensions that rely on or change the localization information such as the "Quick Locale Switcher"

sephr

unread,
Dec 28, 2009, 11:50:52 AM12/28/09
to mozilla-labs-jetpack
Actually, you can't trust navigator.language as it can be changed by
content scripts. You should use this instead if you want to get the
untaintable navigator.language:

Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("general.useragent.")
.getCharPref("locale")

sephr

unread,
Dec 28, 2009, 11:55:46 AM12/28/09
to mozilla-labs-jetpack
You want this:

Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("intl.")
.getCharPref("accept_languages")

Reply all
Reply to author
Forward
0 new messages