473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 14619

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BbT | last post by:
On my site I have a code based on $_SERVER, to selelect language variable: if(!isset($_SESSION)) { if(preg_match("/pl/i", $_SERVER)) { $_SESSION = "pl"; } elseif(preg_match("/en/i", $_SERVER))...
2
by: tgundeck | last post by:
Does anyone know how I can check the file size of photos or any file type in JavaScript? I'm allowing users to store photos in my web page using the input type = file tag, but need to limit the...
6
by: Jim | last post by:
How do I check if javascript is enabled in PHP? TIA, Jim
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
2
by: RvT | last post by:
I have an interesting problem. I have FireFox2, IE7 and Opera9 as test browsers. All browsers are in Dutch. When I execute the following: <?=$_SERVER?> The result is as follows: FireFox:...
11
by: Patrick | last post by:
Trying this question again in a different way and expanding it to another newsgroup. Looking for how I would do this. For an html form; Say I have three check boxes A, B, and C . When I click...
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.