The Internet Juggling Database


endeesjafrcaitherudanlel

Creating multilingual websites

Colin E. - 4th November, 2004.
[Page 1] [Page 2]

This article describes the development and usage of an Object Oriented solution for dynamic, multilingual websites, SiteTranslator. If you want to cut the waffle you can download it now:

SourceForge.net Logo

An Object Oriented solution for dynamic, multilingual websites.The latest version of SiteTranslator can be downloaded form the SourceForge project page.

Preface

During the past couple of years I have spent countless hours developing this site (the Internet Juggling Database). In this time I have learnt a lot about PHP, HTML, Java, mySQL and whole host of other technologies. Some of the code behind this site is good and some is bad. I have decided to share my learning experience by publishing some of the code and techniques I have learnt and developed. Although I hasten to add, I will only be sharing the good bits!

This article describes the technique I used to create a dynamic multilingual website. If you find it useful, please let me know. It will encourage me to keep on sharing.

Introduction

While it is true that the English language is the most commonly used on the internet, I was personally surprised that is was not more dominant. The English language accounts for 38.3% of internet use, i.e. 38.3% of internet users browse sites written in English. For a full break-down of internet language use have a look at the statistics found on the Global Reach site.

When I first developed the Internet Juggling Database (IJDb) my aim was to create a community site for anyone who had an interest in juggling. The choice of language was pretty obvious, I am British, and like most Brits I speak English ... only English. So the when the IJDb first went online the content was written entirely in English.

The dynamic nature of the site ensured that after being online for just a year, it was getting quite large. I put a little comment in the online 'to do' list saying that it would be nice if the site could be translated into other languages, although I wasn't really expecting anyone to volunteer for such a mammoth task. However, sure enough a kind soul stepped forward and volunteered to translate the site into German.

Now that I had a volunteer I just had to find a way to enable the translations to commence. Not an easy problem.

The problem

There are numerous multilingual websites on the internet, most large multi-national companies have websites available in a few different languages. However, the most of these sites provide links for different languages, following a link takes you to a different document for each language. I found this technique to be quite undesirable due to the problem of maintaining these static documents, a time consuming process with the developers having to synchronise separate copies of the website. Furthermore, considering the dynamic nature of my website, I was loathe to create a static copy just for the purposes of translation. What I needed was a dynamic solution.

Multilingual software is certainly not a new thing, and existing techniques used for offline software can be used online. A brief overview of the possibilities can be found in this article on Native Language Support. However, I did't find a solution which had all the features I required, as did the author of this article. In summary what I wanted was:

  • To be able to display blocks of text in the language most preferable to the visitor.
  • If their language preference could tot be fulfilled, the text would be displayed in English. (Many people prefer to read in their own language, but are still able to make some sense of English - again, apart from the British)
  • That the dynamic nature of the site would not be restricted in any way.
  • That translators would be able to work interactively, clicking on blocks of text which they wish to translate.
  • The newly translated text would be instantly available.

The only way I was going to meet these needs was to create my write my own system.

The Solution

Most of the text displayed on any website resides within HTML, although it may sit next to dynamic PHP code. The first step in my solution was to remove the static text and place it in a table within the database, with each block of text being identified by a short text key, for example 'frontpage_introduction'. All that is left to do is add a little bit of PHP code to retrieve the text that this key refers to:

<?
  //displays the text for the key 'frontpage_introduction'  
  echo GetText('frontpage_introduction');
  
  function GetText($key)
  {
    //NOTE: code to connect to the database has been omitted      
    $strQuery = "SELECT Description FROM EnglishText WHERE Key='$key'";
    $myrow = mysql_fetch_array(mysql_query($strQuery));
    return $myrow["description"];
  }
?>

With the text now stored in the database it is now possible to start serving localised text. In the above example the English text is stored in a table 'EnglishText', a second table can be created, 'GermanText' with the same structure, i.e. short text keys associated with the larger text descriptions. The above code then needs to be modified to query the text-table for the visitors language preference. The above example is modified as follows:

<?
  //displays the text for the key 'frontpage_introduction'  
  echo GetText('frontpage_introduction', $language);
  
  function GetText($key,$language)
  {
    //NOTE: code to connect to the database has been omitted
    //provide the German version of this text
    if ($language == "de")
    {
      $strQuery = "SELECT Description FROM GermanText WHERE Key='$key'";
      $result = mysql_query($strQuery);
    	
      //determine whether this text key exists in the German table
      if (mysql_num_rows($result)!=0) 
      {    	
        $myrow = mysql_fetch_array($result);
        return $myrow["description"];
      }		
    }
	
    //provide the English version of this text
    $strQuery = "SELECT Description FROM EnglishText WHERE Key='$key'";
    $myrow = mysql_fetch_array(mysql_query($strQuery));
    return $myrow["description"];		
  }
?>

In the above example, if the German version of the text is preferable, the German table is queried first. If the text is not present in the German table, the default English text is returned. This will allow the German translator of the site to translate the text gradually, with visitors browsing in German still being able to see the entire site, i.e. they are not restricted to the German content.

Of course the above example is a little simplistic, the visitor language should really be stored as a session variable and determined dynamically (this can be done by inspecting the $HTTP_ACCEPT_LANGUAGE variable as described in this article, although the illustrated approach is a little lacking in that it does not deal with multiple language preferences). Also, with a little bit of imagination the above example could be developed further to query a number of databases with support for a whole range of languages.

One of my main requirements was to allow translators to log in to the site and edit the text 'live'. I acheived this by making an addition to the above GetText function which detected whether the visitor was logged in as an editor, if so, a litte 'translate' link would be output next to the block of text. This links lead them to a special translation page where they can input the translated text in their own language.

With this dynamic method of translating my website implemented I am happy to say that numerous people volunteered to translate the site into different languages. My website is now available in 10 different languages, with another 4 under development!

A few years after developing this translation system I found that on occasions people would ask how it worked and whether they could use the system on their own websites. This encouraged me to totally re-write the system using Object Oriented techniques allowing for a more modular and flexible system which should be easy to 'plug-in' to any website. Rather than expand the above examples into a fully functioning translation system, the next page of this article describes my final solution, SiteTranslator.


[Page 1] [Page 2]