Modifying a styleswitcher to make a language switcher.
March 26th, 2005This is a modified version of Alistapart’s PHP styleswitcher. The idea was to take a site that is both in English and Spanish and allow someone to make a semi-permanent decision on which language they would like to use. This project came through a friend of mine, and with few details, so I figured I’d pick a method that could be easily changed if it didn’t fit his current setup.
Here is a rudimentary example if you want to see it in action. (After you’ve chosen your language just try going to the other page in the address bar!)
My idea was that the english pages would be named something like index_en.html and the spanish pages would be named index_es.html. This means that to switch from one to the other, all you need to do is swap out the bit at the end of the filename. (You could modify this so that it swapped out a directory name instead, and then you could have a /en/ directory and a /es/ directory in the path name and essentially maintain two different folder structurers.) Okay, enough hypotheticals. Here is how to do it.
The first page you need to build is a modified version of the alistapart styleswitcher. Place this code in a file called switcher.php:
< ?php
setcookie ('sitelang', $set, time()+31536000, '/', 'yourdomain.com', '0');
header("Location: $HTTP_REFERER");
?>
This simply takes an argument passed to it through the url bar and redirects the user back to the same page but with a cookie set that contains the information passed to it. (Don’t forget to change the domain name.)
Next you need to put the following snippets of code a the very top of each of your pages, one for the Spanish pages and one for the English pages:
< ?php
if ($sitelang == '_en') {
$location = $_SERVER["PHP_SELF"];
$goto = str_replace("_es", "_en", "$location");
header("Location: $goto");
die();
}
?>
The first one goes in the top of your Spanish pages, an simply checks to see if the cookie says you should be on an english page, if it does, it replaces the _es in the page you are currently on with _en and then redirects you to the English page.
< ?php
if ($sitelang == '_es') {
$location = $_SERVER["PHP_SELF"];
$goto = str_replace("_en", "_es", "$location");
header("Location: $goto");
die();
}
?>
The English version works exactly the same way, except that it replaces _en with _es.
Finally you need to have some way for your browsers to choose their preferred language. In this case I chose to pass the argument through the URL, but you could create a form on your page that had a submit button and some radio buttons to make your choice. The following two links should be present somewhere on every page.
Oprima aquí para español!
<br />
Click here for English!
The first line lets you choose Spanish as your language and the second lets you choose English.
Sorry if my Spanish is a little out of practice. . .
Post a comment if you find this useful.
Update 3-28-05
After a little more discussion about the specifics of what my friend wanted, we came to the conclusion that while this works really well, it wasn’t quite what he was looking for.
The site he was looking to use this is actually 3 sites, where the first is a splash site for the other two.
So, the final decision was made to have the splash site allow a user to choose their language and to choose if that language is permanent or not. Were still working out the specifics, but here’s a way to set the language and forward people to the correct url based on their decision.
The index page for the splash site would contain the following code:
< ?PHP
if ($sitelang == '_es') {
header("Location: http://www.thespanishsite.org");
die();
}
if ($sitelang == '_en') {
header("Location: http://www. thesenglishsite.org");
die();
}
?>
Then within the body you need a form:
<form action="switcher.php" method="post" name="radio">
<label for="set">Español</label>
<input name="set" id="set" type="radio" value="_es" checked="checked" />
<br />
<label for="set">English</label>
<input name="set" id="set" type="radio" value="_en" />
<br />
<label for="length">Remember this setting</label><input name="length" id="length" type="checkbox" value="year" />
<br />
<input type="submit" value="Set Language"/>
</form>
Then the switcher needs to be changed so that it knows about temporary and long term cookies:
< ?php
if ($length == 'year') {
setcookie ('sitelang', $set, time()+31536000, '/', 'yoursplashdomain.com', '0');
}
else {
// this cookie just needs to survive long enough to get the user to the right place
// one time.
setcookie ('sitelang', $set, time()+60, '/', 'yoursplashdomain.com', '0');
}
header("Location: $HTTP_REFERER");
?>
That should do it.
You can try out a demo here.