Forum Moderators: open
Hope that you find this useful and would be grateful for any feedback on compatibility with browsers.
P>S Didn;t try the server side stuff - just stuck to javascript here.
1)
[javascriptkit.com...]
Description: If your site is presented in more than one language (ie: English and Chinese), use this script to automatically detect the language setting of your visitor's browser, and redirect them to the appropriate section within your site. The script relies on the browser-returned ISO 639 code for determining language type of the product.
Example: N/A
Directions: Simply insert the below code into the <head> section of your "redirection" page. Read documentation inside script for details on configuring it.
<script>
/*
Browser Language Redirect script- By JavaScript Kit
For this and over 400+ free scripts, visit [javascriptkit.com...]
This notice must stay intact
*/
//Enter ISO 639-2 letter Language codes to detect (see: [w3.org...]
var langcodes=new Array("en", "fr", "es", "ja", "zh", "default")
//Enter corresponding redirect URLs (last one is for default URL):
var langredirects=new Array("index.htm", "french.htm", "spanish.htm", "japan.htm", "chinese.htm", "index.htm")
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>
2)
VERSION 4 BROWSERS ONLY
THE WEBSITE FOR THIS SITE SAID THAT YOUR LANGUAGE WAS NOT SET UP
(Version 4.0+ browsers) Redirects a user to the page based on their language. For example, English language user browsers, which report the language as 'en' in most cases, can be redirected to an English page, while Spanish language user browser, which report the language as 'es' in most cases, can be sent to a separate Spanish page. This method works well for redirecting users in all the languages that you set up. Instructions on adding more languages are included in the source code.
<!-- 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 -->
3)
INTERNET EXPLORER ONLY
Title: User Language and Country
Contributor: Jason Specht (webmaster@sweetyguy2001.com)
Contributor URL: [sweetyguy2001.com...]
Details: 11.69 KB * Uploaded August 6 2001
Description: Identify the language and country of the user's browser. Very comprehensive! IE only.
4)
SERVER SIDE scripts
[dotnetjohn.com...]
[webtips.dan.info...]
5) From webmaster forum - the e-mail that bignooz very kindly sent me via a stickymail
<script language="Javascript1.2">
function recup_lg()
{
var lgr;
if (verif_lg(navigator.userLanguage)) { lgr = verif_lg(navigator.userLanguage); }
else if (verif_lg(navigator.systemLanguage)) { lgr = verif_lg(navigator.systemLanguage); }
else if (verif_lg(navigator.browserLanguage)) { lgr = verif_lg(navigator.browserLanguage); }
else if (verif_lg(navigator.language)) { lgr = verif_lg(navigator.language); }
else { lgr = 'us'; }
return lgr;
}
function verif_lg(lg_tmp)
{
if(lg_tmp)
{
if(lg_tmp.indexOf('fr')!= -1) { return 'fr'; }
else if (lg_tmp.indexOf('en')!= -1) { return 'us'; }
else if (lg_tmp.indexOf('es')!= -1) { return 'es'; }
else if (lg_tmp.indexOf('pt')!= -1) { return 'pt'; }
else if (lg_tmp.indexOf('nl')!= -1) { return 'nl'; }
else if (lg_tmp.indexOf('de')!= -1) { return 'de'; }
else if (lg_tmp.indexOf('it')!= -1) { return 'it'; }
else { return ''; }
}
}
var lg_nav = recup_lg();
var url_redir;
if(lg_nav == 'fr') { url_redir = 'http://url1'; }
else if (lg_nav == 'us'){ url_redir = 'http://url2'; }
else if (lg_nav == 'de'){ url_redir = 'http://url3'; }
else if (lg_nav == 'it'){ url_redir = 'http://url4'; }
else if (lg_nav == 'pt'){ url_redir = 'http://url5'; }
else if (lg_nav == 'es'){ url_redir = 'http://url6'; }
else if (lg_nav == 'nl'){ url_redir = 'http://url7'; }
else{ url_redir = 'http://url8; }
window.location =</script> url_redir;
Sure, those long-winded if, else if, else if.. blocks could be cut down into lean object literal switches, and a few more things besides, but that's just window dressing. Fact is, I don't have a clue what browser supports what particular object property for getting the language setting.
I do find client-side redirection irritating though.
Even with the Accept-Language header, my reading suggests that content negotiation is a bit of a gray area with both Agent-driven and Server-driven models. Ultimately, the server is the sole arbiter of content negotiation matters.
Characteristically, JavaScript never sees a request header, it is generated by the user agent internally and processed at the server. The headers JS deals with are, at least for the most part, entity headers defining the character and content of a page. The response header, Content-language, is such a header, which specifies the language of the delivered page.
JavaScript can, through its manipulation of the HTML <meta> element's http-equiv property, modify the outgoing HTTP Request headers, but it doesn't have insight into the user agent's internals, unless the UA exposes it, as Netscape does with navigator.language and IE does with navigator.browserLanguage and .userLanguage.
To summarize: As far as I can tell, the preferred language of a user agent can only be reliably (with limits) derived in the HTTP 1.1 request header at the server. This assumes the user agent makes that information available, which appears to be a better bet than the proprietary navigator properties of language, userLanguage or browserLanguage.
I can live without it working in firefox I guess.
var nav_lng="peed-jahn_brah"; //default language string
if(navigator.language)nav_lng=navigator.language;
if(navigator.browserLanguage)nav_lng=navigator.userLanguage;
alert(nav_lng);
For a solution that serves different pages to browsers with different language settings, I would suggest that a more efficient solution could be engineered by designing beyond simply JavaScript. For instance, if the pages for different languages were kept in separate sub-directories, a script using regular expressions could parse the current location and redirect the browser to the appropriate directory, if needed. This would probably facilitate improved code portability.
Rather unbelievably Firefox does seem to work differently to Mozilla. HELP!
I tried your code in Firefox - got the peed-jahn_brah. So, it doesn't seem to recognise any of those terms. I also tried the following code and still got the peed-jahn_brah.
<SCRIPT LANGUAGE="JavaScript1.2">
var nav_lng="peed-jahn_brah"; //default language string
if(navigator.language)nav_lng=navigator.language;
if(navigator.browserLanguage)nav_lng=navigator.userLanguage;
if(navigator.systemLanguage)nav_lng=navigator.systemLanguage;
if(navigator.browserLanguage)nav_lng=navigator.browserLanguage;
if(navigator.userLanguage)nav_lng=navigator.userLanguage;
alert(nav_lng);
</script>
So - Firefox doesnt recognise the terms:
navigator.browserLanguage
navigator.systemLanguage
navigator.userLanguage
navigator.language
I am out of ideas. What term does it use? Have you any ideas? Do you know how I would go about finding out?
Additionally, here's the Mozilla Foundation's take on how the property should behave:
[mozilla.org...]
But just to repeat what I got there (was the same thing that you said basically):
This was a specific bug in Firefox 1.0PR that has since been fixed, it should work in all other versions of Firefox.
p.s. I'll spare you the "this should be done on the server" speech.
And my reply:
Thanks for your prompt reply mate. I understand that server is better - and I am looking into that as well.
The thing is that I only downloaded firefox yesterday - downloaded Firefox 1.0PR, as this was the default quick download. But I cannot get the above code to work in it. Help! So, if the bug has been fixed in 1.0PR - why will this code not work for me?
As an illustration of the problems that I am having - perhaps try this code:
<SCRIPT LANGUAGE="JavaScript1.2">
var nav_lng="peed-jahn_brah"; //default language string
if(navigator.language)nav_lng=navigator.language;
if(navigator.browserLanguage)nav_lng=navigator.userLanguage;
if(navigator.systemLanguage)nav_lng=navigator.systemLanguage;
if(navigator.browserLanguage)nav_lng=navigator.browserLanguage;
if(navigator.userLanguage)nav_lng=navigator.userLanguage;
if(window.navigator.language)nav_lng=window.navigator.language;
alert(nav_lng);
</script>
The code returns the alert peed-jahn_brah as my Firefox doesn't recognise any of these terms:
navigator.browserLanguage
navigator.systemLanguage
navigator.userLanguage
navigator.language
window.navigator.language
I would be so grateful for any help.
So - basically the bug has been fixed apparantly - but I cannot see any evidence of this. And I only downloaded firefox yesterday. Gutted.
:
You misunderstood me, the bug was (and is) in 1.0PR but has been fixed in newer development builds, try for example downloading a build from here:
ftp://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-0.9/
The latest official release (1.0PR) still has the bug, the latest development builds (see above link) don't.