PHP gettext caching workaround

Gettext is probably the most flexible, the most convenient, and the fastest way to handle I18N in PHP applications (and not only PHP of course).

However, when you have PHP installed as an Apache module (mod_php), PHP’s gettext extension translation files are cached, so updates are not instantly visible. Unfortunately, there are no configuration directives to control caching time and practically the only way to clear the cache is to restart the web server. Obviously it can be a serious issue, especially on shared hosts, where you rarely have control over the server.

Fortunately, there’s a little workaround, which makes the webserver restart needless.

Simply change your domain and translation files names every time you make the update.

Typical update procedure would be :

# configuration file
$currentDomain = 'domain2'; #changeable

# somewhere in your code
$domain = $config->getCurentDomain();

bindtextdomain($domain, '/path/to/locale');
textdomain($domain);

You can consider writing some shell script to automate this as it can be pretty tedious task to do it manually over and over again.

  1. by sobstel • November 2008