Click to See Complete Forum and Search --> : Language detection


_LOBO_
06-17-2003, 08:19 AM
Im trying to make a laguage detection, this code detect the language from the web browser, and it works almost ok, but in MAC dosent work I test the page in OS X and OS 9, some one can give me a hand?

Thank you for STOP in my question nad for any help!

* One more thing someone can tell me how some web sites can detect your country? And how can I detect the language from the OS ?

--------------------------------CODE------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script>



//Enter ISO 639-2 letter Language codes to detect (see: http://www.w3.org/WAI/ER/IG/ert/iso639.htm):

var langcodes=new Array("en", "fr", "es", "pt", "de", "it", "default")



//Enter corresponding redirect URLs (last one is for default URL):

var langredirects=new Array("en.php", "fr.php", "es.php", "pt.php", "de.php", "it.php", "en.php")



var languageinfo=navigator.language? navigator.language : navigator.userLanguage

var gotodefault=1



function redirectpage(dest){

if (window.location.replace)

window.location.replace(dest)

else

window.location=dest

}



for (i=0;i<langcodes.length-1;i++){

if (languageinfo.substr(0,2)==langcodes[i]){

redirectpage(langredirects[i])

gotodefault=0

break

}

}



if (gotodefault)

redirectpage(langredirects[langcodes.length-1])

</script>
</head>
<body>
<br>
<br>
<br>
</body>
</html>

Khalid Ali
06-17-2003, 08:31 AM
navigator object is probably the best bet JavaScript has its limitations...You might want to check into ActiveXControls for MS+IE stuff there may be a better solution to read OS lang

_LOBO_
06-17-2003, 08:38 AM
Thanks Khalid

Can you please put a LINK where I can get more info about ActiveXControls for MS+IE and this controls work in NETSCAPE? You Know where I can find the code for make the language detection ?

_LOBO_
06-17-2003, 09:37 AM
This SCRIPT works fine in PC and MAC, tested in
PC Win XP IE 6
Mac OSX / 9 IE 5

I found the aswere!


<!-- ONE STEP TO INSTALL LANGUAGE:

1. Copy the coding into the HEAD of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Begin
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

/*-------------------------------------------------------------
Netscape instructions:
In Netscape, find the language you want to redirect by going to

Edit Menu --> Preferences...

Then click the '+' box next to 'Navigator' and click 'Languages'
Click 'Add' then find the languages you want and add them below.

---------------------------------------------------------------

Microsoft Internet Explorer instructions:
In MSIE, find the language you want to redirect by going to

Tools Menu --> Internet Options...

Then click the 'Languages' button near the bottom of the page.
Click 'Add' then find the languages you want and add them below.

-------------------------------------------------------------*/

if (language.indexOf('en') > -1) document.location.href = 'English.html';
else if (language.indexOf('nl') > -1) document.location.href = 'dutch.html';
else if (language.indexOf('fr') > -1) document.location.href = 'french.html';
else if (language.indexOf('de') > -1) document.location.href = 'german.html';
else if (language.indexOf('ja') > -1) document.location.href = 'japanese.html';
else if (language.indexOf('it') > -1) document.location.href = 'italian.html';
else if (language.indexOf('pt') > -1) document.location.href = 'portuguese.html';
else if (language.indexOf('es') > -1) document.location.href = 'Spanish.html';
else if (language.indexOf('sv') > -1) document.location.href = 'swedish.html';
else if (language.indexOf('zh') > -1) document.location.href = 'chinese.html';
else
document.location.href = 'English.html';
// End -->
</script>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.95 KB -->