PHP gettext caching workaround

almost 2 years ago sobstel php gettext comments

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 :

  • Change names of .mo (and .po) files, e.g. from domain1 to domain2.
  • Update domain names in your code to domain2.
# configuration file
$currentDomain = 'domain2'; #changeable

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

bindtextdomain($domain, '/path/to/locale');
textdomain($domain);
  • Upload/commit changes.

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.

Comments

4 months ago Dolby #0

very helpful. thanks..
I still wanna do a test soon to prove this cross-config

New comment

  1. (never shown, just for us for possible contact)

  2. (with http or https prefix)

  3. (use [code][/code] tags to post a code snippet)

  4. (javascript is required)

Website content premeditately commited by segfaultlabs (malczak & sobstel).
Layout by mlando. Icons by DryIcons.com. All rights reserved, of course.