TextDomain.pm

filter: Statementstmt 55.1% 81 / 147 Branchbran 29.1% 14 / 48 Conditioncond 13.8% 5 / 36 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 52.9% 18 / 34 Podpod 100% 7 / 7 totaltotal 42.9% 127 / 296 CC 40 SCAR 21.6
CC
40
Cov
43%
__tied_gettext
30.0
__x
12.0
__n
12.0
CRAP 8.7
« RecodeData/ISO_8859_1.pm ↑ Summary Recode.pm »
1 #! /bin/false
2  
3 # vim: set autoindent shiftwidth=4 tabstop=4:
4  
5 # High-level interface to Perl i18n.
6 # Copyright (C) 2002-2026 Guido Flohr <guido.flohr@cantanea.com>,
7 # all rights reserved.
8  
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13  
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18  
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21  
22 package __TiedTextDomain;
23  
24 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub TIEHASH
27 3 ▶{
Subroutine TIEHASH: called CC=1 SCAR=0.0
28 3 my ($class, $function) = @_;
29 3 bless {
30 __function => $function,
31 }, $class;
32 }
33  
34 sub FETCH
35 0 ▶{
Subroutine FETCH: not called CC=1 SCAR=6.9
36 0 my ($self, $msg) = @_;
37
38 0 &{$self->{__function}} ($msg);
39 }
40  
41 sub FIRSTKEY
42 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44 0 my $reset_iterator = keys %$self;
45 0 return scalar each %$self;
46 }
47  
48 sub NEXTKEY
49 0 ▶{
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
50 0 my $self = shift;
51 0 return scalar each %$self;
52 }
53  
54 0 ▶sub CLEAR {}
Subroutine CLEAR: not called CC=1 SCAR=0.0
55 0 ▶sub STORE {}
Subroutine STORE: not called CC=1 SCAR=0.0
56 0 ▶sub DELETE {}
Subroutine DELETE: not called CC=1 SCAR=0.0
57  
58 3 1;
59  
60 package Locale::TextDomain;
61  
62 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 3 ▶use Locale::Messages qw (textdomain bindtextdomain dgettext dngettext dpgettext dnpgettext);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 ▶use Cwd qw (abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 3 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 3 $VERSION = '1.37';
70  
71 3 require Exporter;
72  
73 3 ▶use vars qw (@ISA @EXPORT %__ $__);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 3 @ISA = ('Exporter');
76 3 @EXPORT = qw (__ __x __n __nx __xn __p __px __np __npx $__ %__
77 N__ N__n N__p N__np);
78  
79 3 my %textdomains = ();
80 3 my %bound_dirs = ();
81 3 my @default_dirs = ();
82  
83 sub __ ($);
84
85 sub __find_domain ($);
86 sub __expand ($%);
87 sub __tied_gettext ($$);
88  
89 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
90 # Tie the hash to gettext().
91 3 tie %__, '__TiedTextDomain', \&__tied_gettext;
92 3 $__ = \%__;
93  
94 # Add default search directories, but only if they exist.
95 3 for my $dir (qw (/usr/share/locale /usr/local/share/locale)) {
96 3 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 3 0 3
97 3 @default_dirs = ($dir);
98 3 last;
99 }
100 }
101 }
102  
103 # Class methods.
104 2 ▶sub keywords {
Subroutine keywords: called CC=1 SCAR=0.0
Pod documented
105 2 join ' ', (
106 '--keyword=__',
107 '--keyword=%__',
108 '--keyword=$__',
109 '--keyword=__x',
110 '--keyword=__n:1,2',
111 '--keyword=__nx:1,2',
112 '--keyword=__xn:1,2',
113 '--keyword=__p:1c,2',
114 '--keyword=__px:1c,2',
115 '--keyword=__np:1c,2,3',
116 '--keyword=__npx:1c,2,3',
117 '--keyword=N__',
118 '--keyword=N__n:1,2',
119 '--keyword=N__p:1c,2',
120 '--keyword=N__np:1c,2,3',
121 );
122 }
123  
124 2 ▶sub flags {
Subroutine flags: called CC=1 SCAR=0.0
Pod documented
125 2 join ' ', (
126 '--flag=__:1:pass-perl-format',
127 '--flag=%__:1:pass-perl-format',
128 '--flag=$__:1:pass-perl-format',
129 '--flag=__x:1:perl-brace-format',
130 '--flag=__x:1:pass-perl-format',
131 '--flag=__n:1:pass-perl-format',
132 '--flag=__n:2:pass-perl-format',
133 '--flag=__nx:1:perl-brace-format',
134 '--flag=__nx:1:pass-perl-format',
135 '--flag=__nx:2:perl-brace-format',
136 '--flag=__nx:2:pass-perl-format',
137 '--flag=__xn:1:perl-brace-format',
138 '--flag=__xn:1:pass-perl-format',
139 '--flag=__xn:2:perl-brace-format',
140 '--flag=__xn:2:pass-perl-format',
141 '--flag=__p:2:pass-perl-format',
142 '--flag=__px:2:perl-brace-format',
143 '--flag=__px:2:pass-perl-format',
144 '--flag=__np:2:pass-perl-format',
145 '--flag=__np:3:pass-perl-format',
146 '--flag=__npx:2:perl-brace-format',
147 '--flag=__npx:2:pass-perl-format',
148 '--flag=__npx:3:perl-brace-format',
149 '--flag=__npx:3:pass-perl-format',
150 '--flag=N__:1:pass-perl-format',
151 '--flag=N__n:1:pass-perl-format',
152 '--flag=N__n:2:pass-perl-format',
153 '--flag=N__p:2:pass-perl-format',
154 '--flag=N__np:2:pass-perl-format',
155 '--flag=N__np:3:pass-perl-format',
156 );
157 }
158  
159 1 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
160 1 my ($class) = @_;
161  
162 1 join ' ', $class->keywords, $class->flags;
163 }
164  
165 # Normal gettext.
166 sub __ ($)
167 2 ▶{
Subroutine __: called CC=3 SCAR=11.0
168 2 my $msgid = shift;
169
170 2 my $package = caller;
171
172 2 my $textdomain = $textdomains{$package};
173
174 __find_domain $textdomain if
175 2 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 1 1 2
Condition66%2/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
011
MC/DC50%1/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table66%2/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
176
177 2 return dgettext $textdomain => $msgid;
178 }
179  
180 # Called from tied hash.
181 sub __tied_gettext ($$)
182 0 ▶{
Subroutine __tied_gettext: not called CC=5 SCAR=34.0
183 0 my ($msgid) = @_;
184
185 0 my ($package) = caller (1);
186
187 0 my $textdomain = $textdomains{$package};
188 0 ▶ unless (defined $textdomain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $textdomain) 0 0 0
189 0 my ($maybe_package, $filename, $line) = caller (2);
190 0 ▶ if (exists $textdomains{$maybe_package}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $textdomains{$maybe_package}) 0 0 0
191 0 warn <<EOF;
192 Probable use of \$__ or \%__ where __() should be used at $filename:$line.
193 EOF
194 }
195 }
196 __find_domain $textdomain if
197 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
198
199 0 return dgettext $textdomain => $msgid;
200 }
201  
202 # With interpolation.
203 sub __x ($@)
204 0 ▶{
Subroutine __x: not called CC=3 SCAR=24.8
205 0 my ($msgid, %vars) = @_;
206
207 0 my $package = caller;
208
209 0 my $textdomain = $textdomains{$package};
210
211 __find_domain $textdomain if
212 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
213
214 0 return __expand ((dgettext $textdomain => $msgid), %vars);
215 }
216  
217 # Plural.
218 sub __n ($$$)
219 0 ▶{
Subroutine __n: not called CC=3 SCAR=24.8
220 0 my ($msgid, $msgid_plural, $count) = @_;
221
222 0 my $package = caller;
223
224 0 my $textdomain = $textdomains{$package};
225
226 __find_domain $textdomain if
227 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
228
229 0 return dngettext $textdomain, $msgid, $msgid_plural, $count;
230 }
231  
232 # Plural with interpolation.
233 sub __nx ($$$@)
234 0 ▶{
Subroutine __nx: not called CC=3 SCAR=24.8
235 0 my ($msgid, $msgid_plural, $count, %args) = @_;
236
237 0 my $package = caller;
238
239 0 my $textdomain = $textdomains{$package};
240
241 __find_domain $textdomain if
242 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
243
244 0 return __expand ((dngettext $textdomain, $msgid, $msgid_plural, $count),
245 %args);
246 }
247  
248 # Plural with interpolation.
249 sub __xn ($$$@)
250 0 ▶{
Subroutine __xn: not called CC=3 SCAR=24.8
251 0 my ($msgid, $msgid_plural, $count, %args) = @_;
252
253 0 my $package = caller;
254
255 0 my $textdomain = $textdomains{$package};
256
257 __find_domain $textdomain if
258 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
259
260 0 return __expand ((dngettext $textdomain, $msgid, $msgid_plural, $count),
261 %args);
262 }
263  
264 # Context. (p is for particular or special)
265 sub __p ($$)
266 0 ▶{
Subroutine __p: not called CC=3 SCAR=24.8
267 0 my $msgctxt = shift;
268 0 my $msgid = shift;
269
270 0 my $package = caller;
271
272 0 my $textdomain = $textdomains{$package};
273
274 __find_domain $textdomain if
275 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
276
277 0 return dpgettext $textdomain => $msgctxt, $msgid;
278 }
279  
280 # With interpolation.
281 sub __px ($$@)
282 0 ▶{
Subroutine __px: not called CC=3 SCAR=24.8
283 0 my ($msgctxt, $msgid, %vars) = @_;
284
285 0 my $package = caller;
286
287 0 my $textdomain = $textdomains{$package};
288
289 __find_domain $textdomain if
290 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
291
292 0 return __expand ((dpgettext $textdomain => $msgctxt, $msgid), %vars);
293 }
294  
295 # Context + Plural.
296 sub __np ($$$$)
297 0 ▶{
Subroutine __np: not called CC=3 SCAR=24.8
298 0 my ($msgctxt, $msgid, $msgid_plural, $count) = @_;
299
300 0 my $package = caller;
301
302 0 my $textdomain = $textdomains{$package};
303
304 __find_domain $textdomain if
305 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
306
307 0 return dnpgettext $textdomain, $msgctxt, $msgid, $msgid_plural, $count;
308 }
309  
310 # Plural with interpolation.
311 sub __npx ($$$$@)
312 0 ▶{
Subroutine __npx: not called CC=3 SCAR=24.8
313 0 my ($msgctxt, $msgid, $msgid_plural, $count, %args) = @_;
314
315 0 my $package = caller;
316
317 0 my $textdomain = $textdomains{$package};
318
319 __find_domain $textdomain if
320 0 ▶ defined $textdomain && defined $bound_dirs{$textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $textdomain and defined $bound_dirs{$textdomain} 0 0 0
Condition0%0/3
defined $textdomain and defined $bound_dirs{$textdomain}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $textdomain and defined $bound_dirs{$textdomain}
defined $textdomain defined $bound_dirs{$textdomain}
Truth table0%0/3
A and B
A= defined $textdomainB= defined $bound_dirs{$textdomain}
ABresult
0X0
100
111
321
322 0 return __expand ((dnpgettext $textdomain, $msgctxt, $msgid, $msgid_plural, $count),
323 %args);
324 }
325  
326 # Dummy functions for string marking.
327 sub N__($)
328 2 ▶{
Subroutine N__: called CC=1 SCAR=0.0
Pod documented
329 2 return shift;
330 }
331  
332 sub N__n($$$)
333 1 ▶{
Subroutine N__n: called CC=1 SCAR=0.0
Pod documented
334 1 return @_;
335 }
336  
337 1 ▶sub N__p($$) {
Subroutine N__p: called CC=1 SCAR=0.0
Pod documented
338 1 return @_;
339 }
340  
341 1 ▶sub N__np($$$$) {
Subroutine N__np: called CC=1 SCAR=0.0
Pod documented
342 1 return @_;
343 }
344  
345 sub import
346 2 ▶{
Subroutine import: called CC=8 SCAR=22.0
347 2 my ($self, $textdomain, @search_dirs) = @_;
348
349 # Check our caller.
350 2 my $package = caller;
351 2 ▶ return if exists $textdomains{$package};
Branch: 1 branch
BranchTrueFalseTotal
if exists $textdomains{$package} 0 2 2
352
353 # Was a textdomain specified?
354 2 ▶ $textdomain = textdomain unless defined $textdomain && length $textdomain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $textdomain and length $textdomain 1 1 2
Condition66%2/3
defined $textdomain and length $textdomain
!ll&&!rl&&r
101
MC/DC50%1/2
defined $textdomain and length $textdomain
defined $textdomain length $textdomain
Truth table66%2/3
A and B
A= defined $textdomainB= length $textdomain
ABresult
0X0
100
111
355
356 # Remember the textdomain of that package.
357 2 $textdomains{$package} = $textdomain;
358
359 # Remember that we still have to bind that textdomain to
360 # a directory.
361 2 ▶ unless (exists $bound_dirs{$textdomain}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $bound_dirs{$textdomain}) 2 0 2
362 2 ▶ unless (@search_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@search_dirs) 2 0 2
363 2 ▶ @search_dirs = ((map $_ . '/LocaleData', @INC), @default_dirs)
Branch: 1 branch
BranchTrueFalseTotal
unless @search_dirs 2 0 2
364 unless @search_dirs;
365 2 ▶ if (my $share = eval {
Branch: 1 branch
BranchTrueFalseTotal
if (my $share = eval { do { require File::ShareDir; File::ShareDir::dist_dir($textdomain) } }) 0 2 2
366 2 require File::ShareDir;
367 2 File::ShareDir::dist_dir ($textdomain);
368 }) {
369 unshift @search_dirs,
370 0 map { "$share/$_" }
371 qw (locale LocaleData);
372 }
373 }
374 2 $bound_dirs{$textdomain} = [grep { -d $_ } @search_dirs];
375 }
376
377 2 Locale::TextDomain->export_to_level (1, $package, @EXPORT);
378
379 2 return;
380 }
381  
382 # Private functions.
383 sub __find_domain ($)
384 1 ▶{
Subroutine __find_domain: called CC=8 SCAR=21.5
385 1 my $domain = shift;
386
387 1 my $try_dirs = $bound_dirs{$domain};
388
389 1 ▶ if (defined $try_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try_dirs) 1 0 1
390 1 my $found_dir;
391
392 1 TRYDIR: foreach my $dir (grep { -d $_ } @$try_dirs) {
393 # Is there a message catalog? We have to search recursively
394 # for it. Since globbing is reported to be buggy under
395 # MS-DOS, we roll our own version.
396 1 local *DIR;
397 1 ▶ if (opendir DIR, $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $dir) 1 0 1
398 4 my @files = map { "$dir/$_/LC_MESSAGES/$domain.mo" }
399 1 grep { ! /^\.\.?$/ } readdir DIR;
400  
401 1 foreach my $file (@files) {
402 1 ▶ if (-f $file || -l $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file or -l $file) 1 0 1
Condition33%1/3
-f $file or -l $file
l!l&&r!l&&!r
100
MC/DC0%0/2
-f $file or -l $file
-f $file -l $file
Truth table33%1/3
A or B
A= -f $fileB= -l $file
ABresult
1X1
011
000
403 # If we find a non-readable file on our way,
404 # we access has been disabled on purpose.
405 # Therefore no -r check here.
406 1 $found_dir = $dir;
407 1 last TRYDIR;
408 }
409 }
410 }
411 }
412
413 # If there was no success, this will fall back to the default search
414 # directories.
415 1 ▶ bindtextdomain $domain => abs_path $found_dir if defined $found_dir;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found_dir 1 0 1
416 }
417
418 # The search has completed.
419 1 undef $bound_dirs{$domain};
420
421 1 return 1;
422 }
423  
424 sub __expand ($%)
425 0 ▶{
Subroutine __expand: not called CC=2 SCAR=17.9
426 0 my ($translation, %args) = @_;
427
428 0 my $re = join '|', map { quotemeta $_ } keys %args;
429 0 ▶ $translation =~ s/\{($re)\}/defined $args{$1} ? $args{$1} : "{$1}"/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$1} ? : 0 0 0
430
431 0 return $translation;
432 }
433  
434 3 1;
435  
436 __END__
« RecodeData/ISO_8859_1.pm ↑ Summary Recode.pm »