Internationalising perl with gettext
By Darryl Gove on May 27, 2008
The previous blog entry showed how to do internationalisation using an example in C. Unsurprisingly very similar code can be used in perl.
#!/bin/perl -w use POSIX; use Sun::Solaris::Utils qw(textdomain gettext bindtextdomain); setlocale(LC_MESSAGES,"fr"); bindtextdomain("EX","/export/home/perl_test/locale"); textdomain("EX"); printf gettext("test\\n");
The perl code uses the same text domain, and same binding to end up using the same message catalogue as the C example. So again this code will output "notest" rather than "test" when run.
One thing that's caught me a couple of times is the use of the setting
LC_MESSAGES in the call to
setlocale. It's tempting to use
LC_ALL which means that all text should be translated, but that does not work - I believe the reason is that a sublocale needs to be specified. The higher level locale may have sublocales with conflicting settings - for example an application that outputs messages in English could have Pounds or Dollars as currency.
The other thing to be aware of is that the call to
bindtextdomain needs to be performed before that domain is used by the