Archive for April, 2009

Troubleshooting gettext for php translation

Saturday, April 18th, 2009

I was trying to deploy a translated peice of software on a newly installed server running debian, but whatever I did, the strings would not appear in the translated language.

A sample php page might look something like this:

<?php
$locale = "fr_FR";
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "../locale");
textdomain("messages");
echo _("Name");
?>

Eventually I figured out that the setlocale() command was returning false, which means that the desired locale is not installed on the server.

To fix this, login as root and edit the file /etc/locale.gen

I have looked at another clean debian install, and it would appear that the default file only contains a single entry:

en_GB.UTF-8 UTF-8

I added a few more lines to this file, such as

de_DE.UTF-8 UTF-8
de_DE ISO-8859-1

and then (as root) ran

$ locale-gen

which generates the locales for the server. Then the gettext translation worked fine!

If your gettext does not work, check that your server is setup with the desired locale. You can get a list of them by running

$ locale -a