Tutorial :How to build PHP application with multiple language support (English, French, Chinese etc..)


I'm building a web application that uses LAMP and the Smarty template framework. The website will have plenty of static content (about us page, error alerts, confirmation emails etc...).

The web application must support multiple languages. Is the following approach appropriate?

1) All static copy on Smarty template pages (html pages) will be replaced by calls to smarty plugins such as {lang file='about.xml' getTerm='title'} which returns a string for the title tag in the appropriate language

2) The file about.xml is an xml document on the webserver that acts as a multiple language dictionary. So it will have xml nodes such as

<term value="title">     <en>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; is a great site</en>     <fr>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; c'est website magnifique</fr>     <ch>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; complex looking chinese characters I don't udnerstand</ch>  </term>  <term value="signupmessage">     <en>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; great deals!</en>     <fr>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; magnifique deals!</fr>     <ch>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; complex looking chinese characters I don't udnerstand</ch>  </term>  

3) The url structure for the site will be http://mywebsite.com/language/index.php. To do this, my webserver will have symbolic links such as ln -s ./en ./ so that http://mywebsite.com/en/index.php points to the same php page as http://mywebsite.com/fr/index.php, but I will check the url for language settings. My hope is that google will index both versions of the site.

Is this a good approach? Is xml the best format for storing content of different languages even though it has htmlentity encoded html tags? Did I overlook anything?


Not sure about the details of your question but you may want to take a look at Zend_Translate.


XML is a container format and this use of it isn't too bad. It has the advantage of being relatively human readable except for the HTML entities. You could try using CDATA sections for that. One advantage of using XML for this is that you could use a DTD to validate that all languages have translations present for all keys. Make sure you encode the file using UTF-8 or UTF-16 and it probably helps if you put the encoding at the top of the file. I'd recommend UTF-8.

As for how you load the XML data and incorporate it into the page, I don't know "smarty" but the overall approach sounds reasonable. The only downside I can see is that you have to release all the translations simultaneously (difficult to avoid this problem).

One thing I note is that you use ch as the language code for Chinese. You should use zh. That is the ISO code for it. ch is something else.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »