How To Translate PHP Apps Like A Boss

Translating PHP applications can be tricky. This post reviews the available solutions. Making your application available in various languages is an important step for growing your user base and improving the overall user experience.

Translating PHP apps

 

Despite alternative web programming languages like Ruby (on Rails), Python or NodeJS dominating the industry headlines, PHP remains a popular choice for building web applications.

Despite this fact there is still no real standard solution for localizing PHP applications.

Building a truly international application is not just about translating strings. Other issues to consider are date and time formats, currency symbols and pluralization. Programmers often underestimate the complexity of localization and get stuck with homemade code that is a pain to maintain.

So let’s talk about:

1. PHP Arrays

Associative Arrays have been the primitive approach of many big and small PHP projects for a long time. Translatable strings are stored in a associative array, one file per language.

The appropriate language file is loaded at the beginning of your application code:

This approach is simple and easy to use, but it quickly reaches its limits. Consider an application that displays a notification when the user uploads files:

What if it was only one file? Maybe do this:

Nah! That’s just plain ugly. I could come up with a hack like:

Not only does this clutter up the code, it doesn’t really solve the problem. In the english language appending a ‘s’ is enough, but pluralization works differently in other languages. For example, the pluralization of the world “file” (plik) in the Polish language works like this:

2. Gettext

The GNU gettext system has been around for more than 20 years. It is widely used and is the de-facto standard for localization in many programming languages.

Using gettext with PHP can be tricky in some setups.

If you are running a stock Ubuntu VPS, gettext will only support the locales installed on the machine. Or perhaps you are on a hosting plan where the gettext extension isn’t available.

In both cases, php-gettext can help. php-gettext it is a drop-in replacement for PHP enviroments where the gettext extension isn’t installed.

Basic Setup

In this example, i want to use English and German. I create the following directory structure:

Translations are stored in .po files, a simple plain-text file format. Using just a text editor I create en/LC_MESSAGES/messages.po:

I also create a messages.po for the de_DE locale:

In the next step, .po files are compiled to .mo files. This can be done using the msgfmt command line utility:

This is done for each .po file.

Usage

I can now use the .mo files via gettext in my PHP app:

This script creates a new gettext enviroment using the de_DE locale. Then the message with the id ‘hello’ is echo’ed which will output the german “Hallo”.

The gettext system supports plural forms. Using the example from above, I create a plural msgid:

which I can use in my app code:

gettext can handle pluralization but it has no tools for working with numbers, currency, date/time formats.

3. Frameworks

All major PHP frameworks have built-in support for creating translations. Some offer additional features such as classes for currency and date/time formatting.

Language filesPluralsDate/TimeCurrency
SymfonyYAML, XLIFF, PHP Arrays
F3PHP Arrays, INI
CodeIgniterPHP Arrays
KohanaPHP Arrays
CakePHPgettext
ZendPHP Arrays, CSV, TBX/TMX, gettext, Qt, XLIFF, INI, …

The localization modules of these frameworks can be used as a standalone tool without a lot of overhead code from the framework itself.

4. Intl

PHP 5.3 introduces the Intl class. Intl is a set of convenient helpers for formatting dates, time, numbers and working with currency. It can be used to complement gettext or frameworks that lack some of the functionality.

Conclusion

Localization can sometimes seem hard. Fortunately you don’t have to try to solve the problems with homemade code.

Unfortunately gettext doesn’t play very smoothly with PHP so for this I’d recommend using a framework.

Symfony, Zend and F3 all do a great job and are easy to use. After playing around with all frameworks, i really like the F3 approach. I have written a step-by-step guideon getting started with F3.

Looking for more information on how you can localize your software, app or website? Download your copy of our free eBook The Complete Guide to Growing Your Business with Localization here and receive tips and best practices for preparing your content for a global audience.


Also published on Medium.

Comments