Tuesday May 27, 2008

Internationalising perl with gettext

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 textdomain call.

About

Darryl Gove is a senior engineer in the Solaris Studio team, working on optimising applications and benchmarks for current and future processors. He is also the author of the books:
Multicore Application Programming
Solaris Application Programming
The Developer's Edge

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
5
6
8
9
10
12
13
14
15
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
The Developer's Edge
Solaris Application Programming
Publications
Webcasts
Presentations
OpenSPARC Book
Multicore Application Programming
Docs