I have spent hours on Google researching internationalization with php and read about the various pear packages to do that. I came across things like gettext, Translation2, and i18n. Yet I rather want to hear what you guys have done to serve a website in different languages. I want to know what approach you used and how you did it. Please elaborate and share your experience and tips.
I i18n’d one site before. The easy/best way IMO is have a XML file with the messages for one culture, and then a function translates from english to the culture.
For long text like articles, use an ORM with i18n built in to it.
At work we use gettext extensively. We have 14 sites with different languages/ cultures (including German, Russian, French, Dutch, English, Swedish etc.).
We have a command line script that scours the project directories looking for ‘gettext’ calls and then it writes out the corresponding .po files, which we use to translate (we do half the languages in-house, the rest go to an external translation bureau).
The .po language files are then compiled to .mo files. Because the files are compiled, all calls to gettext are blindingly fast.
It’s a little difficult to set up, but I love gettext
I’ve used Zend Framework which should have i18n features. Also used Drupal and Magento (using CSV files), both of which has great support for multilingual content. Immerse’s solution seems quite cool and I see no reason to spend time reinventing the wheel. Except that I admit it would be fun.
We don’t use a CMS, the whole application is custom-coded (purely for performance purposes).
We each work in our own svn checkout, and add the new strings to the po files whenever we feel the strings for a certain piece of code/ functionality are complete. Then we do the first translations ourselves (i.e. we program in English, I translate to Dutch, my French colleague to French and our Russian colleague to Russian). The other languages are sent to our translation bureau (who we taught how to use poEdit to edit the po files).
Once the files come back, we generate the mo compiled files, deploy them on a test server and start testing to make sure everything still works.
The nice thing about gettext is that if a string isn’t translated, you simply get the English string back, i.e.:
if(User::doLogin($username, $password)) {
$data['response'] = gettext("You are now logged in.");
} else {
$data['response'] = gettext("You are not logged in.");
}
If we have no translated string for “You are now logged in.”, then we simply display the English version.
It gets more complicated when using numbers though (using ngettext()).
F’r instance, in English there are two possible strings you could want for displaying a row count:
There are %s rows.
and
There is 1 row.
But in Arabian, there are (I think) 6 different versions, luckily ngettext/ poEdit etc. can handle that quite well.
I used Translation2 from pear, which is a good and flexible package. It allows you to store translation strings in database or xml or in text files, it’s up to you.
Plus it has options to set default language, so if string is not available in specific language then it will be returned in default language.
After using it for awhile I ended up writing my own classes for translation which used the same ideas as Translation2 but did not have any features that I don’t need, so it works only with database backend and all decorators like default fallback language and fallback strings are hard coded. So basically my own class is faster because in only has features that I need and the features are hard coded into the class. I traded flexibility for speed.