Search code examples
phpinternationalizationlocale

Localize existing PHP application


I have an existing, database driven PHP application with around 40 pages printing an unknown (but rather large) number of English strings. The strings are currently all hard coded. There is also a set of static documentation pages. I now need to add language support to this application.

As far as I can tell gettext seems to be the standard solution to this problem, but gettext feels very much like a "hack" to me. I am also not certain about the additional overhead (both in development and run-time) it will cause. Are there any other solutions or frameworks that could be better suited to my requirements? Any best practices or pitfalls I should be aware of when starting this project?


Solution

  • gettext is the "standard" for multi-lingual text storage, but in the end, it is just a storage engine. It does nothing to put the proper text into your page. You need to abstract the text from your View so the proper text can be inserted.

    In the end, you need a way to place the language text in your document. This means "tagging" the text in a certain way so that it can be searched and replaced by with desired language text. For the e-commerce site I manage (3 languages) I used a technique derived from Facebook FBML tagging system.

    You can wrap your text in "tags" <trans id="slt">something like this</trans>. Then use the DOM tools in PHP to extract the ID, look up the translated text based on the id, replace the content between the tags with the proper language text. You can still use gettext for your storage mechanism, or your database. Your browser will ignore the tags, so your page will still look fine during development.

    This is just one tagging example. You can use any tagging mechanism and use grep instead to extract, search and replace.

    For static pages, you can pre-generate the translated versions and load the appropriate language version. This way there is no extra overhead for different languages.