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
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
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.
>
>
>
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
> 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
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>:
> 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
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("general.useragent.")
.getCharPref("locale")
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("intl.")
.getCharPref("accept_languages")