cowboy has asked for the wisdom of the Perl Monks concerning the following question:

I'm having problems getting gettext to work. I've tried both Locale::gettext and Locale::TextDomain. The translation never happens, I always get 'Baz' back. Below is the code for both my attempts.

The locale datafile contains a msgid Baz which translates to 'New Baz'

/home/cowboy/locale/x/LC_MESSAGES/test.mo

Attempt 1 (Locale::gettext)

use strict; use Locale::gettext; use POSIX qw(:locale_h); setlocale( LC_ALL, 'x' ); my $d = Locale::gettext->domain('test'); $d->dir('/home/cowboy/locale'); print $d->get('Baz') . "\n";

Second attempt (Locale::TextDomain)

use strict; use Locale::TextDomain('test','/home/cowboy/locale'); use POSIX qw(:locale_h); setlocale( LC_ALL, 'x' ); print $__{'Baz'} . "\n";

Any help would be greatly appreciated. I'm not tied to these modules, or even gettext if a better alternative is suggested.

Update: using strace on the execution of the programs, the first one does not seem to even attempt to look in the directory I specified, instead, trying /usr/lib/locale. The second does a stat on the file, but doesn't actually attempt to open it.

Replies are listed 'Best First'.
Re: Problems using gettext in perl.
by Khen1950fx (Canon) on Jul 13, 2006 at 20:05 UTC
    Interesting question! The problem may not be with the modules, but with gettext itself. What editor are you using? And, is it setup for .po files? For example, in Emacs, some config changes should be made. I would also check to see that libintl is present and properly linked. There's a multititude of tweaks that can be done to gettext. See:

    http://www.iro.umontreal.ca/translation/HTML/gettext.html#Files

      I used kbabel for the .po file. msgfmt liked the file, msgunfmt gives me what I'd expect back from the .mo file. I've been through the documentation you linked a few times with no luck yet. Thanks for the reply.