Forum Moderators: open

Message Too Old, No Replies

I need help with js code for language redirect

         

Tribe

10:13 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Ok. here is the current code I have, & it does seem to work as far as I know. However, I'd like to add more languages to be redirected to their own pages..

Here is what I have now;

<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('zh') > -1 ¦¦ language.indexOf('zh-cn') > -1 ¦¦ language.indexOf('zh-hk') > -1 ¦¦ language.indexOf('zh-mo') > -1 ¦¦ language.indexOf('zh-tw') > -1 ¦¦ language.indexOf('zh-sg') > -1)
document.location.href = 'http://www.domain.com/chinese.html';
</script>

How do I add more languages & redirect them to another page? Ie: korean to korean.html
japanese to japanese.html
etc, etc..

will the same script used more than once work? Like this

<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('zh') > -1 ¦¦ language.indexOf('zh-cn') > -1 ¦¦ language.indexOf('zh-hk') > -1 ¦¦ language.indexOf('zh-mo') > -1 ¦¦ language.indexOf('zh-tw') > -1 ¦¦ language.indexOf('zh-sg') > -1)
document.location.href = 'http://www.chevynova.be/China/superad2.html';
</script>
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('jp') > -1)
document.location.href = 'http://www.domain.com/japanese.html';
</script>
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('ko') > -1)
document.location.href = 'http://www.domain.com/korean.html';
</script>


will it work like this?

Moby_Dim

9:45 am on Jan 28, 2006 (gmt 0)

10+ Year Member



Seems simple programming basics are needed here.

1)do not repeat var declaration for the same variable.

2)if - else if - else construction :

if(language.indexOf('zh') > -1...) {
....
}
else if(language.indexOf('jp') > -1) {
...
}
//more else if , if needed..
else {
....
}

Tribe

12:20 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



None of that made any sense to me?
I didn't write the code, it was a modified cut & paste job provided to me. I am not familiar at all with the construction of these, and as far as repeating goes, from what I do know, zh-cn, zh-tw, etc are separate identities, not repeated, to an extent. So, if eliminated by specific & just break down to simple zh, will this cover & include all versions of zh = cn, tw, mo, hk, sg?

also, using your example with the else if, at what point would I have the sending information?

I am sending each language to a separate page?
Sorry if I misunderstand, but I really don't know about writing the code.

Bernard Marx

4:15 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you look at the test for Chinese, the first test:

language.indexOf('zh') > -1

will catch all the others ('zh-cn','zh-hk' etc), so the other tests are redundant.

We are (I assume) just looking to map 2-char codes to full languages, so as to use them in document names. The easiest way to do that in JS is to use an Object literal..

/* change corrupted ¦¦ chars for pipes */


/*--configs--*/
var urlBase = "http://www.example.com/";
var defaultLang = "japanese";

var languageMap =
{
cn: "chinese",
ko: "korean",
jp: "japanese"
}

/*--*/
var majorLang = (navigator.language¦¦navigator.browserLanguage).substring(0,2);

var url = urlBase+(languageMap[majorLang]¦¦defaultLang)+".html";