Monday, June 13, 2011

Internationalization in PHP

Our current game plan with First EMT is to build a Javascript and PHP web application so I've been looking at ways to do internationalization in PHP. If you're writing code that needs to be accessible in many different languages, internationalization is a must. Instead of hard coding text you create a variable for every body of text in your interface. Then you store translation files for every language your UI must support. By changing your locale variable you can quickly and easily switch the language your UI displays. If you make the translation files human readable it is also very easy to add new languages by simply hiring a professional translator for a few hours of work.

My first searches seemed fruitful... I found this documentation describing Resource Bundles. It seemed great until I noticed it was under the header "Future Directions". I'm a fairly proficient programmer but I'm certainly not ready to write a patch to PHP... time to look at other options...

The best option I've found so far is called Zend_Translate. Zend's translate framework is really flexible. You can provide something as basic as arrays of words to translate or as complex as importing a Translation Memory file and dynamically translating everything. For my purposes I think I'm going to use the TMX adapter. TMX is an extension of XML and it's an industry standard for this sort of application. It can be modified in any XML editor which makes it very easy to add new translations. An example TMX file would be something like:

  1. <?xml version="1.0" ?>
  2. <!DOCTYPE tmx SYSTEM "tmx14.dtd">
  3. <tmx version="1.4">
  4. <header creationtoolversion="1.0.0" datatype="winres"segtype="sentence"
  5.         adminlang="en-us" srclang="de-at" o-tmf="abc"
  6.         creationtool="XYZTool" >
  7. </header>
  8. <body>
  9. <tu tuid='message1'>
  10. <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
  11. <tuv xml:lang="en"><seg>message1</seg></tuv>
  12. </tu>
  13. <tu tuid='message2'>
  14. <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
  15. <tuv xml:lang="en"><seg>message2</seg></tuv>
  16. </tu>
  17. </body>
  18. </tmx>

This example is straight from the Zend Framework website.

No comments:

Post a Comment