Detects the language used by the client's browser
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>JsLib 1.3 - Exemple - langue.js</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Etienne CHEVILLARD">
<!-- langue.js -->
<SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">
/* langue.js
* Role : detecte la langue du navigateur du client
* Projet : JsLib
* Auteur : Etienne CHEVILLARD (echevillard@users.sourceforge.net)
* Version : 1.3
* Creation : 10/04/2001
* Mise a jour : 23/02/2005
*/
// --- Variables globales ---
// tableau des codes ISO
var langue_tabct=new Array("aa","ab","af","am","ar","as","ay","az","ba","be",
"bg","bh","bi","bn","bo","br","ca","co","cs","cy",
"da","de","dz","el","en","eo","es","et","eu","fa",
"fi","fj","fo","fr","fy","ga","gd","gl","gn","gu",
"gv","ha","he","hi","hr","hu","hy","ia","id","ie",
"ik","is","it","iu","ja","jw","ka","kk","kl","km",
"kn","ko","ks","ku","kw","ky","la","lb","ln","lo",
"lt","lv","mg","mi","mk","ml","mn","mo","mr","ms",
"mt","my","na","ne","nl","no","oc","om","or","pa",
"pl","ps","pt","qu","rm","rn","ro","ru","rw","sa",
"sd","se","sg","sh","si","sk","sl","sm","sn","so",
"sq","sr","ss","st","su","sv","sw","ta","te","tg",
"th","ti","tk","tl","tn","to","tr","ts","tt","tw",
"ug","uk","ur","uz","vi","vo","wo","xh","yi","yo",
"za","zh","zu");
// tableau des langues en francais
var langue_tablg=new Array("Afar","Abkhaze","Afrikaans","Amharique","Arabe",
"Assamais","Aymara","Azéri","Bachkir","Biélorusse",
"Bulgare","Bihari","Bêche-De-Mer","Bengali","Tibétain",
"Breton","Catalan","Corse","Tchèque","Gallois",
"Danois","Allemand","Boutani","Grec","Anglais",
"Espéranto","Espagnol","Estonien","Basque","Persan",
"Finnois","Fidjien","Féroïen","Français","Frison",
"Gaélique Irlandais","Gaélique Écossais","Galicien","Guarani","Goudjarati",
"Gaélique de l'Île de Man","Haoussa","Hébreu","Hindi","Croate",
"Hongrois","Arménien","Interlingua","Indonésien","Interlingue",
"Inupiak","Islandais","Italien","Inuktitut","Japonais",
"Javanais","Géorgien","Kazakh","Groenlandais","Cambodgien",
"Kannada","Coréen","Cachemiri","Kurde","Cornique",
"Kirghiz","Latin","Luxembourgeois","Lingala","Lao",
"Lithuanien","Letton","Malgache","Maori","Macédonien",
"Malayalam","Mongol","Moldave","Marathe","Malais",
"Maltais","Birman","Nauri","Népalais","Néerlandais",
"Norvégien","Occitan","Oromo","Oriya","Pendjabi",
"Polonais","Pachto","Portuguais","Quechua","Rhéto-Roman",
"Roundi","Roumain","Russe","Rouanda","Sanscrit",
"Sindhi","Sámi Du Nord","Sango","Serbo-Croate","Cingalais",
"Slovaque","Slovène","Samoan","Chona","Somali",
"Albanais","Serbe","Siswati","Sotho Du Sud","Soundanais",
"Suédois","Souahéli","Tamoul","Télougou","Tadjik",
"Thaï","Tigrigna","Turkmène","Tagal","Setchwana",
"Kitonga","Turc","Tsonga","Tatar","Tchi",
"Ouïgour","Ukrainien","Ourdou","Ouzbek","Vietnamien",
"Volapük","Ouolof","Xhosa","Yidich","Yorouba",
"Tchouang","Chinois","Zoulou");
// --- Fonctions ---
// retourne le code ISO de la langue du navigateur
function obtenirCodeLangueNavig() {
var lct="en";
if (navigator.language) {
lct=navigator.language.toLowerCase().substring(0, 2);
} else if (navigator.userLanguage) {
lct=navigator.userLanguage.toLowerCase().substring(0, 2);
} else if (navigator.userAgent.indexOf("[")!=-1) {
var debut=navigator.userAgent.indexOf("[");
var fin=navigator.userAgent.indexOf("]");
lct=navigator.userAgent.substring(debut+1, fin).toLowerCase();
}
return lct;
} // fin obtenirCodeLangueNavig()
// retourne la langue en francais correspondant au code de langue specifie
function obtenirLangue(code) {
if ((!code) || (code.length<2))
return "";
code=code.toLowerCase().substring(0, 2);
for (var i=0; i<langue_tabct.length; i++) {
if (langue_tabct[i]==code)
return(langue_tablg[i]);
}
return "";
} // fin obtenirLangue(code)
// retourne la langue du navigateur en francais
function obtenirLangueNavig() {
return (obtenirLangue(obtenirCodeLangueNavig()));
} // fin obtenirLangueNavig()
</SCRIPT>
</HEAD>
<BODY>
<H1>JsLib 1.3</H1>
<HR>
<H2>Exemple - langue.js</H2>
<NOSCRIPT>
<P><I>Erreur : votre navigateur ne reconnaît pas le Javascript ou est configuré pour ne
pas prendre en compte le code Javascript. Dans ce dernier cas, vous pouvez modifier la
configuration dans les préférences/options de votre navigateur.</I>
<HR>
</NOSCRIPT>
<P>Code de la langue de votre navigateur :
<SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(obtenirCodeLangueNavig())</SCRIPT>
<P>Langue de votre navigateur :
<SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(obtenirLangueNavig())</SCRIPT>
<P>Langue pour un code de langue :
<FORM ACTION="GET" NAME="f">
<TABLE SUMMARY="formulaire" BORDER=0 CELLSPACING=2 CELLPADDING=2>
<TR><TD>
Code :
</TD><TD>
<INPUT NAME="t1" TYPE="text" SIZE="30" MAXLENGTH="2" VALUE="zu">
<INPUT NAME="b1" TYPE="button" VALUE="Rechercher"
onClick="this.form.t2.value=obtenirLangue(this.form.t1.value)">
</TD></TR><TR><TD>
Langue :
</TD><TD>
<INPUT NAME="t2" TYPE="text" SIZE="30" VALUE="">
</TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Related examples in the same category