1 package Locale::gettext;
5 Locale::gettext - message handling functions
10 use POSIX; # Needed for setlocale()
12 setlocale(LC_MESSAGES, "");
15 my $d = Locale::gettext->domain("my_program");
17 print $d->get("Welcome to my program"), "\n";
18 # (printed in the local language)
20 # Direct access to C functions
21 textdomain("my_program");
23 print gettext("Welcome to my program"), "\n";
24 # (printed in the local language)
28 The gettext module permits access from perl to the gettext() family of
29 functions for retrieving message strings from databases constructed
30 to internationalize software.
39 @ISA = qw(Exporter DynaLoader);
44 $encode_available = 1;
46 import Encode if ($encode_available);
53 locale_h => [qw(LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_ALL)],
55 libintl_h => [qw(gettext textdomain bindtextdomain dcgettext dgettext ngettext dngettext dcngettext bind_textdomain_codeset)],
59 Exporter::export_tags();
64 bootstrap Locale::gettext $VERSION;
68 my $constname = $AUTOLOAD;
69 $constname =~ s/.*:://;
70 my $val = constant($constname, (@_ ? $_[0] : 0));
72 *$AUTOLOAD = sub { $val };
75 croak "Missing constant $constname";
82 =item $d = Locale::gettext->domain(DOMAIN)
84 =item $d = Locale::gettext->domain_raw(DOMAIN)
86 Creates a new object for retrieving strings in the domain B<DOMAIN>
87 and returns it. C<domain> requests that strings be returned as
88 Perl strings (possibly with wide characters) if possible while
89 C<domain_raw> requests that octet strings directly from functions
95 my ($class, $domain) = @_;
96 my $self = { domain => $domain, raw => 1 };
101 my ($class, $domain) = @_;
102 unless ($encode_available) {
103 croak "Encode module not available, cannot use Locale::gettext->domain";
105 my $self = { domain => $domain, raw => 0 };
107 eval { bind_textdomain_codeset($self->{domain}, "UTF-8"); };
108 if ($@ =~ /not implemented/) {
110 $self->{emulate} = 1;
112 die; # some other problem
119 Calls C<dgettext()> to return the translated string for the given
125 my ($self, $msgid) = @_;
126 $self->_convert(dgettext($self->{domain}, $msgid));
129 =item $d->cget(MSGID, CATEGORY)
131 Calls C<dcgettext()> to return the translated string for the given
132 B<MSGID> in the given B<CATEGORY>.
137 my ($self, $msgid, $category) = @_;
138 $self->_convert(dcgettext($self->{domain}, $msgid, $category));
141 =item $d->nget(MSGID, MSGID_PLURAL, N)
143 Calls C<dngettext()> to return the translated string for the given
144 B<MSGID> or B<MSGID_PLURAL> depending on B<N>.
149 my ($self, $msgid, $msgid_plural, $n) = @_;
150 $self->_convert(dngettext($self->{domain}, $msgid, $msgid_plural, $n));
153 =item $d->ncget(MSGID, MSGID_PLURAL, N, CATEGORY)
155 Calls C<dngettext()> to return the translated string for the given
156 B<MSGID> or B<MSGID_PLURAL> depending on B<N> in the given
162 my ($self, $msgid, $msgid_plural, $n, $category) = @_;
163 $self->_convert(dcngettext($self->{domain}, $msgid, $msgid_plural, $n, $category));
166 =item $d->dir([NEWDIR])
168 If B<NEWDIR> is given, calls C<bindtextdomain> to set the
169 name of the directory where messages for the domain
170 represented by C<$d> are found. Returns the (possibly changed)
171 current directory name.
176 my ($self, $newdir) = @_;
177 if (defined($newdir)) {
178 bindtextdomain($self->{domain}, $newdir);
180 bindtextdomain($self->{domain});
184 =item $d->codeset([NEWCODE])
186 For instances created with C<Locale::gettext-E<gt>domain_raw>, manuiplates
187 the character set of the returned strings.
188 If B<NEWCODE> is given, calls C<bind_textdomain_codeset> to set the
189 character encoding in which messages for the domain
190 represented by C<$d> are returned. Returns the (possibly changed)
191 current encoding name.
196 my ($self, $codeset) = @_;
197 if ($self->{raw} < 1) {
198 warn "Locale::gettext->codeset: meaningful only for instances created with domain_raw";
201 if (defined($codeset)) {
202 bind_textdomain_codeset($self->{domain}, $codeset);
204 bind_textdomain_codeset($self->{domain});
209 my ($self, $str) = @_;
210 return $str if ($self->{raw});
211 # thanks to the use of UTF-8 in bind_textdomain_codeset, the
212 # result should always be valid UTF-8 when raw mode is not used.
213 if ($self->{emulate}) {
214 delete $self->{emulate};
216 my $null = $self->get("");
217 if ($null =~ /charset=(\S+)/) {
218 $self->{decode_from} = $1;
220 } #else matches the behaviour of glibc - no null entry
221 # means no conversion is done
223 if ($self->{decode_from}) {
224 return decode($self->{decode_from}, $str);
226 return decode_utf8($str);
236 gettext(), dgettext(), and dcgettext() attempt to retrieve a string
237 matching their C<msgid> parameter within the context of the current
238 locale. dcgettext() takes the message's category and the text domain
239 as parameters while dcgettext() defaults to the LC_MESSAGES category
240 and gettext() defaults to LC_MESSAGES and uses the current text domain.
241 If the string is not found in the database, then C<msgid> is returned.
243 ngettext(), dngettext(), and dcngettext() function similarily but
244 implement differentiation of messages between singular and plural.
245 See the documentation for the corresponding C functions for details.
247 textdomain() sets the current text domain and returns the previously
250 I<bindtextdomain(domain, dirname)> instructs the retrieval functions to look
251 for the databases belonging to domain C<domain> in the directory
254 I<bind_textdomain_codeset(domain, codeset)> instructs the retrieval
255 functions to translate the returned messages to the character encoding
256 given by B<codeset> if the encoding of the message catalog is known.
260 Not all platforms provide all of the functions. Functions that are
261 not available in the underlying C library will not be available in
264 Perl programs should use the object interface. In addition to being
265 able to return native Perl wide character strings,
266 C<bind_textdomain_codeset> will be emulated if the C library does
275 gettext(3i), gettext(1), msgfmt(1)
279 Phillip Vandry <vandry@TZoNE.ORG>