SiteTranslator

SourceForge.net Logo

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

Introduction

SiteTranslator is a tool which allows you to quickly and easily build multilingual websites.All the text rendered on the site is stored in a database, translators are able to login to the site and translate blocks of text interactively. For a full tutorial which details how this set of classes were developed, see my article on the Internet Jugglign Database (which is avaiable in 9 different languages!) - Creating multilingual websites

SiteTranslator consists of the following classes. Rather than document the methods of each in detail, I will provide an overview of the responsibilities of each class and give a few examples of how they may be used.

SiteTranslatorConfig
A class which mostly consists of configuration information, such as translator and editor information and a list of languages supported.

SiteTranslator
Each page that contains multilingual content should create an instance of this class. The constructor starts a session so it should be instantiated before any HTML headers are sent. When the session is first registered this class will determine the most suitable language for the visitor, however they can select their preferred language via a variable passed through the HTTP GET method. The most frequently used method of this class is getLocalisedText() which displays the text associated with a key in the visitors desired language

LocalisedTextBase
An abstract base class which provides persistence. Classes derived from this can implement persistence using a number of different methods, for example a text file or mySQL database.

LocalisedTextBaseMySQL
A class which provides persistence using a mySQL database

SiteTranslatorAdmin
This class extends SiteTranslator to provide a number of useful functions for editors and translators.

Example

The following example shows how the SiteTranslator class is typically used.

<?
  //create an instance of SiteTranslator, the class keeps track of the user's language
  //preferences. Also it will handle language changes via the configurable GET variable
  require ("lib/SiteTranslator.php");
  $translator = new SiteTranslator();
?>
<html>
<head>
<?
  //Output the character set meta tag
  <meta http-equiv="content-type" content="text/html; charset=<? echo $translator->getCharSet(); ?>"?>
?>
<title>
<?
  //Output the localised title for this page, suppressing the translation
  //HTML tag, because the title cannot contain HTML!
  echo $translator-?>getLocalisedText("title", true);
?>
</title>
</head>
<body>
<?

  //if the visitor to the page is logged in as a translator, provide a link to the logout page
  if ($translator->isLoggedIn())
    echo '<a href="logout.php">Log out</a>';
    
  //output the different languages which this site is available in. The flags provide hyperlinks
  //which allow the visitor to select their preferred language.
  echo $translator->showFlags("", false, $_SERVER["PHP_SELF"]);
  
  //output the text for this page
  echo $translator->getLocalisedText("front_page_introduction");
  
  //output the navigation menu. Note the 'false' parameter passed to getLocalisedText. When
  //a translator browses this page, 'translate' links are appended to the localised text. We need 
  //to suppress these links otherwise the following code will created nested <a> tags
  echo '<a href="home.php">' .$translator->getLocalisedText("menu_home", false) .'</a>';
  echo '<a href="about.php">'.$translator->getLocalisedText("menu_about", false).'</a>';
  echo '<a href="faq.php">'  .$translator->getLocalisedText("menu_faq", false)  .'</a>';
?>
</body>
</html>

Although the page illustrated above is a little simple, it does show the key functionality of the SiteTranslator class. If you use this system within your site, I would strongly recommend that you place most of the above code, e.g. the SiteTranslator instantiation, the character set output, flag output etc... into your website template. You are using a template aren't you?

Editors and Translators

There are two different types of 'editors':

Translators
When logged in, these users see a small 'translate' link next to every piece of translatable text. When they click on it they are able to translate this text into their own language (or languages).

Editors
These users can perform all the functions of translators. They are also able to add new text keys and descriptions or delete existing ones. Furthermore, when an editor makes changes to existing text they have the option to mark it as requiring an update, so that next time a translator logs in to the system, they know that some text which they have previously translated, requires updating.

The framework used by the editors and translators is found in the 'translation' directory. These files use the SiteTranslatorAdmin class to edit, modify and update the text in the database. The following image shows the interface they use to edit the text on the website:

Installation

Full installation instructions are found in the README.txt file within the download package.

And finally ...

If you do use SiteTranslator within your website - please let me know.

Colin E. (webmaster@jugglingdb.com)