469,290 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

How to check HTTP_ACCEPT_LANGUAGE using Javascript?

It's quite easy in Php:

<?php

$language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$language_short = substr($taal,0,2);

echo $language_short;
?>
But would love to do it in Javascript......

Anybody?

Many thanks in advance!!
Edo.
Jul 20 '05 #1
8 14150

"Edo van der Zouwen" <ezouwen@haalweg_removthis_andthis_xs4all.nl> schreef
in bericht news:Xn*****************************@216.168.3.44. ..

But would love to do it in Javascript......

Anybody?


IE extends the navigator object with two properties you could reference:

navigator.systemLanguage
navigator.userLanguage

Doesn't work in other browsers and I'm not shure whether this would work on
a mac or not...
JW

Jul 20 '05 #2
Janwillem Borleffs wrote:
"Edo van der Zouwen" <ezouwen@haalweg_removthis_andthis_xs4all.nl> schreef
in bericht news:Xn*****************************@216.168.3.44. ..
But would love to do it in Javascript......

Anybody?

IE extends the navigator object with two properties you could reference:

navigator.systemLanguage
navigator.userLanguage

Doesn't work in other browsers and I'm not shure whether this would work on
a mac or not...


Gecko based browsers look like they use

navigator.language

My reference says this goes back to NN4.

.... this doesn't seem to give exactly what the ACCEPT_LANGUAGE header
would have, but it might be close enough ...

Regards,
Stephen

JW


Jul 20 '05 #3
Stephen <ss*****@austin.rr.com> wrote in
news:mR*******************@twister.austin.rr.com:
Janwillem Borleffs wrote:
"Edo van der Zouwen" <ezouwen@haalweg_removthis_andthis_xs4all.nl>
schreef in bericht
news:Xn*****************************@216.168.3.44. ..
But would love to do it in Javascript......

Anybody?

IE extends the navigator object with two properties you could
reference:

navigator.systemLanguage
navigator.userLanguage

Doesn't work in other browsers and I'm not shure whether this would
work on a mac or not...


Gecko based browsers look like they use

navigator.language

My reference says this goes back to NN4.

... this doesn't seem to give exactly what the ACCEPT_LANGUAGE header
would have, but it might be close enough ...

Regards,
Stephen

JW


Thanks for both replies. However. the .userlanguage .browserlanguage or
..systemlanguage properties give the language of the browser or system.

In a browser, you can however also set the 'preferred language' in which
you can see a webpage.

For example, many people outside the US or UK have an english browser,
but would like to see webpages in their home language. This can not be
solved by using the properties above.

Any more ideas?

Thanks,
Edo.
P.S. By the way, for those interested, this is the code for checking the
browser language:

(example for dutch, polish and english users, which will be directed to
index_en.html, index_pl.html or index_nl.html)

<SCRIPT LANGUAGE="JavaScript1.2"><!--
// The following only works in JavaScript 1.2 or greater:
function showpage(code) {

location = ("index_" + code + ".html");
}
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

var code = language.substring(0,2);

if (code == 'pl' || code == 'nl' || code == 'en')
showpage(code);
else
showpage('en');
//--></SCRIPT>
Jul 20 '05 #4
Edo van der Zouwen wrote:

[...snip suggestions to use navigator.userLanguage, navigator.language, etc. ...]
Thanks for both replies. However. the .userlanguage .browserlanguage or
.systemlanguage properties give the language of the browser or system.

In a browser, you can however also set the 'preferred language' in which
you can see a webpage.

For example, many people outside the US or UK have an english browser,
but would like to see webpages in their home language. This can not be
solved by using the properties above.

Any more ideas?

As far as I know, the HTTP request & headers sent with the request are
not accessible to javascript. This is probably (my guess) because these
are formed immediately before transmission and too late for server-side
scripting to make use of.

I believe I mentioned before, my prefered solution would be to use the
content-negotiation capabilities of HTTP 1.1 and configure the web
server to handle the content decision-making. How to do this is beyond
the scope of this NG, which is fortunate for me because it is presently
also beyond the scope of my knowledge. :-)

Sorry I've no further suggestions.
Stephen



P.S. By the way, for those interested, this is the code for checking the
browser language:

[...snip...]


Jul 20 '05 #5
Stephen <ss*****@austin.rr.com> wrote in
news:aN*******************@twister.austin.rr.com:

As far as I know, the HTTP request & headers sent with the request are
not accessible to javascript. This is probably (my guess) because
these are formed immediately before transmission and too late for
server-side scripting to make use of.

I believe I mentioned before, my prefered solution would be to use the
content-negotiation capabilities of HTTP 1.1 and configure the web
server to handle the content decision-making. How to do this is beyond
the scope of this NG, which is fortunate for me because it is
presently also beyond the scope of my knowledge. :-)

Sorry I've no further suggestions.
Stephen


Thanks for your input.

Cheers,
Edo.
Jul 20 '05 #6
I have found the following solution, which is a combination of php and
Javascript.

Look at how the variable from php is passed onto Javascript.

This code is based on the following input for the propertie:
http://www.weijers.net/sitemap.html

and the following for passing the variable:
http://www.sitepointforums.com/showt...hreadid=106385


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php

$preflanguage = $_SERVER['HTTP_ACCEPT_LANGUAG E'];
#Check the property
$lang = substr($preflanguage,0,2);
#Take the first 2 characters

if ( $lang == "nl" or $lang == "en" or $lang =="pl")
{
$filename = "index_" . $lang . ".html";
#create the filename to go to later

}

else

{
$lang = "en";
$filename = "index_en.html";
#create the filename to go to later

}

?>

<SCRIPT LANGUAGE=JAVASCRIPT>
location = ("<?php echo $filename; ?>");
//Let javascript redirect, using the php variable

</script>
</body>
</html>

I'm sure it isn't the most beautiful solution, but it works!

Edo.
Jul 20 '05 #7
Edo van der Zouwen wrote:
Stephen <ss*****@austin.rr.com> wrote in
news:aN*******************@twister.austin.rr.com:
As far as I know, the HTTP request & headers sent with the request are
not accessible to javascript. This is probably (my guess) because
these are formed immediately before transmission and too late for
server-side scripting to make use of.
I saw your solution, but just to correct my misstatement here: I meant
"too late for *client*-side scripting to make use of" ...
S.

I believe I mentioned before, my prefered solution would be to use the
content-negotiation capabilities of HTTP 1.1 and configure the web
server to handle the content decision-making. How to do this is beyond
the scope of this NG, which is fortunate for me because it is
presently also beyond the scope of my knowledge. :-)

Sorry I've no further suggestions.
Stephen

Thanks for your input.

Cheers,
Edo.


Jul 20 '05 #8
Edo van der Zouwen wrote:
I have found the following solution, which is a combination of php and
Javascript.

Look at how the variable from php is passed onto Javascript.

This code is based on the following input for the propertie:
http://www.weijers.net/sitemap.html

and the following for passing the variable:
http://www.sitepointforums.com/showt...hreadid=106385
[...snip the code sample...]
I'm sure it isn't the most beautiful solution, but it works!


A very acceptable solution, here. Good digging.
This also prompts me to believe that using server-side includes you
could achieve the same thing:

<script type="text/javascript">
var acceptLang="<!--#echo var="HTTP_ACCEPT_LANGUAGE"-->"
// figure out which language by parsing "acceptLang"
switch(myLanguage) {
case "nl":
...
break;
case "en":
...
break;
case "pl":
...
break;
default:
...
break;
}
// or something similar
</script>

Very like your PHP approach.

Stephen
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by BbT | last post: by
2 posts views Thread by tgundeck | last post: by
11 posts views Thread by Patrick | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.