Last year, I wrote a post about using gettext to localize PHP web pages. Gettext makes it easy to maintain the translations and always provides a fallback locale. But is it fast?
I created a simple web page to compare the performance of various localization methods for PHP. It only contains 3 localized strings and does not use advanced features of gettext (e.g. plurals). I wrote a version using the gettext PHP extension (“gettext Ext.”), one using PHP-gettext (“gettext PHP”, a gettext implementation written in pure PHP) and a version that does not use gettext at all, instead it uses an array that contains all the translations (“String ID”).
I put all three pages on a Debian machine with the latest Apache 2.0.55 and PHP 5.1.2 and used the Apache HTTP server benchmarking tool to measure the performance of the different methods. I always made two tests – using the default locale (English) and a translation (German), because gettext does not have to use a locale file for the default language (it’s embedded in the page).
Here are the results (requests per second, more is better, I used “ab -n 5000 URL”):
As you can see, the version using the PHP gettext extension is the fastest solution. It is only marginally slower when using a language file, because the extension caches the translations (the downside is that you have to restart the webserver when you change a locale file). The String ID version is equally fast for either locale, because it always has to lookup the text in the locale array. The pure PHP gettext implementation is the slowest solution, and even slower when it needs to use a locale file. This is understandable because it always has to read the whole file for every request.
You can download the test files if you’re interested.
The gettext extension
When using the gettext extension on Linux, make sure all used locales are installed on the system. For example, in Debian (and probably other distributions, too), add the required locales to /etc/locale.gen and run locale-gen. For this test, I added “de_DE.UTF-8 UTF-8″ for the German UTF-8 version.
The native gettext extension for PHP performed best in the benchmarks. Using the gettext Extension allows you to create clean code by only wrapping strings in _() and including a file that loads the localizations. As I already wrote in my previous post, gettext also allows painless updates of the localizations by automatically finding new or changed strings. The only downside is that the webserver must be restarted to load the new language files. This can be a problem for users on shared hosts. In these cases, a pure PHP implementation can be used (which is not very fast though).
If you can restart the webserver, I recommend using gettext. It is very useful especially for larger projects or projects where not all translations can be updated simultaneously, because it always provides a fallback language. It also makes the source code much easier to read, because the default texts are directly in the source code.