1
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.
7
38
@ISA = qw(Exporter DynaLoader);
43
$encode_available = 1;
45
import Encode if ($encode_available);
13
52
locale_h => [qw(LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_ALL)],
15
libintl_h => [qw(gettext textdomain bindtextdomain dcgettext dgettext)],
54
libintl_h => [qw(gettext textdomain bindtextdomain dcgettext dgettext ngettext dngettext dcngettext bind_textdomain_codeset)],
46
gettext - message handling functions
51
use POSIX; # Needed for setlocale()
53
setlocale(LC_MESSAGES, "");
54
textdomain("my_program");
56
print gettext("Welcome to my program"), "\n";
57
# (printed in the local language)
61
The gettext module permits access from perl to the gettext() family of
62
functions for retrieving message strings from databases constructed
63
to internationalize software.
81
=item $d = Locale::gettext->domain(DOMAIN)
83
=item $d = Locale::gettext->domain_raw(DOMAIN)
85
Creates a new object for retrieving strings in the domain B<DOMAIN>
86
and returns it. C<domain> requests that strings be returned as
87
Perl strings (possibly with wide characters) if possible while
88
C<domain_raw> requests that octet strings directly from functions
94
my ($class, $domain) = @_;
95
my $self = { domain => $domain, raw => 1 };
100
my ($class, $domain) = @_;
101
unless ($encode_available) {
102
croak "Encode module not available, cannot use Locale::gettext->domain";
104
my $self = { domain => $domain, raw => 0 };
106
eval { bind_textdomain_codeset($self->{domain}, "UTF-8"); };
107
if ($@ =~ /not implemented/) {
109
$self->{emulate} = 1;
111
die; # some other problem
118
Calls C<dgettext()> to return the translated string for the given
124
my ($self, $msgid) = @_;
125
$self->_convert(dgettext($self->{domain}, $msgid));
128
=item $d->cget(MSGID, CATEGORY)
130
Calls C<dcgettext()> to return the translated string for the given
131
B<MSGID> in the given B<CATEGORY>.
136
my ($self, $msgid, $category) = @_;
137
$self->_convert(dcgettext($self->{domain}, $msgid, $category));
140
=item $d->nget(MSGID, MSGID_PLURAL, N)
142
Calls C<dngettext()> to return the translated string for the given
143
B<MSGID> or B<MSGID_PLURAL> depending on B<N>.
148
my ($self, $msgid, $msgid_plural, $n) = @_;
149
$self->_convert(dngettext($self->{domain}, $msgid, $msgid_plural, $n));
152
=item $d->ncget(MSGID, MSGID_PLURAL, N, CATEGORY)
154
Calls C<dngettext()> to return the translated string for the given
155
B<MSGID> or B<MSGID_PLURAL> depending on B<N> in the given
161
my ($self, $msgid, $msgid_plural, $n, $category) = @_;
162
$self->_convert(dcngettext($self->{domain}, $msgid, $msgid_plural, $n, $category));
165
=item $d->dir([NEWDIR])
167
If B<NEWDIR> is given, calls C<bindtextdomain> to set the
168
name of the directory where messages for the domain
169
represented by C<$d> are found. Returns the (possibly changed)
170
current directory name.
175
my ($self, $newdir) = @_;
176
if (defined($newdir)) {
177
bindtextdomain($self->{domain}, $newdir);
179
bindtextdomain($self->{domain});
183
=item $d->codeset([NEWCODE])
185
For instances created with C<Locale::gettext-E<gt>domain_raw>, manuiplates
186
the character set of the returned strings.
187
If B<NEWCODE> is given, calls C<bind_textdomain_codeset> to set the
188
character encoding in which messages for the domain
189
represented by C<$d> are returned. Returns the (possibly changed)
190
current encoding name.
195
my ($self, $codeset) = @_;
196
if ($self->{raw} < 1) {
197
warn "Locale::gettext->codeset: meaningful only for instances created with domain_raw";
200
if (defined($codeset)) {
201
bind_textdomain_codeset($self->{domain}, $codeset);
203
bind_textdomain_codeset($self->{domain});
208
my ($self, $str) = @_;
209
return $str if ($self->{raw});
210
# thanks to the use of UTF-8 in bind_textdomain_codeset, the
211
# result should always be valid UTF-8 when raw mode is not used.
212
if ($self->{emulate}) {
213
delete $self->{emulate};
215
my $null = $self->get("");
216
if ($null =~ /charset=(\S+)/) {
217
$self->{decode_from} = $1;
219
} #else matches the behaviour of glibc - no null entry
220
# means no conversion is done
222
if ($self->{decode_from}) {
223
return decode($self->{decode_from}, $str);
225
return decode_utf8($str);
65
235
gettext(), dgettext(), and dcgettext() attempt to retrieve a string
66
236
matching their C<msgid> parameter within the context of the current