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]
post a new message
29th Apr 2008
overheads?
So for each and every page you got you have to run a DB query searching for the text required based on the language 'key'?

Isn't that a heavy overhead for the site, imagine 1,000 people running it simutaniously, that's a lot of DB hits.

What about a properties file for each language, you hold the users desired langauge 'id' in the browser session and each time you load a page the site populates the page with labels from the file
4th Dec 2007
Re: So where exactly is the code t...
10th Apr 2008
Re: So where exactly is the code t...
We search Feeds For You
http://feedogator.com/
27th Mar 2008
Re: So where exactly is the code t...
Try NeedForNews.com, a new comprehensive news aggregator. With NeedForNews, you don ’t really have to go anywhere else; NeedForNews has it all.
needfornews.com
vidslib.com
24th Mar 2008
urls to the latest world news
urls to the latest world news. needfornews.com http://needfornews.com/top_search.php
17th Jan 2008
Translation for dynamic data
Great job a quite informative piece.
But there is something i'd like t ask about.
can this method translate dynamic data.
like registrants data on my website. or product cataloge data?
6th Feb 2005
So where exactly is the code t...
So where exactly is the code that will make me a billionaire? I need this multilingual enchancing code now!

http://archivenyc.zsxonline.com
4th Dec 2007
Re: So where exactly is the code t...
http://azzuplinks.com
Very useful information. Thanks !
6th Aug 2006
using mySQL and PHP to overwrite static pages
Just an idea of using PHP and mySQL to overwrite static pages?

Basically, you'd have few templates and use PHP to pull updated text from a DB into variables then insert into template those variables and write a new html file as an output?

Correct me if I am wrong but from what I have seen most CMSs such as reddot usually work on static pages and overwrite them whenever content updated. so content would be hold in DB but the output would be HTML files thereby speeding up the site and making it not dependent on DB (in case it goes down).

let me know your comments

David
ps. I am completely new to this stuff :)
10th Aug 2007
Re: using mySQL and PHP to overwrite static pages
msg
9th Apr 2007
Re: using mySQL and PHP to overwrite static pages
I do not use a CMS program allthough I did try MAMBO and deleted it as program was so huge and other problems, that it created more work then it saved me. Using this program I needed to change all my html pages to php pages, it is like starting from scratch rebuilding the site (not done yet with about 1,000 pages) In their directions I think near the bottom they mention putting "flags I think" in template, with the after thought "you do have a template don't you?" and of course I do not and have no way of knowing how that is accomplished. I did notice at least some of this sites pages seem to come up as html instead of php pages and I was never able to get php to work unless the page was php and guess it has to do with a template, it is to late now but any comment on how you accomplish html pages to work with php would be appreciatted not using CMS.
Thanks
Trabob
30th Oct 2006
A working site using this script
I installed the script to one of my sites at
http://www.trabob.com/translate.php
It seems to work good, and it seems to be attracting some viewers from other languages.
The biggest problem I have is some languages Greek, Russian, oriental, need special codes in mysqyl codes and I have not figured out which codes to use for them in mysqyl. Greek is one that I need to reset my browser view for each page and not sure if codes are wrong in mysqyl or if it is a problem in browser (internet explorer 6.0)

I set it up somewhat different were the only flag of language it is using shows on each page with a link back to main translate page to change it, as the flags take to long to load if all on every page. I plan on adding about one language each month (12 per year) but can't add languages that use something different then Latin unless I can find the correct codes for mysqyl.

Comments - suggestions appreciated
Thanks
Bob
9th Apr 2007
Re: A working site using this script
I have since found the way to get the browser to recognize the language code by inserting
# ">
and finnally figured out that in the configure file between "" in one of the spaces is were the charset goes, as there is no info to tell you that on that file, and the greek language appears to correct when in the greek language, at least with IE browser, in case anyone else runs into that problem. I haven't tried the russian language yet charset "ro18" or something like that as database didn't seem to like that code (scrambled it)
Trabob
9th Apr 2007
Re: A working site using this script
trying code again
< META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=" >
9th Apr 2007
database time
To those writting about long times extracting data from data base to view page, I have not had any delays in showing files and I have a lot of tables in data base. The only time delay is when menu loades in edit mode, and because I have so much in my database it can take upto about 2 minuets to load menu and database is growing, so to elimanate load time there I mearly set the menu side to open (there are 2 sides on main edit page, one listing all tables in menu and other the page section they click to edit-translate) in seperate window with a link to it in main edit page were the menu side use to be, and now when editors edit then translate page file the menu does not load at the edit page (loads in seconds) (when they refresh updates the edit page reloads allmost instantly (no menu refresh time), Also advantage is they do not need to access menu and I prefer not giving them access to it, as they can tell if page needs updated by reading the page they are on. As I want this program available for many people to edit their own pages (not just languages) so that it would work as a wysiwyg type editor for them no matter what language. I am still working on a solution to that so abc company can only edit their page(s). For now all I have done is disable the links to other pages when in edit mode so they do not go to other pages and could be a security issue with one bad apple and still working on that, any idears would be appreciated.
Trabob
www.trabob.com
28th Jan 2005
Very UNWISE using sql-database...
Very UNWISE using sql-database
Page generation time will 8 (=infinity)
12th Jan 2006
Re: Very UNWISE using sql-database...
yes, using a SQL database is probably not the best way of doing it. It's probably the easier if you want dynamic updates possible, but for a website with lots of visits, it wouldn't be recommended.
7th Apr 2007
Re: Very UNWISE using sql-database...
It's good to create a new language pages for the editor. And after each static pages are created by the editor using this method, visitors seeing the static pages already translated... that would be the best way to go... cuz if every time visitors need to see different language pages by accessing database for translating, each translated pages may have really long delay loading each page. At least that's what I think. ^^;
1 2 3 Next