File Coverage

blib/lib/Locale/Messages.pm
Criterion Covered Total %
statement 159 180 88.3
branch 10 22 45.4
condition 28 36 77.7
subroutine 48 55 87.2
pod 28 28 100.0
total 273 321 85.0


line stmt bran cond sub pod time code
1             #! /bin/false
2              
3             # vim: set autoindent shiftwidth=4 tabstop=4:
4              
5             # Copyright (C) 2002-2017 Guido Flohr ,
6             # all rights reserved.
7              
8             # This program is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation; either version 3 of the License, or
11             # (at your option) any later version.
12              
13             # This program is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17              
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see .
20              
21             package Locale::Messages;
22              
23 25     25   112116 use strict;
  25         221  
  25         895  
24              
25 25     25   138 use vars qw ($package @EXPORT_OK %EXPORT_TAGS @ISA $VERSION);
  25         50  
  25         9281  
26              
27             $VERSION = '1.32';
28              
29             # Try to load the C version first.
30             $package = 'gettext_xs';
31              
32             # Do not load from current working directory.
33             local @INC = grep { $_ ne '.' } @INC;
34              
35             eval <<'EOF';
36             require Locale::gettext_xs;
37             my $version = Locale::gettext_xs::__gettext_xs_version();
38             die "Version: version mismatch ($VERSION vs. $version)" unless $version eq $VERSION;
39             EOF
40             my $no_xs = $@;
41              
42             # There are systems where setlocale() and the LC_ constants are not
43             # defined at all, see https://rt.cpan.org/Ticket/Display.html?id=98109
44             #
45             # On such systems, we always fall back to gettext_dumb.
46             if ($no_xs) {
47             eval {
48             require POSIX;
49             # void
50             POSIX::setlocale(POSIX::LC_ALL());
51             };
52             if ($@) {
53             $package = 'gettext_dumb';
54             require Locale::gettext_dumb;
55             } else {
56             $package = 'gettext_pp';
57             require Locale::gettext_pp;
58             }
59             }
60            
61             require Exporter;
62             @ISA = qw (Exporter);
63             %EXPORT_TAGS = (locale_h => [ qw (gettext
64             dgettext
65             dcgettext
66             ngettext
67             dngettext
68             dcngettext
69             pgettext
70             dpgettext
71             dcpgettext
72             npgettext
73             dnpgettext
74             dcnpgettext
75             textdomain
76             bindtextdomain
77             bind_textdomain_codeset
78             )
79             ],
80             libintl_h => [ qw (LC_CTYPE
81             LC_NUMERIC
82             LC_TIME
83             LC_COLLATE
84             LC_MONETARY
85             LC_MESSAGES
86             LC_ALL)
87             ],
88             );
89              
90             @EXPORT_OK = qw (select_package
91             turn_utf_8_on
92             turn_utf_8_off
93             gettext
94             dgettext
95             dcgettext
96             ngettext
97             dngettext
98             dcngettext
99             pgettext
100             dpgettext
101             dcpgettext
102             npgettext
103             dnpgettext
104             dcnpgettext
105             textdomain
106             bindtextdomain
107             bind_textdomain_codeset
108             bind_textdomain_filter
109             nl_putenv
110             setlocale
111             LC_CTYPE
112             LC_NUMERIC
113             LC_TIME
114             LC_COLLATE
115             LC_MONETARY
116             LC_MESSAGES
117             LC_ALL);
118              
119             BEGIN {
120 25     25   1900 my ($has_encode, $has_bytes);
121            
122 25 50       144 if ($] >= 5.006) {
123 25 50       105 unless (defined $has_encode) {
124 25         1589 eval "require Encode";
125 25         303433 $has_encode = !$@;
126             }
127              
128 25 50 33     125 unless ($has_encode || defined $has_bytes) {
129 0         0 eval "use bytes";
130 0         0 $has_bytes = !$@;
131             }
132             }
133              
134             # Turn the UTF-8 flag on or off unconditionally. The prototypes
135             # allow an optional second parameter, so that you can use the
136             # functions as callbacks to bind_textdomain_filter.
137 25 50       89 if ($has_encode) {
    0          
138 25     1607 1 8751 eval <<'EOF';
  1607     0 1 5426  
  1607         3650  
  0         0  
  0         0  
139             sub turn_utf_8_on($;$)
140             {
141             Encode::_utf8_on ($_[0]);
142             return $_[0];
143             }
144              
145             sub turn_utf_8_off($;$)
146             {
147             Encode::_utf8_off ($_[0]);
148             return $_[0];
149             }
150              
151             EOF
152             } elsif ($has_bytes) {
153 0         0 eval <<'EOF';
154             sub turn_utf_8_on($;$)
155             {
156             $_[0] = pack "U0C*", unpack "C*", $_[0];
157             }
158              
159             sub turn_utf_8_off($;$)
160             {
161             use bytes;
162             $_[0] = join "", split //, $_[0];
163             }
164              
165             EOF
166             } else {
167 0         0 eval <<'EOF';
168             sub turn_utf_8_on($;$)
169             {
170             return $_[0];
171             }
172              
173             sub turn_utf_8_off($;$)
174             {
175             return $_[0];
176             }
177              
178             EOF
179             }
180             }
181              
182             # The textdomain could be undef. We avoid a warning by specifying
183             # a filter for the undefined textdomain.
184             my %filters = (undef => \&turn_utf_8_off);
185              
186             sub select_package {
187 21     21 1 6903 my ($pkg, $compatibility) = @_;
188              
189             # Compatibility quirk for a bug pre 1.17:
190 21 50 33     170 if (__PACKAGE__ eq $pkg && defined $compatibility) {
191 21         54 $pkg = $compatibility;
192             }
193              
194 21 50 33     120 if ($no_xs && 'gettext_xs' eq $pkg) {
195 0         0 $pkg = 'gettext_pp';
196             }
197              
198 21 100 66     119 if (defined $pkg && 'gettext_pp' eq $pkg) {
    50          
199             # This branch is not unnecessary. The next (elsif) branch does
200             # essentially the same but catches compilation errors.
201 20         10075 require Locale::gettext_pp;
202 20         96 $package = 'gettext_pp';
203             } elsif (defined $pkg) {
204 1         4 my $filename = "Locale::$pkg";
205 1         8 $filename =~ s{::|\'}{/};
206 1         3 $filename .= '.pm';
207 1         44 eval { require $filename };
  1         465  
208 1 50       6 $package = $pkg unless $@;
209             } else {
210 0         0 eval "require Locale::gettext_xs";
211 0 0       0 $package = 'gettext_xs' unless $@;
212             }
213              
214 21         154 return $package;
215             }
216              
217             sub bind_textdomain_filter ($;$$) {
218 1     1 1 19 my ($textdomain, $coderef, $data) = @_;
219              
220 1         4 $filters{$textdomain} = [ $coderef, $data ];
221              
222 1         6 return 1;
223             }
224              
225             sub textdomain (;$) {
226 209     209 1 2506 my $function = "Locale::${package}::textdomain";
227            
228 25     25   221 no strict 'refs';
  25         52  
  25         1971  
229 209         686 &$function;
230             }
231              
232             sub bindtextdomain ($;$) {
233 41     41 1 1714 my $function = "Locale::${package}::bindtextdomain";
234              
235 25     25   166 no strict 'refs';
  25         50  
  25         1868  
236 41         179 &$function;
237             }
238              
239             sub bind_textdomain_codeset ($;$) {
240 2     2 1 242 my $function = "Locale::${package}::bind_textdomain_codeset";
241              
242 25     25   171 no strict 'refs';
  25         57  
  25         2936  
243 2         14 &$function;
244             }
245              
246             sub gettext ($) {
247 106     106 1 790 my $textdomain = textdomain;
248 106   100     333 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
249 106         173 my $cb = $filters{$textdomain};
250              
251 106         188 my $function = "Locale::${package}::gettext";
252            
253 25     25   195 no strict 'refs';
  25         49  
  25         3049  
254 106         306 $cb->[0] (&$function, $cb->[1]);
255             }
256              
257             sub dgettext($$) {
258 12   100 12 1 248 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
259              
260 12         33 my $function = "Locale::${package}::dgettext";
261            
262 25     25   177 no strict 'refs';
  25         46  
  25         3169  
263 12         60 $cb->[0] (&$function, $cb->[1]);
264             }
265              
266             sub dcgettext($$$) {
267 12   100 12 1 64 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
268              
269 12         45 my $function = "Locale::${package}::dcgettext";
270            
271 25     25   191 no strict 'refs';
  25         47  
  25         3213  
272 12         49 $cb->[0] (&$function, $cb->[1]);
273             }
274              
275             sub ngettext($$$) {
276 83     83 1 3936 my $textdomain = textdomain;
277 83   100     210 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
278 83         142 my $cb = $filters{$textdomain};
279              
280 83         140 my $function = "Locale::${package}::ngettext";
281            
282 25     25   177 no strict 'refs';
  25         56  
  25         2917  
283 83         212 $cb->[0] (&$function, $cb->[1]);
284             }
285              
286             sub dngettext($$$$) {
287 83   100 83 1 4165 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
288              
289 83         179 my $function = "Locale::${package}::dngettext";
290            
291 25     25   177 no strict 'refs';
  25         48  
  25         2972  
292 83         293 $cb->[0] (&$function, $cb->[1]);
293             }
294              
295             sub dcngettext($$$$$) {
296 83   100 83 1 273 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
297              
298 83         172 my $function = "Locale::${package}::dcngettext";
299            
300 25     25   191 no strict 'refs';
  25         52  
  25         3275  
301 83         259 $cb->[0] (&$function, $cb->[1]);
302             }
303              
304             sub pgettext($$) {
305 3     3 1 7 my $textdomain = textdomain;
306 3   50     11 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
307 3         6 my $cb = $filters{$textdomain};
308              
309 3         6 my $function = "Locale::${package}::pgettext";
310            
311 25     25   198 no strict 'refs';
  25         68  
  25         3011  
312 3         12 $cb->[0] (&$function, $cb->[1]);
313             }
314              
315             sub dpgettext($$$) {
316 4   100 4 1 20 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
317              
318 4         11 my $function = "Locale::${package}::dpgettext";
319            
320 25     25   178 no strict 'refs';
  25         61  
  25         3236  
321 4         14 $cb->[0] (&$function, $cb->[1]);
322             }
323              
324             sub dcpgettext($$$$) {
325 5   100 5 1 27 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
326              
327 5         11 my $function = "Locale::${package}::dcpgettext";
328            
329 25     25   184 no strict 'refs';
  25         48  
  25         2864  
330 5         15 $cb->[0] (&$function, $cb->[1]);
331             }
332              
333             sub npgettext($$$$) {
334 91   100 91 1 4823 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
335              
336 91         185 my $function = "Locale::${package}::npgettext";
337            
338 25     25   185 no strict 'refs';
  25         96  
  25         2918  
339 91         294 $cb->[0] (&$function, $cb->[1]);
340             }
341              
342             sub dnpgettext($$$$$) {
343 91   100 91 1 4680 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
344              
345 91         186 my $function = "Locale::${package}::dnpgettext";
346            
347 25     25   180 no strict 'refs';
  25         43  
  25         2828  
348 91         297 $cb->[0] (&$function, $cb->[1]);
349             }
350              
351             sub dcnpgettext($$$$$$) {
352 91   100 91 1 349 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
353              
354 91         181 my $function = "Locale::${package}::dcnpgettext";
355            
356 25     25   224 no strict 'refs';
  25         102  
  25         2346  
357 91         288 $cb->[0] (&$function, $cb->[1]);
358             }
359              
360             sub setlocale($;$) {
361 128     128 1 774 my $function = "Locale::${package}::setlocale";
362            
363 25     25   171 no strict 'refs';
  25         46  
  25         1722  
364 128         438 &$function;
365             }
366              
367             sub nl_putenv($) {
368 530     530 1 14472 my $function = "Locale::${package}::nl_putenv";
369            
370 25     25   175 no strict 'refs';
  25         52  
  25         2150  
371 530         1399 &$function;
372             }
373              
374             sub LC_NUMERIC {
375 0     0 1 0 my $function = "Locale::${package}::LC_NUMERIC";
376            
377 25     25   219 no strict 'refs';
  25         51  
  25         1998  
378 0         0 &$function;
379             }
380              
381             sub LC_CTYPE {
382 0     0 1 0 my $function = "Locale::${package}::LC_CTYPE";
383            
384 25     25   175 no strict 'refs';
  25         69  
  25         1684  
385 0         0 &$function;
386             }
387              
388             sub LC_TIME {
389 0     0 1 0 my $function = "Locale::${package}::LC_TIME";
390            
391 25     25   158 no strict 'refs';
  25         51  
  25         1750  
392 0         0 &$function;
393             }
394              
395             sub LC_COLLATE {
396 0     0 1 0 my $function = "Locale::${package}::LC_COLLATE";
397            
398 25     25   182 no strict 'refs';
  25         52  
  25         1917  
399 0         0 &$function;
400             }
401              
402             sub LC_MONETARY {
403 0     0 1 0 my $function = "Locale::${package}::LC_MONETARY";
404            
405 25     25   162 no strict 'refs';
  25         41  
  25         1716  
406 0         0 &$function;
407             }
408              
409             sub LC_MESSAGES {
410 191     191 1 28990 my $function = "Locale::${package}::LC_MESSAGES";
411            
412 25     25   172 no strict 'refs';
  25         57  
  25         1674  
413 191         4899 &$function;
414             }
415              
416             sub LC_ALL {
417 0     0 1   my $function = "Locale::${package}::LC_ALL";
418            
419 25     25   174 no strict 'refs';
  25         60  
  25         3550  
420 0           &$function;
421             }
422              
423             1;
424              
425             __END__