Workaround for gettext caching issue in PHP

gettext is the GNU internationalization and localization (i18n) library, which can be used in almost any programming language. And, of course, it can be used under PHP too, to internationalize your web applications.

I will not go into gettext usage details, there are enough resources available for that.

Official PHP gettext documentation:
http://www.php.net/manual/en/book.gettext.php
A nice “gettext introduction” and well-done article:
http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/
Some benchmarks:
http://mel.melaxis.com/devblog/2006/04/10/benchmarking-php-localization-is-gettext-fast-enough/

And now let’s get back to what I actually wanted to talk about. It’s a well known problem that new or updated translated strings don’t appear immediately on your PHP page. Or to say the truth – they don’t come up at all. This happens only if you are running PHP as a module (mod_php, …) inside your webserver (Apache, etc), it does not affect php scripts run as CGI.

Why does it happen? The short answer is – because of caching.
The first time you are initializing a text domain (loading a compiled binary .MO file), the file is loaded and cached in memory. Performance-wise, this is a very good thing, the translation string lookups run very fast and not needing to check the filesystem again and again helps a lot with the speed.
The disadvantage is that if you update the .MO file or even delete it, gettext will not catch the change, it would still run with the file from the cache. Very irritating, especially in a development environment.

The recommended and most well-known solution is to restart the web server – this will restart the PHP module with its gettext extension and the cache will be cleared. Unfortunately restarting the web server is not always something that can be done easily, or we might not even have the privileges to do it if we’re on a shared hosting.

I came up with a workaround, that does not require fiddling at all with the web server, it’s all done through PHP. Basically, what we do, is make sure that we are always using the last updated .MO file by using copies of it. We check for modifications by checking the file modification time using filemtime and then we make sure that’s the one we’re using by binding to a unique text domain and a filename computed from the file modification time. If that file exists (from a previous run), then that’s all. But if it doesn’t exists, then create it by copying the original .MO file. So, what we are actually doing is activating a new unique translation domain for each and every time the .MO file is modified.

What you need to consider:

  • you need write permissions in the locale folder
  • if you update the .MO file many time, then a lot of garbage temporary .MO files are generated – you need to remember to clear the folder once a month or something like that
  • is meant to be used in a development environment only, the filesystem checks are not benchmarked (even though PHP filestat caching may help here) and may be too expensive on a production server.

Finally, the example code is:

// settings you may want to change
$locale = "en_US";  // the locale you want
$locales_root = "locales";  // locales directory
$domain = "default"; // the domain you're using, this is the .PO/.MO file name without the extension

// activate the locale setting
setlocale(LC_ALL, $locale);
setlocale(LC_TIME, $locale);
putenv("LANG=$locale");
// path to the .MO file that we should monitor
$filename = "$locales_root/$locale/LC_MESSAGES/$domain.mo";
$mtime = filemtime($filename); // check its modification time
// our new unique .MO file
$filename_new = "$locales_root/$locale/LC_MESSAGES/{$domain}_{$mtime}.mo"; 

if (!file_exists($filename_new)) {  // check if we have created it before
      // if not, create it now, by copying the original
      copy($filename,$filename_new);
}
// compute the new domain name
$domain_new = "{$domain}_{$mtime}";
// bind it
bindtextdomain($domain_new,$locales_root);
// then activate it
textdomain($domain_new);
// all done

Comments (6)

Postfix + Dovecot

I have just moved to a new server last week, so I started fresh and decided to go ahead with a new mail server configuration.

On the old server I was using qmail+vpopmail+courier.
qmail – smtp, pop3 server
vpopmail – provides mailbox & virtual domain management
courier – for the imap server part

I got tired of all the patching mess in qmail. The only way to continue using qmail was to copy the already patched version, because I couldn’t recall all the patches and hand changes I have done over time. Yeah, I even modified its source code myself to fix some small things, that’s the best part of it – qmail is composed of very very small programs and have nice clean source code, easy to understand and follow. But you get tired after a while and you want something that can do more and just work. So, here I am now using postfix+dovecot:
postfix – smtp server
dovecot – pop3+imap server, mailbox & virtual domain management

I recreated all the emails and domains in dovecot, copied the Maildir directories, set the old server to forward all emails to the new one just in case someone still sent email to the old MX entry, and that was all. 0 second downtime and nobody noticed a thing.

I had a great howto/guide for installing Postfix+Dovecot:
http://johnny.chadda.se/2007/04/15/mail-server-howto-postfix-and-dovecot-with-mysql-and-tlsssl-postgrey-and-dspam/

One thing to check if using that howto as a base, a friend that has an account and uses Outlook Express couldn’t send mail at all. I then figured out that OE would not want to authenticate at all with the smtp server, I have then found out that OE will not login if the only auth method advertised by the smtp server is “PLAIN”, even though everything is under TLS. After testing different auth methods, it seemed to be ok if “LOGIN” it’s on. So, just edit doveconf.conf and make sure you have both PLAIN and LOGIN activated:

doveconf.conf :

...
auth default {
  mechanisms = plain login
  ...
  }
...

That’s all for now.

Comments

Notebook Design

I don’t like all these new and fancy models.

Why are all the cables on left/right side, it must be really cute having cables going out of the laptop on the left on your desk, what a mess.
Why are the audio jacks in front, now really, why would you want to have cables in front of your laptop.
Who killed the pointing stick? I loved it on my old IBM ThinkPad, it was so precise. But no, no, no, the touchpad is the way to go. And if you somehow really want a pointing stick… they have it hidden away in one of the “premium” models.

Where is the world heading to…
at least my Dell Inspiron 6400 managed to get some things right :)

Comments

Height for an empty div in Internet Explorer

I have just found about this stupid problem yesterday when trying to add some nice round corners to a page. I was using 4 empty div’s, setting the positions and a background image trough css. Everything was okay in SeaMonkey and Opera, but definitely not ok on Internet Explorer.

Trying to debug the problem, I added a 1px red border to all div’s to check correct positioning. So, there was supposed to be 4 red squares, the div’s were each exactly 9 x 9 px. But instead of that, Internet Explorer would display 4 red boxes having their height waaaay larger than 9px. The div border did not appear as a square even though its css specified exactly “width: 9px; height:9px“. That was pretty nice for the browser to ignore my css code.

After some researching, found about someone who stumbled over the same problem:
http://www.zucchetti.co.uk/?p=75

It seems it’s actually a IE-only problem. When rendering an empty div, Internet Explorer will use the current font size as its height. The fix is as simple as setting the font size to 0 by using “font-size:0“, getting rid of the minimum div height being the font-size.

Comments

Welcome….

This should be my personal site, where I SHOULD be posting different articles about interesting problems and solutions, my projects, ideas…

Comments