View Full Version : How do I switch pages automatically
Barry
10-23-2002, 05:48 PM
I have a web site that needs to be bilingual. What I would like to do is have a link on the top of each page that will switch the current page to the other language. THe link would be "english" on the french page and "french" on the english page. What I do not want to do is hard code each of these links. Can I somehow get the page the user is on and switch them with a single language link?
Thanks
Barry
vkidv
10-23-2002, 06:06 PM
i dont know how you would do that, but would probably take a some amount of coding, why dont you make an entrance page and the visitor/reader can choose which language...with the french flag and the english flag?
or you could always once the person has click english/french make it open a new window...
there were just my suggestions and what i would/could do
requestcode
10-23-2002, 06:19 PM
Here is a link to a script in JavaScriptkits Free Script section that will redirect based on the language setting of the browser. Perhaps you can adapt it to your needs.
http://www.wsabstract.com/script/script2/language.shtml
Barry
10-23-2002, 06:21 PM
Thanks vkidv
I saw a javascript that would do this a couple of years ago so I am hoping someone knows how it was done.
The decision was made that each page would switch. Seems that some people read in english until they do not understand something, then they switch to the french page, and then switch back to the english pages again. Also sometimes the English page is more current because the French text is being translated.
thanks
Barry
Barry
10-23-2002, 06:35 PM
Thanks requestcode.
I do not have the knowledge to modify it but it is encourging that this script exists, perhaps what I need is "out there".
Barry
requestcode
10-23-2002, 08:42 PM
You could place this script in the body section of your document and it would write out a link for either french or english depending on what there browser setting is.
<html>
<head>
<title>(Type a title for your page here)</title>
</head>
<body>
SOME TEXT FOR ME<BR>
<SCRIPT LANGUAGE="JavaScript">
var type=navigator.appName
var lang=""
if (type=="Netscape")
{var lang = navigator.language}
else
{lang = navigator.userLanguage}
//cut down to first 2 chars of country code
var lang = lang.substr(0,2)
// english
if (lang == "fr")
{document.write("<A HREF='frenchver.html'>Frence Version</A>")}
else
{document.write("<A HREF='englishver.html'>English Version</A>")}
</SCRIPT>
</body>
</html>
The problem is if they don't have there language setting set correctly. Also it will still write out a link for french if they have that language setting bu the page is in french. The only other option would be to have two links one for each language.
Barry
10-23-2002, 08:47 PM
Thanks again requestcode
As you mention the problem will be that most surfers will not have the correct language setting.
I will try the code though.
thanks
Barry
Roelf
10-23-2002, 08:53 PM
as far as i understand, the link shown must not be dependant of the users language setting, but from the language of the ppage he is viewing at the moment. If the user is viewing the french page there should be a link to the english one. You can accomplish this by setting some sort of languagecode in the pagename (ore pagetitle) like yourpagename_fr.html with the english version being yourpagename_en.html
then you can strip the currrent language from this name, cut two characters off and stick the other language abbrev. on and provide a link to it. Pseudo code:
if pagename.indexof("fr") > 0
write the link to the english version of this page
by cutting of the wo last characters
then add "en" to it and provide the link
else
write the link to the french version of this page
by cutting of the wo last characters
then add "fr" to it and provide the link
dont have the time to code and test it but this is the general idea
stick it into a js file and link it into each page
Barry
10-23-2002, 09:14 PM
Hi Roelf
Thats exactly what I need but I can not code it as I do not know how.
Would someone else be willing to give it a try.
thanks
Barry
Roelf
10-23-2002, 09:19 PM
hi barry,
its after 10 pm local time here, i have to sleep now, tomorrow morning i will code it for you, if no one else has beaten me to it :)
Barry
10-23-2002, 09:51 PM
Thanks Roelf
Its only 4:50 pm here, have a good sleep.
regards
Barry
beetle
10-23-2002, 10:06 PM
function insertLink() {
var url = top.location.href;
var eng = (/_en/.test(url));
var label;
if (eng) {
url = url.replace("_en","_fr");
label = "French";
}
else {
url = url.replace("_fr","_en");
label = "Anglais";
}
document.write('<a href="'+url+'">'+label+'</a>');
}
<script>
insertLink();
</script>Not debugged :D
This could be modified to NOT use a document.write() and use a DOM method instead...
Barry
10-24-2002, 01:01 AM
thanks Bettle
I am going to hang my complete lack of knowledge out here for everyone to see.
I tried the code and it does not switch it just reloads the test.htm page. I have a file test_en.htm along with test_fr.htm and the test file with the code in it as follows in a dir. on the sever. What am I missing??
-----------------------------------------------
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script language="javascript"> I added this line
function insertLink() {
var url = top.location.href;
var eng = (/_en/.test(url));
var label;
if (eng) {
url = url.replace("_en","_fr");
label = "French";
}
else {
url = url.replace("_fr","_en");
label = "Anglais";
}
document.write('<a href="'+url+'">'+label+'</a>');
}
<script>
insertLink();
</script>
</body>
</html>
----------------------------------------------------------
thanks
Barry
Roelf
10-24-2002, 09:48 AM
It does work, i changed it a bit:
make a file: yourpagename_fr.html, this is the page with the french content, like this:
<html>
<head>
<title></title>
</head>
<body>
<!-- next line produces the link -->
<script language="JavaScript" src="switch.js"></script>
This is the french version of the page
here your content
</body>
</html>
make another file: yourpagename_en.html, this will be the exact same page with the english content:
<html>
<head>
<title></title>
</head>
<body>
<!-- next line produces the link -->
<script language="JavaScript" src="switch.js"></script>
This is the english version of the page
here your content
</body>
</html>
then create a file: switch.js and put this code in it (beetles code but a bit changed):
//------------------------------
var url = top.location.href;
var eng = (/_en/.test(url));
var label;
if (eng) {
url = url.replace("_en","_fr");
label = "French";
} else {
url = url.replace("_fr","_en");
label = "Anglais";
}
document.write('<a href="'+url+'">'+label+'</a>');
//---------------------- end of file----------------
now open one of the pages in the browser, and there shoul be a link to the other language page
Roelf
Barry
10-24-2002, 08:28 PM
It works, It works, he yelled.
Thanks Roelf for the complete code. I am sure that others will find this very useful.
Sorry Bettle that I was not knowledgeable enough to follow your suggestion. I appreciate your try.
regards
Barry
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.