Setting up an International Multi-Language Site
- Setting up an International Multi-Language Site
We were tossing around an idea at work one time about proposing an integration of a potential client's international site with their US version of the site.
While they were thinking based on ideas I got distracted by the idea of how I would build such a CMS, and how we would make it work seamlessly. So I made a quick demo one night.
I used five languages:
- English
- Spanish (Español)
- French (Français)
- Portuguese (Português)
- German (Deutsch)
I assumed that this would be for a big company who would actually pay someone to do the translations rather than use an online computer translation service.
Before we get to database integration for this I'll use a simple switch statement on a demo page.
First I grabbed an excerpt from one of my favorite stories "The Secret Garden" by Frances Hodgson Burnett from Authorama - Public Domain Books and made my English version of the script:
PHP
<?php
$title = 'Exerpt from Chapter XXIII - Magic';
$byline = 'From The Secret Garden By Frances Hodgson Burnett';
$content = '"Even if it isn’t real Magic," Colin said, "we can pretend it is. Something is there–something!"
"It’s Magic," said Mary, "but not black. It\'s as white as snow."
They always called it Magic and indeed it seemed like it in the months that followed–the wonderful months–the radiant months–the amazing ones.';
?>
I then translated the variables values into the other four languages and added them all to a switch statement, using their language abbreviations as the case.
- English =
en - Spanish =
es - French =
fr - Portuguese =
pt - German =
de
PHP
<?php
switch($lang){
case 'en':
$title = 'Exerpt from Chapter XXIII - Magic';
$byline = 'From The Secret Garden By Frances Hodgson Burnett';
$content = '"Even if it isn’t real Magic," Colin said, "we can pretend it is. Something is there–something!"
"It’s Magic," said Mary, "but not black. It\'s as white as snow."
They always called it Magic and indeed it seemed like it in the months that followed–the wonderful months–the radiant months–the amazing ones.';
break;
case 'es':
$title = 'Exerpt del Capítulo XXIII - Magia';
$byline = 'Del Jardín Secreto de Frances Hodgson Burnett';
$content = '"Uniforme si no es magia verdadera," Colin dijo, "podemos fingirla somos. Algo es alli\'-algo!"<br />
"Es mágico," dijo Maria, "pero no negro. Es tan blanco como nieve."<br />
Siempre lo llamaron magia y se parecía de hecho como ella en los meses que siguieron -los meses-radiantes de los meses-maravillosos que sorprendían unos.';
break;
case 'fr':
$title = 'Exerpt du Chapitre XXIII - Magie';
$byline = 'Du Jardin Secret par Frances Hodgson Burnett';
$content = '"Même si ce n\'est pas vraie magie," Colin a indiqué, "nous pouvons la feindre est. Quelque chose est là-quelque chose!"
"Elle est magique," a dit Mary, "mais pas le noir. Elle est aussi blanche que la neige."
Ils l\'ont toujours appelée magie et en effet elle a semblé comme elle en mois qui ont suivi-le des mois-le radiants de mois-le merveilleux stupéfiant ceux.';
break;
case 'pt':
$title = 'Exerpt do Capítulo XXIII - M ágica';
$byline = 'Do Jardim Secreto por Frances Hodgson Burnett';
$content = '"Uniforme se não for mágica real," Colin disse, "nós podemos fingi-la somos. Algo é lá-algo!"
"É mágico," disse Mary, "mas não preto. É tão branco quanto a neve."
Chamaram-no sempre mágica e certamente pareceu como ela nos meses que seguiu-os meses-radiantes de meses-maravilhosos que espantam.';
break;
case 'de':
$title = 'Exerpt von Kapitel XXIII - Magie';
$byline = 'Vom Geheimen Garten durch Frances Hodgson Burnett';
$content = '"Selbst wenn es nicht reale Magie ist," sagte Colin, "wir können es vortäuschen sind. Etwas ist dort-etwas!"
"Es ist magisch," sagte Mary, "aber nicht Schwarzes. Es ist so weiß wie Schnee."
Sie nannten es immer Magie und in der Tat schien sie wie es in den Monaten, die wundervolle Monate-d den leuchtenden Monaten-d eine überraschend folgten-d.';
break;
}
?>
I developed a simple HTML page to display this data using a forced GET request, or appending the query string ?lang=en or ?lang=de to the end of the url to make the decision of what language to display.
HTML & PHP
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Switching Languages</title>
<?php
$lang = (!isset($_GET['lang']))?$_GET['lang'] : 'en';
# [...] insert above switch statement here[...]
$content = nl2br($content);
echo <<<ENDLINE
<div class="content">
<h2>$title</h2>
<h3>$byline</h3>
<p>$content</p>
ENDLINE;
?>
</div>
</body>
</html>
In the ternary operator $lang = (!isset($_GET['lang']))?$_GET['lang'] : 'en'; you can see I set the default language to English, which is helpful since I am not using a default case in the switch statement.
I thought the best idea would be to use the Apache module mod_rewrite to "fake" directories.
A URL like /fr/ would rewrite to index.php?lang=fr or a URL like /es/ would rewrite to index.php?lang=es
This is accomplished in your .htaccess file as simply as:
.htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(de|en|es|fr|pt)/$ index.php?lang=$1 [nc]
Now add links to your index.php page and you can quickly and easily toggle between the languages:
HTML
<ul>
<li><a href="/en/">English</a></li>
<li><a href="/es/">Español</a></li>
<li><a href="/fr/">Français</a></li>
<li><a href="/pt/">Português</a></li>
<li><a href="/de/">Deutsch</a></li>
</ul>
This all assumes you are in the root of your domain. If you are in a directory you'll have to change you .htaccess file to say RewriteBase /directory/name/ and add /directory/name/ before all the links in your navigation to toggle between languages.
There's lot of other things you can do to spice this up a little bit and we'll go over that in another entry another day including setting cookies to languages are remembered! For now work with this and see what it can take you.
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)



Hey man, Thanks so much! I have been only searching for mod_rewrite in hopes of writing the same code you already wrote for this page! Awesome! I have a bi-lingual site. To bring it a step up, I thought to use WP. But WP was to bloated and slow, the multilingual capabilities were rather lacking and flawed. Nothing better than designing my own site - lightweight and lightning fast!