lib/Locale/Simple.pm

filter: Statementstmt 84.7% 128 / 151 Branchbran 92.6% 63 / 68 Conditioncond 78.7% 26 / 33 MC/DCmcdc 71.4% 15 / 21 Subroutinesub 72.0% 31 / 43 Podpod 56.0% 14 / 25 totaltotal 81.2% 277 / 341 CC 49 SCAR 19.9
CC
49
Cov
86%
_placeholder
17.0
ldnp
15.0
l_lang
8.1
CRAP 7.3
« lib/Locale/Simple/PO2JSON.pm ↑ Summary lib/Locale/Simple/CLI.pm »
1 package Locale::Simple;
2 # ABSTRACT: Short gettext helpers with one API for Perl, Python, JavaScript, PHP, Go and Rust
3 32 our $VERSION = '0.200';
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Locale::TextDomain 1.23 (); # to ensure we get the right version of gettext_dumb
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Locale::gettext_dumb qw(:locale_h :libintl_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use POSIX qw' setlocale ';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 our @EXPORT = qw(
13  
14 l_dir
15 l_lang
16 l_dry
17 l_nolocales
18  
19 l
20 ln
21 lp
22 lnp
23 ld
24 ldn
25 ldp
26 ldnp
27  
28 N_
29 Nn_
30 Np_
31 Nnp_
32 Nd_
33 Ndn_
34 Ndp_
35 Ndnp_
36  
37 ltd
38  
39 );
40  
41 # Deferred translation markers — no-op identity stubs that exist so the
42 # scraper can treat them as msgid sources while runtime just returns the
43 # arguments unchanged. Use them where strings must be declared before the
44 # viewer's locale is known (attribute defaults, DSLs, constants) and pass
45 # the stored msgid(s) through the matching l*() later.
46 2 ▶sub N_ { $_[0] }
Subroutine N_: called CC=1 SCAR=0.0
Pod undocumented
47 3 ▶sub Nn_ { wantarray ? @_[0,1] : $_[0] }
Subroutine Nn_: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
48 1 ▶sub Np_ { $_[1] }
Subroutine Np_: called CC=1 SCAR=0.0
Pod undocumented
49 2 ▶sub Nnp_ { wantarray ? @_[1,2] : $_[1] }
Subroutine Nnp_: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
50 1 ▶sub Nd_ { $_[1] }
Subroutine Nd_: called CC=1 SCAR=0.0
Pod undocumented
51 2 ▶sub Ndn_ { wantarray ? @_[1,2] : $_[1] }
Subroutine Ndn_: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
52 1 ▶sub Ndp_ { $_[2] }
Subroutine Ndp_: called CC=1 SCAR=0.0
Pod undocumented
53 2 ▶sub Ndnp_ { wantarray ? @_[2,3] : $_[2] }
Subroutine Ndnp_: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
54  
55 32 my $dry;
56 my $nowrite;
57 32 my $nolocales;
58  
59 32 my %tds;
60 32 my $dir;
61  
62 3 ▶sub l_nolocales { $nolocales = shift }
Subroutine l_nolocales: called CC=1 SCAR=0.0
Pod documented
63 10 ▶sub l_dry { $dry = shift; $nowrite = shift; $nolocales = _set($dry) ? 1 : 0 }
Subroutine l_dry: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_set($dry) ? : 6 4 10
64  
65 # A string counts as set when it is defined and not empty, "0" included.
66 401 ▶sub _set { defined $_[0] && length $_[0] }
Subroutine _set: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and length $_[0] 189 212 401
67  
68 550 ▶sub gettext_escape {
Subroutine gettext_escape: called CC=1 SCAR=0.0
Pod undocumented
69 550 my ( $content ) = @_;
70 550 $content =~ s/\\/\\\\/g;
71 550 $content =~ s/\n/\\n/g;
72 550 $content =~ s/\t/\\t/g;
73 550 $content =~ s/\r/\\r/g;
74 550 $content =~ s/"/\\"/g;
75 550 return $content;
76 }
77  
78 0 ▶sub sprintf_compare {
Subroutine sprintf_compare: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my ( $first, $second ) = @_;
80 0 my $re = qr/(?:%%|%(?:[0-9]+\$)?[+-]?(?:[ 0]|\'.)?-?[0-9]*(?:\.[0-9]+)?[bcdeufFosxX])/;
81 0 my @placeholder_first = sort { $a cmp $b } map { my @chars = split(//,$_); pop @chars; } ($first =~ m/$re/g);
82 0 my @placeholder_second = sort { $a cmp $b } map { my @chars = split(//,$_); pop @chars; } ($second =~ m/$re/g);
83 0 return join("",@placeholder_first) eq join("",@placeholder_second);
84 }
85  
86 # One placeholder of mo-reader.md §5: %%, or the N$ position, the flags (with
87 # '<char> as pad character), width, .precision and the conversion.
88 32 my $placeholder = qr{
89 ( % (?: % | (?: ([0-9]+) \$ )? ( (?: [-+\ 0\#] | '. )* ) ( \* | [0-9]+ )?
90 (?: \. ( \* | [0-9]* ) )? ( [bcdeEfFgGiosuxX] ) ) )
91 }xs;
92  
93 # One pass over the format: the text between placeholders is copied, and each
94 # placeholder is formatted on its own with CORE::sprintf and its own
95 # arguments, so an argument value is never read as a format. %2$s picks its
96 # argument, a placeholder without N$ takes the next one in order (a * width or
97 # precision too). A placeholder without an argument stays as it is; extra
98 # arguments are ignored. python/locale_simple.py formats the same way.
99 117 ▶sub _sprintf {
Subroutine _sprintf: called CC=1 SCAR=0.0
100 117 my ( $format, @args ) = @_;
101 117 my $next = 0;
102 117 $format =~ s{$placeholder}{_placeholder( \@args, \$next, $1, $2, $3, $4, $5, $6 )}ge;
103 117 return $format;
104 }
105  
106 118 ▶sub _placeholder {
Subroutine _placeholder: called CC=17 SCAR=28.4
107 118 my ( $args, $next, $all, $index, $flags, $width, $precision, $conversion ) = @_;
108 118 ▶ return '%' if $all eq '%%';
Branch: 1 branch
BranchTrueFalseTotal
if $all eq "%%" 4 114 118
109 114 my @picks;
110 114 for my $star ( $width, $precision ) {
111 228 ▶ push @picks, ${$next}++ if defined $star && $star eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if defined $star and $star eq "*" 6 222 228
Condition100%3/3
defined $star and $star eq "*"
!ll&&!rl&&r
195276
MC/DC100%2/2
defined $star and $star eq "*"
defined $star $star eq "*"
Truth table100%3/3
A and B
A= defined $starB= $star eq "*"
ABresult
0X0
100
111
112 }
113 114 ▶ push @picks, defined $index ? $index - 1 : ${$next}++;
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 20 94 114
114 114 ▶ return $all if grep { $_ < 0 || $_ > $#{$args} } @picks;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 0 or $_ > $#{$args;};} @picks 5 109 114
Condition66%2/3
$_ < 0 || $_ > $#{$args;}
l!l&&r!l&&!r
05115
MC/DC50%1/2
$_ < 0 || $_ > $#{$args;}
$_ < 0 $_ > $#{$args;}
Truth table66%2/3
A || B
A= $_ < 0B= $_ > $#{$args;}
ABresult
1X1
011
000
115 109 my @values = @{$args}[@picks];
116 109 ▶ my $tail = ( defined $precision ? '.'.$precision : '' ).$conversion;
Branch: 1 branch
BranchTrueFalseTotal
defined $precision ? : 11 98 109
117 109 my $pad;
118 109 ▶ $pad = $1 while $flags =~ s/'(.)//s;
Branch: 1 branch
BranchTrueFalseTotal
$flags =~ s/'(.)//s and do { $pad = $1 } 7 109 116
119 109 ▶ return CORE::sprintf( '%'.$flags.( defined $width ? $width : '' ).$tail, @values )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $pad and defined $width 103 6 109
defined $width ? : 15 88 103
Condition100%3/3
defined $pad and defined $width
!ll&&!rl&&r
10216
MC/DC100%2/2
defined $pad and defined $width
defined $pad defined $width
Truth table100%3/3
A and B
A= defined $padB= defined $width
ABresult
0X0
100
111
120 unless defined $pad && defined $width;
121 # '<char>: the formatted value is filled up to the width with that character.
122 6 ▶ my $size = $width eq '*' ? shift @values : $width;
Branch: 1 branch
BranchTrueFalseTotal
$width eq '*' ? : 1 5 6
123 6 ▶ my $left = $flags =~ tr/-// || $size < 0;
Condition66%2/3
$flags =~ tr/\-// || $size < 0
l!l&&r!l&&!r
204
MC/DC50%1/2
$flags =~ tr/\-// || $size < 0
$flags =~ tr/\-// $size < 0
Truth table66%2/3
A || B
A= $flags =~ tr/\-//B= $size < 0
ABresult
1X1
011
000
124 6 $flags =~ tr/-0//d;
125 6 my $body = CORE::sprintf( '%'.$flags.$tail, @values );
126 6 my $fill = abs($size) - length $body;
127 6 ▶ return $body if $fill <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fill <= 0 0 6 6
128 6 ▶ return $left ? $body.( $pad x $fill ) : ( $pad x $fill ).$body;
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 2 4 6
129 }
130  
131 0 ▶sub coderef_hash {{
Subroutine coderef_hash: not called CC=1 SCAR=6.9
Pod undocumented
132 0 ▶ l => sub { l(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ ln => sub { ln(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ lp => sub { lp(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ lnp => sub { lnp(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 ▶ ld => sub { ld(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 ▶ ldn => sub { ldn(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 ▶ ldp => sub { ldp(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 ▶ ldnp => sub { ldnp(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 }}
141  
142 8 ▶sub l_dir {
Subroutine l_dir: called CC=2 SCAR=6.9
Pod documented
143 8 $dir = shift;
144 8 _bind_textdomain($_) for keys %tds;
145 8 return $dir;
146 }
147  
148 # Binds a text domain to the l_dir() directory. ltd(), the domain helpers
149 # and l_dir() all bind through here, so every bound domain follows l_dir().
150 13 ▶sub _bind_textdomain {
Subroutine _bind_textdomain: called CC=1 SCAR=0.0
151 13 my ( $td ) = @_;
152 13 bindtextdomain($td,$dir);
153 13 bind_textdomain_codeset($td,'utf-8');
154 13 $tds{$td} = 1;
155 }
156  
157 5 ▶sub l_lang {
Subroutine l_lang: called CC=7 SCAR=20.9
Pod documented
158 5 my $primary = shift;
159 5 $ENV{LANGUAGE} = $primary;
160 5 $ENV{LANG} = $primary; # unsure if these ENV assignments are still needed with the setlocale below
161 5 $ENV{LC_ALL} = $primary;
162 5 $ENV{LC_MESSAGES} = $primary; # set locale for messages if the system supports it
163 # POSIX knows LC_MESSAGES only where the system has it; Locale::gettext_pp
164 # answers its own LC_MESSAGES with a stand-in number everywhere.
165 5 ▶ return unless defined eval { POSIX::LC_MESSAGES() };
Branch: 1 branch
BranchTrueFalseTotal
unless defined eval { do { 5 } } 1 4 5
166 4 setlocale( LC_MESSAGES, $primary );
167 # Under the C or POSIX messages locale gettext ignores LANGUAGE and
168 # translates nothing. Without an installed locale for $primary, move
169 # LC_MESSAGES to one that is not C, so the language set here still wins.
170 4 my $now = setlocale( LC_MESSAGES );
171 4 ▶ return if defined $now && $now ne 'C' && $now ne 'POSIX';
Branch: 1 branch
BranchTrueFalseTotal
if defined $now and $now ne "C" and $now ne "POSIX" 0 4 4
Condition33%2/6
defined $now and $now ne "C" and $now ne "POSIX"
!ll&&!rl&&r
400
defined $now and $now ne "C"
!ll&&!rl&&r
040
MC/DC0%0/3
defined $now and $now ne "C" and $now ne "POSIX"
defined $now $now ne "C" $now ne "POSIX"
Truth table25%1/4
A and B and C
A= defined $nowB= $now ne "C"C= $now ne "POSIX"
ABCresult
0XX0
10X0
1100
1111
172 4 for my $fallback ( 'C.UTF-8', 'en_US.UTF-8' ) {
173 4 ▶ return if setlocale( LC_MESSAGES, $fallback );
Branch: 1 branch
BranchTrueFalseTotal
if setlocale(&LC_MESSAGES(), $fallback) 4 0 4
174 }
175 0 return;
176 }
177  
178 # write dry: UTF-8 with LF line ends on every platform (:raw drops :crlf)
179 16 ▶sub wd {
Subroutine wd: called CC=3 SCAR=11.4
Pod documented
180 16 ▶ return if $nowrite;
Branch: 1 branch
BranchTrueFalseTotal
if $nowrite 0 16 16
181 16 ▶ open my $fh, '>>:raw:encoding(UTF-8)', $dry
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>:raw:encoding(UTF-8)", $dry or die 'cannot append to ' . $dry . ': ' . $! 16 0 16
182 or die 'cannot append to '.$dry.': '.$!;
183 16 print $fh join("\n",@_)."\n\n";
184 16 close $fh;
185 }
186  
187 # l(msgid,...)
188 91 ▶sub l { return ldnp('',undef,shift,undef,undef,@_) }
Subroutine l: called CC=1 SCAR=0.0
Pod documented
189 # ln(msgid,msgid_plural,count,...)
190 20 ▶sub ln { return ldnp('',undef,@_) }
Subroutine ln: called CC=1 SCAR=0.0
Pod documented
191 # lp(msgctxt,msgid,...)
192 3 ▶sub lp { return ldnp('',shift,shift,undef,undef,@_) }
Subroutine lp: called CC=1 SCAR=0.0
Pod documented
193 # lnp(msgctxt,msgid,msgid_plural,count,...)
194 0 ▶sub lnp { return ldnp('',shift,shift,shift,shift,@_) }
Subroutine lnp: not called CC=1 SCAR=6.9
Pod documented
195 # ld(domain,msgid,...)
196 5 ▶sub ld { return ldnp(shift,undef,shift,undef,undef,@_) }
Subroutine ld: called CC=1 SCAR=0.0
Pod documented
197 # ldn(domain,msgid,msgid_plural,count,...)
198 1 ▶sub ldn { return ldnp(shift,undef,shift,shift,shift,@_) }
Subroutine ldn: called CC=1 SCAR=0.0
Pod documented
199 # ldp(domain,msgctxt,msgid,...)
200 0 ▶sub ldp { return ldnp(shift,shift,shift,undef,undef,@_) }
Subroutine ldp: not called CC=1 SCAR=6.9
Pod documented
201 # ldnp(domain,msgctxt,msgid,msgid_plural,count,...)
202 121 ▶sub ldnp {
Subroutine ldnp: called CC=15 SCAR=27.1
Pod documented
203 121 my ($td, $ctxt, $id, $idp, $n) = (shift,shift,shift,shift,shift);
204 121 ▶ return $id if defined $id && $id eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id eq "" 2 119 121
Condition66%2/3
defined $id and $id eq ""
!ll&&!rl&&r
01192
MC/DC50%1/2
defined $id and $id eq ""
defined $id $id eq ""
Truth table66%2/3
A and B
A= defined $idB= $id eq ""
ABresult
0X0
100
111
205 119 ▶ die "please set a locale directory with l_dir() before using other translate functions" unless _set($dir) || $nolocales;
Branch: 1 branch
BranchTrueFalseTotal
unless _set($dir) or $nolocales 2 117 119
Condition100%3/3
_set($dir) or $nolocales
l!l&&r!l&&!r
33842
MC/DC100%2/2
_set($dir) or $nolocales
_set($dir) $nolocales
Truth table100%3/3
A or B
A= _set($dir)B= $nolocales
ABresult
1X1
011
000
206 117 my @args = @_;
207 117 ▶ unshift @args, $n if defined $idp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $idp 22 95 117
208 117 my $return;
209 117 ▶ if (_set($dry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_set($dry)) { } 20 97 117
210 20 ▶ if (!$nowrite) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nowrite) 16 4 20
211 16 my @save;
212 16 ▶ push @save, '# domain: '.$td if _set($td);
Branch: 1 branch
BranchTrueFalseTotal
if _set($td) 3 13 16
213 16 ▶ push @save, 'msgctxt "'.gettext_escape($ctxt).'"' if _set($ctxt);
Branch: 1 branch
BranchTrueFalseTotal
if _set($ctxt) 3 13 16
214 16 push @save, 'msgid "'.gettext_escape($id).'"';
215 16 ▶ push @save, 'msgid_plural "'.gettext_escape($idp).'"' if _set($idp);
Branch: 1 branch
BranchTrueFalseTotal
if _set($idp) 8 8 16
216 16 wd(@save);
217 }
218 20 ▶ $return = _sprintf(defined $idp && $n != 1 ? $idp : $id, @args);
Branch: 1 branch
BranchTrueFalseTotal
defined $idp && $n != 1 ? : 5 15 20
Condition100%3/3
defined $idp and $n != 1
!ll&&!rl&&r
1055
MC/DC100%2/2
defined $idp and $n != 1
defined $idp $n != 1
Truth table100%3/3
A and B
A= defined $idpB= $n != 1
ABresult
0X0
100
111
219 } else {
220 97 ▶ _bind_textdomain($td) if _set($td) && !$tds{$td};
Branch: 1 branch
BranchTrueFalseTotal
if _set($td) and not $tds{$td} 1 96 97
Condition100%3/3
_set($td) and not $tds{$td}
!ll&&!rl&&r
9331
MC/DC100%2/2
_set($td) and not $tds{$td}
_set($td) not $tds{$td}
Truth table100%3/3
A and not(B)
A= _set($td)B= not $tds{$td}
ABresult
0X0
100
111
221 # L::TD handles msg ids as bytes internally
222 97 utf8::encode($id);
223 97 my $gt = dnpgettext($td, $ctxt, $id, $idp, $n);
224 # Fixing bad utf8 handling
225 97 utf8::decode($gt);
226 97 $return = _sprintf($gt,@args);
227 }
228 117 return $return;
229 }
230  
231 10 ▶sub ltd {
Subroutine ltd: called CC=4 SCAR=13.9
Pod documented
232 10 ▶ die "please set a locale directory with l_dir() before using other translate functions" unless _set($dir) || $nolocales;
Branch: 1 branch
BranchTrueFalseTotal
unless _set($dir) or $nolocales 2 8 10
Condition100%3/3
_set($dir) or $nolocales
l!l&&r!l&&!r
622
MC/DC100%2/2
_set($dir) or $nolocales
_set($dir) $nolocales
Truth table100%3/3
A or B
A= _set($dir)B= $nolocales
ABresult
1X1
011
000
233 8 my $td = shift;
234 8 ▶ _bind_textdomain($td) unless $tds{$td};
Branch: 1 branch
BranchTrueFalseTotal
unless $tds{$td} 7 1 8
235 8 textdomain($td);
236 }
237  
238 32 1;
239  
240 __END__
« lib/Locale/Simple/PO2JSON.pm ↑ Summary lib/Locale/Simple/CLI.pm »