File Coverage

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