Detecting user language and branching

M

mouton

Hello,

I am a beginner. My website has 2 pages: one in English and one in
French. I would like non English people to be directed to the English
page. There I would like to display a particular sentence in 1 of 3
languages depending on the language of the visitor (this sentence would
be in Spanish or Italian or German and the rest of the page in English).

I have found this code to detect the language:

<SCRIPT language="javascript">
<!--
function getlanguage(){
language=navigator.language;
if(language)
{language=language.substring(0,2);}
}
switch (language){
case "en" :
window.location="english.htm";
break;
case "fr" :
window.location="french.htm";
break;
default :
window.location="english.htm";
}

}
// -->
</SCRIPT>

But then I do not know if this code is standards compliant and what to
do in the English page to display the sentence in one of the 3 languages...

Can you please help me.

Thank you.
 
S

shimmyshack

Hello,

I am a beginner. My website has 2 pages: one in English and one in
French. I would like non English people to be directed to the English
page. There I would like to display a particular sentence in 1 of 3
languages depending on the language of the visitor (this sentence would
be in Spanish or Italian or German and the rest of the page in English).

I have found this code to detect the language:

<SCRIPT language="javascript">
<!--
function getlanguage(){
language=navigator.language;
if(language)
{language=language.substring(0,2);}}

switch (language){
case "en" :
window.location="english.htm";
break;
case "fr" :
window.location="french.htm";
break;
default :
window.location="english.htm";

}
}

// -->
</SCRIPT>

But then I do not know if this code is standards compliant and what to
do in the English page to display the sentence in one of the 3 languages...

Can you please help me.

Thank you.

do you have any control over your server at all - this is best done
server side. Do you have access use a scripting language like php?

server - apache: use Multiviews, saving your pages as dodah.html.en,
dodah.html.fr and .es
scripting - php: use the value of $_SERVER['HTTP_USER_AGENT'] and look
through it to find the one at the start of the comma separated list.
Use that to return the strings you need within the doc sending
header( 'Content-Language: ' . $lang );
where $lang is en, fr or es and your script

the better way is to either use rewrites on apache and have you
content appear to the user to come from http://server.com/lang/path/to/file
or since your site is small, actually separate your content.

if you use scripting the best way is to use a session to persist the
browsers choice and use that to send appropriate content, setting the
right lang attributes in your xhtml if needed as well. Also dont
forget to allow the user to override the setting, use a cookie in
apache setting prefered-lang or with a post to a script in php which
overrides the session preferred lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top