File Coverage

blib/lib/Locale/gettext.pm
Criterion Covered Total %
statement 7 62 11.2
branch 1 26 3.8
condition n/a
subroutine 2 14 14.2
pod 8 8 100.0
total 18 110 16.3


line stmt bran cond sub pod time code
1             package Locale::gettext;
2              
3             =head1 NAME
4              
5             Locale::gettext - message handling functions
6              
7             =head1 SYNOPSIS
8              
9             use Locale::gettext;
10             use POSIX; # Needed for setlocale()
11              
12             setlocale(LC_MESSAGES, "");
13              
14             # OO interface
15             my $d = Locale::gettext->domain("my_program");
16              
17             print $d->get("Welcome to my program"), "\n";
18             # (printed in the local language)
19              
20             # Direct access to C functions
21             textdomain("my_program");
22              
23             print gettext("Welcome to my program"), "\n";
24             # (printed in the local language)
25              
26             =head1 DESCRIPTION
27              
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.
31              
32             =cut
33              
34 5     5   50568 use Carp;
  5         12  
  5         747  
35              
36             require Exporter;
37             require DynaLoader;
38             @ISA = qw(Exporter DynaLoader);
39              
40             BEGIN {
41 5     5   9 eval {
42 5         7340 require Encode;
43 5         83647 $encode_available = 1;
44             };
45 5 50       5404 import Encode if ($encode_available);
46             }
47              
48             $VERSION = "1.05" ;
49              
50             %EXPORT_TAGS = (
51              
52             locale_h => [qw(LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_ALL)],
53              
54             libintl_h => [qw(gettext textdomain bindtextdomain dcgettext dgettext ngettext dngettext dcngettext bind_textdomain_codeset)],
55              
56             );
57              
58             Exporter::export_tags();
59              
60             @EXPORT_OK = qw(
61             );
62              
63             bootstrap Locale::gettext $VERSION;
64              
65             sub AUTOLOAD {
66 0     0     local $! = 0;
67 0           my $constname = $AUTOLOAD;
68 0           $constname =~ s/.*:://;
69 0 0         my $val = constant($constname, (@_ ? $_[0] : 0));
70 0 0         if ($! == 0) {
71 0     0     *$AUTOLOAD = sub { $val };
  0            
72             }
73             else {
74 0           croak "Missing constant $constname";
75             }
76 0           goto &$AUTOLOAD;
77             }
78              
79             =over 2
80              
81             =item $d = Locale::gettext->domain(DOMAIN)
82              
83             =item $d = Locale::gettext->domain_raw(DOMAIN)
84              
85             Creates a new object for retrieving strings in the domain B
86             and returns it. C requests that strings be returned as
87             Perl strings (possibly with wide characters) if possible while
88             C requests that octet strings directly from functions
89             like C.
90              
91             =cut
92              
93             sub domain_raw {
94 0     0 1   my ($class, $domain) = @_;
95 0           my $self = { domain => $domain, raw => 1 };
96 0           bless $self, $class;
97             }
98              
99             sub domain {
100 0     0 1   my ($class, $domain) = @_;
101 0 0         unless ($encode_available) {
102 0           croak "Encode module not available, cannot use Locale::gettext->domain";
103             }
104 0           my $self = { domain => $domain, raw => 0 };
105 0           bless $self, $class;
106 0           eval { bind_textdomain_codeset($self->{domain}, "UTF-8"); };
  0            
107 0 0         if ($@ =~ /not implemented/) {
    0          
108             # emulate it
109 0           $self->{emulate} = 1;
110             } elsif ($@ ne '') {
111 0           die; # some other problem
112             }
113 0           $self;
114             }
115              
116             =item $d->get(MSGID)
117              
118             Calls C to return the translated string for the given
119             B.
120              
121             =cut
122              
123             sub get {
124 0     0 1   my ($self, $msgid) = @_;
125 0           $self->_convert(dgettext($self->{domain}, $msgid));
126             }
127              
128             =item $d->cget(MSGID, CATEGORY)
129              
130             Calls C to return the translated string for the given
131             B in the given B.
132              
133             =cut
134              
135             sub cget {
136 0     0 1   my ($self, $msgid, $category) = @_;
137 0           $self->_convert(dcgettext($self->{domain}, $msgid, $category));
138             }
139              
140             =item $d->nget(MSGID, MSGID_PLURAL, N)
141              
142             Calls C to return the translated string for the given
143             B or B depending on B.
144              
145             =cut
146              
147             sub nget {
148 0     0 1   my ($self, $msgid, $msgid_plural, $n) = @_;
149 0           $self->_convert(dngettext($self->{domain}, $msgid, $msgid_plural, $n));
150             }
151              
152             =item $d->ncget(MSGID, MSGID_PLURAL, N, CATEGORY)
153              
154             Calls C to return the translated string for the given
155             B or B depending on B in the given
156             B.
157              
158             =cut
159              
160             sub ncget {
161 0     0 1   my ($self, $msgid, $msgid_plural, $n, $category) = @_;
162 0           $self->_convert(dcngettext($self->{domain}, $msgid, $msgid_plural, $n, $category));
163             }
164              
165             =item $d->dir([NEWDIR])
166              
167             If B is given, calls C 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.
171              
172             =cut
173              
174             sub dir {
175 0     0 1   my ($self, $newdir) = @_;
176 0 0         if (defined($newdir)) {
177 0           bindtextdomain($self->{domain}, $newdir);
178             } else {
179 0           bindtextdomain($self->{domain});
180             }
181             }
182              
183             =item $d->codeset([NEWCODE])
184              
185             For instances created with Cdomain_raw>, manuiplates
186             the character set of the returned strings.
187             If B is given, calls C 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.
191              
192             =cut
193              
194             sub codeset {
195 0     0 1   my ($self, $codeset) = @_;
196 0 0         if ($self->{raw} < 1) {
197 0           warn "Locale::gettext->codeset: meaningful only for instances created with domain_raw";
198 0           return;
199             }
200 0 0         if (defined($codeset)) {
201 0           bind_textdomain_codeset($self->{domain}, $codeset);
202             } else {
203 0           bind_textdomain_codeset($self->{domain});
204             }
205             }
206              
207             sub _convert {
208 0     0     my ($self, $str) = @_;
209 0 0         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 0 0         if ($self->{emulate}) {
213 0           delete $self->{emulate};
214 0           $self->{raw} = 1;
215 0           my $null = $self->get("");
216 0 0         if ($null =~ /charset=(\S+)/) {
217 0           $self->{decode_from} = $1;
218 0           $self->{raw} = 0;
219             } #else matches the behaviour of glibc - no null entry
220             # means no conversion is done
221             }
222 0 0         if ($self->{decode_from}) {
223 0           return decode($self->{decode_from}, $str);
224             } else {
225 0           return decode_utf8($str);
226             }
227             }
228              
229             sub DESTROY {
230 0     0     my ($self) = @_;
231             }
232              
233             =back
234              
235             gettext(), dgettext(), and dcgettext() attempt to retrieve a string
236             matching their C parameter within the context of the current
237             locale. dcgettext() takes the message's category and the text domain
238             as parameters while dcgettext() defaults to the LC_MESSAGES category
239             and gettext() defaults to LC_MESSAGES and uses the current text domain.
240             If the string is not found in the database, then C is returned.
241              
242             ngettext(), dngettext(), and dcngettext() function similarily but
243             implement differentiation of messages between singular and plural.
244             See the documentation for the corresponding C functions for details.
245              
246             textdomain() sets the current text domain and returns the previously
247             active domain.
248              
249             I instructs the retrieval functions to look
250             for the databases belonging to domain C in the directory
251             C
252              
253             I instructs the retrieval
254             functions to translate the returned messages to the character encoding
255             given by B if the encoding of the message catalog is known.
256              
257             =head1 NOTES
258              
259             Not all platforms provide all of the functions. Functions that are
260             not available in the underlying C library will not be available in
261             Perl either.
262              
263             Perl programs should use the object interface. In addition to being
264             able to return native Perl wide character strings,
265             C will be emulated if the C library does
266             not provide it.
267              
268             =head1 VERSION
269              
270             1.05.
271              
272             =head1 SEE ALSO
273              
274             gettext(3i), gettext(1), msgfmt(1)
275              
276             =head1 AUTHOR
277              
278             Phillip Vandry
279              
280             =cut
281              
282             1;