1. Presentation

I've been looking for the best way to have my WebSite support multiple languages. Most of my pages are straight standard HTML, but I also use PHP for dynamic content or computation. I found several solutions bases on PHP :

  • 1. Get the texts dynamically from a MySQL Database
  • 2. Include a specific Language File depending on the Language, containing the translated text
  • 3. Make a copy of every page and translate them

It seemed that solution 1 was overkill for the Database. At least that is what people said. Also, having a specific piece of code in each page to retrieve the texts did not appeal to me. Solution 2 was pretty good, but I had to convert all my HTML files to PHP, and include a parameter to switch between the languages. Finally, solution 3 was mentionned for 'small websites', but I wouldn't recommend it even for the most tiny of sites :)

2. Concept

We need to realize that often, we use PHP files for content that could be provided easily using standard HTML pages. The database requests will always provide the same results, giving an identical content for the page. Having your web server work only to provide again and again the exact same page seems silly enough. So we'll try to make the best of solution 2 with an automated process that will save us the work of going with solution 3.

Site Translate Diagram

On the diagram above, you can see how the application is designed. You have one set of pages (Your Website) that we will call "Raw HTML" files. These files are not language dependant, and will contain no text : They will only have references to translations. On the other side, you will have one XML file (or anything that suits your needs) with all the references translated for each language that you wish to support.

Once your page and translation are ready, a simple program (Here called 'Site Translate') will create a set of pages for each language. This way, your website will not have any overhead by supporting multi-languages, besides the time required for the translation of course :)

3. Prepare your site

I could end up this article right here and leave the rest as homework for the reader, but since I have implemented this process for the site you are currently viewing, I can at least publish what I have done :) Here's how I did it.

Sample Raw HTML file :

Sample XML file :

As you can see in the sample file, I used the following syntax for my tags : "{@Tag@}". This is to ensure the tool will not replace parts of the page that are not intended to be. This is all that needs to be changed in your HTML file. The XML file is a bit more complex, but not by much. We have a section to describe which languages we support ( fr and such ), and another section containing the translations for each tags, in each language. If a tag does not have a translation for a specific language, then is used.

4. Deploy

This is definitely the easiest part. Using the Site Translate tool (.NET has to be installed on your computer), you simply need to identify your raw HTML files. For each one, the tool will look for the corresponding XML file and start creating the final pages for each language. I used the following convention :

  • - MyFile.raw.html
  • - MyFile.xml
  • Resulting in
  • - MyFile.html ( For the DEFAULT language )
  • - MyFile_fr.html ( FR Language )
  • - MyFile_XX.html ( XX Language )
  • - ...

5. More...

Things you need to be aware of :

  • - The XML parser used is very sensitive. Your XML file needs to be perfect.
  • - I use UTF-8 for the XML file, and I use it as the content-type of my HTML files. Be sure that you save your file in UTF-8 format and not in iso-8859-1. Any decent text editor such as Ultra Edit will do the trick.
  • - Several characters are not allowed in XML. Do not forget to escape them.
  • - You can use this technique for PHP files as well. It's not a bad thing to remove the processing required by the translation from your web server.

If you wonder if the tool is reliable, well this whole website has been generated with it. Have fun !