File Coverage

blib/lib/Locale/Messages.pm
Criterion Covered Total %
statement 162 183 88.5
branch 10 22 45.4
condition 28 36 77.7
subroutine 49 56 87.5
pod 28 28 100.0
total 277 325 85.2


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   96111 use strict;
  25         170  
  25         681  
24              
25 25     25   115 use File::Spec;
  25         41  
  25         579  
26              
27 25     25   109 use vars qw ($package @EXPORT_OK %EXPORT_TAGS @ISA $VERSION);
  25         43  
  25         9917  
28              
29             $VERSION = '1.31';
30              
31             # Try to load the C version first.
32             $package = 'gettext_xs';
33              
34             # Do not load from current working directory.
35             local @INC = grep { File::Spec->file_name_is_absolute($_) } @INC;
36              
37             eval <<'EOF';
38             require Locale::gettext_xs;
39             my $version = Locale::gettext_xs::__gettext_xs_version();
40             die "Version: version mismatch ($VERSION vs. $version)" unless $version eq $VERSION;
41             EOF
42             my $no_xs = $@;
43              
44             # There are systems where setlocale() and the LC_ constants are not
45             # defined at all, see https://rt.cpan.org/Ticket/Display.html?id=98109
46             #
47             # On such systems, we always fall back to gettext_dumb.
48             if ($no_xs) {
49             eval {
50             require POSIX;
51             # void
52             POSIX::setlocale(POSIX::LC_ALL());
53             };
54             if ($@) {
55             $package = 'gettext_dumb';
56             require Locale::gettext_dumb;
57             } else {
58             $package = 'gettext_pp';
59             require Locale::gettext_pp;
60             }
61             }
62            
63             require Exporter;
64             @ISA = qw (Exporter);
65             %EXPORT_TAGS = (locale_h => [ qw (gettext
66             dgettext
67             dcgettext
68             ngettext
69             dngettext
70             dcngettext
71             pgettext
72             dpgettext
73             dcpgettext
74             npgettext
75             dnpgettext
76             dcnpgettext
77             textdomain
78             bindtextdomain
79             bind_textdomain_codeset
80             )
81             ],
82             libintl_h => [ qw (LC_CTYPE
83             LC_NUMERIC
84             LC_TIME
85             LC_COLLATE
86             LC_MONETARY
87             LC_MESSAGES
88             LC_ALL)
89             ],
90             );
91              
92             @EXPORT_OK = qw (select_package
93             turn_utf_8_on
94             turn_utf_8_off
95             gettext
96             dgettext
97             dcgettext
98             ngettext
99             dngettext
100             dcngettext
101             pgettext
102             dpgettext
103             dcpgettext
104             npgettext
105             dnpgettext
106             dcnpgettext
107             textdomain
108             bindtextdomain
109             bind_textdomain_codeset
110             bind_textdomain_filter
111             nl_putenv
112             setlocale
113             LC_CTYPE
114             LC_NUMERIC
115             LC_TIME
116             LC_COLLATE
117             LC_MONETARY
118             LC_MESSAGES
119             LC_ALL);
120              
121             BEGIN {
122 25     25   96 my ($has_encode, $has_bytes);
123            
124 25 50       100 if ($] >= 5.006) {
125 25 50       92 unless (defined $has_encode) {
126 25         1339 eval "require Encode";
127 25         259667 $has_encode = !$@;
128             }
129              
130 25 50 33     109 unless ($has_encode || defined $has_bytes) {
131 0         0 eval "use bytes";
132 0         0 $has_bytes = !$@;
133             }
134             }
135              
136             # Turn the UTF-8 flag on or off unconditionally. The prototypes
137             # allow an optional second parameter, so that you can use the
138             # functions as callbacks to bind_textdomain_filter.
139 25 50       73 if ($has_encode) {
    0          
140 25     1607 1 7535 eval <<'EOF';
  1607     0 1 4609  
  1607         3052  
  0         0  
  0         0  
141             sub turn_utf_8_on($;$)
142             {
143             Encode::_utf8_on ($_[0]);
144             return $_[0];
145             }
146              
147             sub turn_utf_8_off($;$)
148             {
149             Encode::_utf8_off ($_[0]);
150             return $_[0];
151             }
152              
153             EOF
154             } elsif ($has_bytes) {
155 0         0 eval <<'EOF';
156             sub turn_utf_8_on($;$)
157             {
158             $_[0] = pack "U0C*", unpack "C*", $_[0];
159             }
160              
161             sub turn_utf_8_off($;$)
162             {
163             use bytes;
164             $_[0] = join "", split //, $_[0];
165             }
166              
167             EOF
168             } else {
169 0         0 eval <<'EOF';
170             sub turn_utf_8_on($;$)
171             {
172             return $_[0];
173             }
174              
175             sub turn_utf_8_off($;$)
176             {
177             return $_[0];
178             }
179              
180             EOF
181             }
182             }
183              
184             # The textdomain could be undef. We avoid a warning by specifying
185             # a filter for the undefined textdomain.
186             my %filters = (undef => \&turn_utf_8_off);
187              
188             sub select_package {
189 21     21 1 6183 my ($pkg, $compatibility) = @_;
190              
191             # Compatibility quirk for a bug pre 1.17:
192 21 50 33     153 if (__PACKAGE__ eq $pkg && defined $compatibility) {
193 21         39 $pkg = $compatibility;
194             }
195              
196 21 50 33     97 if ($no_xs && 'gettext_xs' eq $pkg) {
197 0         0 $pkg = 'gettext_pp';
198             }
199              
200 21 100 66     99 if (defined $pkg && 'gettext_pp' eq $pkg) {
    50          
201             # This branch is not unnecessary. The next (elsif) branch does
202             # essentially the same but catches compilation errors.
203 20         8416 require Locale::gettext_pp;
204 20         84 $package = 'gettext_pp';
205             } elsif (defined $pkg) {
206 1         38 my $filename = "Locale::$pkg";
207 1         11 $filename =~ s{::|\'}{/};
208 1         2 $filename .= '.pm';
209 1         2 eval { require $filename };
  1         493  
210 1 50       6 $package = $pkg unless $@;
211             } else {
212 0         0 eval "require Locale::gettext_xs";
213 0 0       0 $package = 'gettext_xs' unless $@;
214             }
215              
216 21         130 return $package;
217             }
218              
219             sub bind_textdomain_filter ($;$$) {
220 1     1 1 15 my ($textdomain, $coderef, $data) = @_;
221              
222 1         11 $filters{$textdomain} = [ $coderef, $data ];
223              
224 1         8 return 1;
225             }
226              
227             sub textdomain (;$) {
228 209     209 1 2294 my $function = "Locale::${package}::textdomain";
229            
230 25     25   193 no strict 'refs';
  25         39  
  25         1682  
231 209         611 &$function;
232             }
233              
234             sub bindtextdomain ($;$) {
235 41     41 1 1389 my $function = "Locale::${package}::bindtextdomain";
236              
237 25     25   147 no strict 'refs';
  25         37  
  25         1669  
238 41         162 &$function;
239             }
240              
241             sub bind_textdomain_codeset ($;$) {
242 2     2 1 180 my $function = "Locale::${package}::bind_textdomain_codeset";
243              
244 25     25   144 no strict 'refs';
  25         40  
  25         2491  
245 2         8 &$function;
246             }
247              
248             sub gettext ($) {
249 106     106 1 671 my $textdomain = textdomain;
250 106   100     270 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
251 106         139 my $cb = $filters{$textdomain};
252              
253 106         172 my $function = "Locale::${package}::gettext";
254            
255 25     25   154 no strict 'refs';
  25         38  
  25         2697  
256 106         240 $cb->[0] (&$function, $cb->[1]);
257             }
258              
259             sub dgettext($$) {
260 12   100 12 1 239 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
261              
262 12         31 my $function = "Locale::${package}::dgettext";
263            
264 25     25   209 no strict 'refs';
  25         58  
  25         2562  
265 12         48 $cb->[0] (&$function, $cb->[1]);
266             }
267              
268             sub dcgettext($$$) {
269 12   100 12 1 52 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
270              
271 12         27 my $function = "Locale::${package}::dcgettext";
272            
273 25     25   147 no strict 'refs';
  25         44  
  25         2730  
274 12         36 $cb->[0] (&$function, $cb->[1]);
275             }
276              
277             sub ngettext($$$) {
278 83     83 1 3953 my $textdomain = textdomain;
279 83   100     239 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
280 83         135 my $cb = $filters{$textdomain};
281              
282 83         144 my $function = "Locale::${package}::ngettext";
283            
284 25     25   152 no strict 'refs';
  25         45  
  25         2576  
285 83         206 $cb->[0] (&$function, $cb->[1]);
286             }
287              
288             sub dngettext($$$$) {
289 83   100 83 1 3442 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
290              
291 83         142 my $function = "Locale::${package}::dngettext";
292            
293 25     25   146 no strict 'refs';
  25         44  
  25         2510  
294 83         254 $cb->[0] (&$function, $cb->[1]);
295             }
296              
297             sub dcngettext($$$$$) {
298 83   100 83 1 224 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
299              
300 83         124 my $function = "Locale::${package}::dcngettext";
301            
302 25     25   150 no strict 'refs';
  25         46  
  25         2845  
303 83         198 $cb->[0] (&$function, $cb->[1]);
304             }
305              
306             sub pgettext($$) {
307 3     3 1 5 my $textdomain = textdomain;
308 3   50     9 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
309 3         4 my $cb = $filters{$textdomain};
310              
311 3         4 my $function = "Locale::${package}::pgettext";
312            
313 25     25   154 no strict 'refs';
  25         39  
  25         2526  
314 3         8 $cb->[0] (&$function, $cb->[1]);
315             }
316              
317             sub dpgettext($$$) {
318 4   100 4 1 16 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
319              
320 4         9 my $function = "Locale::${package}::dpgettext";
321            
322 25     25   178 no strict 'refs';
  25         48  
  25         2347  
323 4         13 $cb->[0] (&$function, $cb->[1]);
324             }
325              
326             sub dcpgettext($$$$) {
327 5   100 5 1 17 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
328              
329 5         11 my $function = "Locale::${package}::dcpgettext";
330            
331 25     25   155 no strict 'refs';
  25         113  
  25         2452  
332 5         19 $cb->[0] (&$function, $cb->[1]);
333             }
334              
335             sub npgettext($$$$) {
336 91   100 91 1 4610 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
337              
338 91         207 my $function = "Locale::${package}::npgettext";
339            
340 25     25   155 no strict 'refs';
  25         47  
  25         2292  
341 91         291 $cb->[0] (&$function, $cb->[1]);
342             }
343              
344             sub dnpgettext($$$$$) {
345 91   100 91 1 3777 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
346              
347 91         180 my $function = "Locale::${package}::dnpgettext";
348            
349 25     25   141 no strict 'refs';
  25         73  
  25         2508  
350 91         243 $cb->[0] (&$function, $cb->[1]);
351             }
352              
353             sub dcnpgettext($$$$$$) {
354 91   100 91 1 241 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
355              
356 91         157 my $function = "Locale::${package}::dcnpgettext";
357            
358 25     25   143 no strict 'refs';
  25         37  
  25         1952  
359 91         225 $cb->[0] (&$function, $cb->[1]);
360             }
361              
362             sub setlocale($;$) {
363 128     128 1 662 my $function = "Locale::${package}::setlocale";
364            
365 25     25   158 no strict 'refs';
  25         45  
  25         1854  
366 128         383 &$function;
367             }
368              
369             sub nl_putenv($) {
370 530     530 1 13772 my $function = "Locale::${package}::nl_putenv";
371            
372 25     25   164 no strict 'refs';
  25         34  
  25         1419  
373 530         1198 &$function;
374             }
375              
376             sub LC_NUMERIC {
377 0     0 1 0 my $function = "Locale::${package}::LC_NUMERIC";
378            
379 25     25   175 no strict 'refs';
  25         46  
  25         1728  
380 0         0 &$function;
381             }
382              
383             sub LC_CTYPE {
384 0     0 1 0 my $function = "Locale::${package}::LC_CTYPE";
385            
386 25     25   142 no strict 'refs';
  25         49  
  25         1527  
387 0         0 &$function;
388             }
389              
390             sub LC_TIME {
391 0     0 1 0 my $function = "Locale::${package}::LC_TIME";
392            
393 25     25   143 no strict 'refs';
  25         74  
  25         1484  
394 0         0 &$function;
395             }
396              
397             sub LC_COLLATE {
398 0     0 1 0 my $function = "Locale::${package}::LC_COLLATE";
399            
400 25     25   136 no strict 'refs';
  25         50  
  25         1632  
401 0         0 &$function;
402             }
403              
404             sub LC_MONETARY {
405 0     0 1 0 my $function = "Locale::${package}::LC_MONETARY";
406            
407 25     25   144 no strict 'refs';
  25         36  
  25         1550  
408 0         0 &$function;
409             }
410              
411             sub LC_MESSAGES {
412 191     191 1 39380 my $function = "Locale::${package}::LC_MESSAGES";
413            
414 25     25   147 no strict 'refs';
  25         38  
  25         3178  
415 191         4052 &$function;
416             }
417              
418             sub LC_ALL {
419 0     0 1   my $function = "Locale::${package}::LC_ALL";
420            
421 25     25   133 no strict 'refs';
  25         38  
  25         1446  
422 0           &$function;
423             }
424              
425             1;
426              
427             __END__