Setting up an International Multi-Language Site


This entry is part 1 of 1 in the series Language Switcher
Language Switcher Series Index
  • 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&iacute;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&aacute;gico," dijo Maria, "pero no negro. Es tan blanco como nieve."<br />
Siempre lo llamaron magia y se parec&iacute;a de hecho como ella en los meses que siguieron -los meses-radiantes de los meses-maravillosos que sorprend&iacute;an unos.';
            break;
        case 'fr':
            $title = 'Exerpt du Chapitre XXIII - Magie';
            $byline = 'Du Jardin Secret par Frances Hodgson Burnett';
            $content = '"M&ecirc;me si ce n\'est pas vraie magie," Colin a indiqu&eacute;, "nous pouvons la feindre est. Quelque chose est l&agrave;-quelque chose!"
"Elle est magique," a dit Mary, "mais pas le noir. Elle est aussi blanche que la neige."
Ils l\'ont toujours appel&eacute;e magie et en effet elle a sembl&eacute; comme elle en mois qui ont suivi-le des mois-le radiants de mois-le merveilleux stup&eacute;fiant ceux.';
            break;
        case 'pt':
            $title = 'Exerpt do Cap&iacute;tulo XXIII - M &aacute;gica';
            $byline = 'Do Jardim Secreto por Frances Hodgson Burnett';
            $content = '"Uniforme se n&atilde;o for m&aacute;gica real," Colin disse, "n&oacute;s podemos fingi-la somos. Algo &eacute; l&aacute;-algo!"
"&Eacute; m&aacute;gico," disse Mary, "mas n&atilde;o preto. &Eacute; t&atilde;o branco quanto a neve."
Chamaram-no sempre m&aacute;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&ouml;nnen es vort&auml;uschen sind. Etwas ist dort-etwas!"
"Es ist magisch," sagte Mary, "aber nicht Schwarzes. Es ist so wei&szlig; 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 &uuml;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&ntilde;ol</a></li>
    <li><a href="/fr/">Fran&ccedil;ais</a></li>
    <li><a href="/pt/">Portugu&ecirc;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.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
15 Creative Ways to Display Date Archives
This entry is part 1 of 1 in the series Language SwitcherLanguage Switcher Series IndexSetting up an International Multi-Language SiteDate based archives aren't necessarily the most important thing for your site but if it's another opportunity to present your information in a different (hopefully creative way) then why not do it? Let's examine 15 different
301 Redirects from your Wordpress 404 Error Page
This entry is part 1 of 1 in the series Language SwitcherLanguage Switcher Series IndexSetting up an International Multi-Language SiteGetting too many 404 requests because you've deleted a post, or moved it/changed it's name. Well there's an easy way to make your Wordpress 404 page redirect to whatever page you need it to!
.htaccess Redirect a Directory to a Subdomain and Force WWW
This entry is part 1 of 1 in the series Language SwitcherLanguage Switcher Series IndexSetting up an International Multi-Language SiteIt's really easy to force the www for your website URL and to redirect a directory properly to a new subdomain address. Just give it a shot, organize your directories!
Wordpress Loves Magazine/CMS Templates
This entry is part 1 of 1 in the series Language SwitcherLanguage Switcher Series IndexSetting up an International Multi-Language SiteTrying to find inspiration for a new project that won't be so much content-heavy as it will need to be easy to update. I'm thinking a magazine look and feel using Wordpress will be the
Update to MySpace Profile Tracker Pepper
This entry is part 1 of 1 in the series Language SwitcherLanguage Switcher Series IndexSetting up an International Multi-Language SiteMint pepper Myspace tracker gets my update. Now it actually works for me which is pretty awesome. I can track Myspace visitor's IP addresses and now with the link to my blog maybe I'll
Next Post
Making MySQL Update and Insert Easier
Previous Post
Little Hiatus

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

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!

Hi, I'm trying to install the script on my server but I always get an error message opening the index-file: Parse error: syntax error, unexpected ';' in /home/bbt4vw/public_html/testmultilanguage/index.php on line 7 Is it possible to attach the original files, so I can see what I'm doing wrong? Thanks in advance!