Be kind, be multilingual

The problem: we want to have a page in several languages. Without having to create one html file for each page for each language.

The solution can be divided in three parts:

1. how to know in which language we must show the page,

2. how to render the “structure” of the pages and

3. how to get the content of the pages.

In this entry I’ll show how to code, in PHP, the fist part of the solution.

The code is simple. We want to change the language of the page at user’s request AND store the language settings in the client, so the next time our page is loaded it will automagically show the language selected by the user in his/her last visit.

// tipically you define the root of your globals in a confif file
// it's define here for completeness...
define('VAR_ROOT', 'MY_PAGE_');

// the language by default
$strDefLanguage = 'es';

// an array of allowed values for strLan
// HINT: whenever possible check any user input with a list
// 		of 'legal' values.
$arrLanguages = array('en', 'es', 'fr', 'de', 'ca');

  * this function returns the language for our application.
  * @param an string that, optionally, contains the language to switch to.
  * @returns the language in use right now for our app
function getLanguage($strLan = '') {

    if($strLan != '' && in_array($strLan, $arrLanguages)) {
        $_SESSION[VAR_ROOT . 'LANG'] = $strLan;
        setcookie(VAR_ROOT . 'LANG', $strLan, time() + 60 * 60 * 24 * 30);
    return (isset($_SESSION[VAR_ROOT . 'LANG']) ? $_SESSION[VAR_ROOT . 'LANG'] :
           (isset($_COOKIE[VAR_ROOT . 'LANG']) ? $_COOKIE[VAR_ROOT . 'LANG'] :

Note that this function goes against the principle of simple responsability. Sometimes we sacrifice this kind of things for speed and functionality. It’s not bad. Neither good.

Anyway, you’ll call this function on each of your pages, before starting to render the structure or to read the data from the database.

// you MUST start a session for this to work.

// your includes go here
require_once('someconfig.php');    // like this one...

// now you read the language
// we're setting the language to the lan parameter
// only if it exists.
// otherwise we just get the current language.
$strLang = getLanguage(isset($_GET['lan']) ? $_GET['lan'] : '');

// you can now read your literals file
// remember that strLang has a known, filtered, value
// so this is safe
require_once('lan' . $strLang . '.php');

// or instanciate your database class
$objNotes = new notesClass($strLang);

In next entries I’ll describe in more detail the other two parts.