This article describes the general design elements of a multi-lingual web site. The site will be very simple, but all the important parts of the design will be present. Here are two images that show what the site looks like. By clicking on the flags at the bottom, our clients can change the languages.
The Design of the Directory Structure
We want to be able to support many languages, but with a single, simple design. A directory structure that makes sense for this is in place at www.php.net. The PHP folks have organized the online manual this way, where XX indicates the language.
php.net/manual/XX/func
The standard terminology for languages is prescribed by ISO639, an evolving standard that employs short abbreviation codes. See http://en.wikipedia.org/wi
Our directory structure will look like this.
www_root
|
-- image/
-- index.php
-- language.php
-- template.php
|
-- en/index.php
|
-- fr/index.php
|
-- de/index.php
This structure will be implemented by using relative links for the web scripts in the language directories and explicit URLs for the image directory. If we wanted to add a fourth language, for example Spanish (/es/), we would be able to copy any of the language directories and translate the text into Spanish.
What Language Should We Use?
We might want to consider using an IP-to-Location service to choose the language, but in practice this is problematical. Consider the French who travel in Germany. Even if they were accessing the web site from a German city, it would not be reasonable to assume that the French now wanted to use German language. So instead of using geolocation to make decisions, we will give the client a highly recognizable visual clue, the national flags. We will make these flag icons into links that activate a common script. The common script will recognize the language, set a cookie and redirect to the appropriate language directory. We can find images of the national flags here: http://en.wikipedia.org/wi
The Home Page in the WWW Root
Our home page will try to recognize the client's preferred language by looking for a long-lived cookie. If the cookie is not set or is invalid, we will choose English. We will redirect the browser to the appropriate directory.
The Language Page
In order to give our client the ability to change languages, we need a language selection page. Instead of using the value in $_COOKIE, it will use the URL argument in $_GET. It is no accident that this script looks very much like the home page of the web root.
Handling Common Elements
Our image files are common to all language versions, therefore we will not use relative URLs to access our images. Instead, we will use a leading-slash notation to make reference to files that are relative to the web root, something like this:
<img src="/image/piglet.png" />
Similarly, our template script will be the same for all languages, so we will set up the template as a common script that can be included in each version of the web page. Near the bottom of this script you will see where we visualize the cookie. When we are able to see data like this, it makes debugging much easier.
Different Content for Different Languages
The use of "heredoc" notation makes PHP templating into a very simple task. We simply assign values to the variables that are embedded in the heredoc blocks. Learn more about heredoc and nowdoc on the PHP web site at http://php.net/manual/en/l
Here are our three different languages.
From This Design...
We could certainly do many more interesting things. For example, the variables that make up our pages, $lang, $head, $text could be much more extensive (In a real-world example, they would probably come from a data base.) If we wanted to add another language, it would be fairly easy. And most importantly, the new translation could be worked on without disrupting any of the existing translations. Once our new translation is ready for publication, we would install a new flag icon, and we would make a simple change to the template script to add the new element to the $languages array.
The combination of a common template and separate language directories makes this a flexible design with an intuitive client interface.
See it in Action
http://www.laprbass.com/RA