View Full Version : Language select page

03-22-2007, 07:28 PM
Hi there.

Im building a website which will be made in two languages - English and Spanish. All the content is split in two (eg an English folder and Spanish folder) and technically its two sites (domain.es/english/index.php and domain.es/spanish/index.php).

Ive seen some sites which have a main index page that asks you to select the language you want and then remembers it so next time you go straight to the language you asked for ogirinally (I think the FIFA ones do it).

How is this done? I know in theory its just a link to the spanish and english sites, but how can I make the site remember which one for the next time people come, rather than making them ALWAYS go through a selection page?


03-22-2007, 07:44 PM
you could do this using a cookie but users delete them so this is where your site would forget the choice and they would have to select the language again

03-22-2007, 07:54 PM
cookies? I know about them but i dont understand how they work - are the programmed like html or something, or are they premade and you have to find the right one for what you want?

Thanks for your help davidj.

03-22-2007, 08:03 PM
a cookie is a file created by your site with information in it and is stored on the users machine

only the site (domain) that created the cookie can access the info within the file.

the user selects the language and at that moment the cookie is created and populated with the language choice. When the user returns to the site the site checks for the cookie first. If the cookie exists then it reads the file. If the cookie does not exist then you will prompt for a choice of language

03-22-2007, 09:54 PM
brilliant, thank you.
Just one thing, how do you write a cookie? is it done as HTML? What extension is it saved as? Sorry all the questions but i know nothing.

03-22-2007, 10:32 PM
drop this in an empty page and run it.

select the langauge then close the page and then revisit it. You will be redirected to the relevant page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function setLang(lang){
document.cookie = "lang="+lang+"; expires=Mon, 13 Jul 2012 05:28:21 UTC; path=/";

reloc = document.location;

switch (document.cookie){

case 'lang=spanish': reloc.href = "spanish.htm"; break;
case 'lang=english': reloc.href = "english.htm"; break;


Select a language
<p><a href="#" onclick="setLang('english')">english</a></p>
<p><a href="#" onclick="setLang('spanish')">spanish</a> </p>

03-22-2007, 10:33 PM
you are a wonderful human being - thank you

03-25-2007, 07:19 PM
Hi again..

Thanks for the code - ive been playing round with it and trying to make it work, but for whatever reason it doesnt.

The site is at www.321flash.es (http://www.321flash.es) and the language options are /spanish/index.php and /english/index.php - what do I need to put, and where, to make it work? As it is, it doesnt do anything, and then when i try to put the address that i think will make it work, it redirects but doesnt remember (ie, when i go back to the cookie page it just asks the language again).

Sorry to be so annoying, but i really cant get it to work, the little tinker.

04-04-2007, 09:00 PM
PLEASE can someone help me with this?

04-04-2007, 09:03 PM
where you have ...

switch (document.cookie){

case 'lang=spanish': reloc.href = "spanish.htm"; break;
case 'lang=english': reloc.href = "english.htm"; break;


change to this...

switch (document.cookie){

case 'lang=spanish': reloc.href = "spanish/index.htm"; break;
case 'lang=english': reloc.href = "english/index.htm"; break;


04-17-2007, 09:27 PM
Hi davidj

I was looking for the answer to the same question, i.e. language selection.

I have tried your example and it works great (almost to simple as I have seen other scripts that look very complicated)

One further question. I want on the English & Spanish pages a drop down to reselect another language (you may have hit the wrong link).

Do you have to delete the original cookie (if so how) or do you just write the cookie again but with the new var set.

Re Dave

04-18-2007, 06:05 AM
i think it is overwritten as long as the value pair is the same

clear your cookies and try it