473,513 Members | 3,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14630

"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
5290
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)) { $_SESSION = "en"; } elseif(preg_match("/de/i", $_SERVER)) {
2
5452
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 photo size to 1 meg. I would like to check the file size in JavaScript on the page, instead of on the server. Anyone know how I can check the file...
6
61591
by: Jim | last post by:
How do I check if javascript is enabled in PHP? TIA, Jim
44
10130
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
5181
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this...
19
107863
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 sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular...
2
5164
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: nl,en-us;q=0.7,en;q=0.3 Opera: nl-NL,nl;q=0.9,en;q=0.8
11
2045
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 on a checkbox a dropdown window will open next to it and the selections will be 1, 2, and 3. I want to use the same script and dropdown for each of...
3
8248
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 the file size; however, I'm not happy with this solution because my application is designed to work in multiple browsers and ActiveX is limited to...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.