MuForm/Localizer.pm

filter: Statementstmt 75.7% 159 / 210 Branchbran 51.3% 75 / 146 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 15 totaltotal 64.5% 258 / 400 SCAR 57.1
CC
75
Cov
66%
load_file
337.2
_calculate_multiplural_index
20.0
translate
9.2
CRAP 301.2
↑ Summary MuForm/Field.pm »
1 package Data::MuForm::Localizer;
2 # ABSTRACT: Localizer
3 95 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 95 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 95 has 'language' => ( is => 'rw', builder => 'build_language' );
9 0 ▶sub build_language { 'en' }
Subroutine build_language: not called CC=1 SCAR=6.9
Pod undocumented
10  
11 95 has 'messages_directory' => ( is => 'rw' );
12  
13 sub loc_ {
14 259 ▶ my ($self, $msgid) = @_;
Subroutine loc_: called CC=1 SCAR=0.0
Pod undocumented
15 # translate($self, $msgctxt, $msgid, $msgid_plural, $count, $is_n)
16 259 return $self->translate(undef, $msgid);
17 }
18  
19 sub loc_x {
20 41 ▶ my ($self, $msgid, %args) = @_;
Subroutine loc_x: called CC=1 SCAR=0.0
Pod undocumented
21 # translate($self, $msgctxt, $msgid, $msgid_plural, $count, $is_n)
22 41 my $msg = $self->translate(undef, $msgid);
23 41 my $out = $self->expand_named( $msg, %args );
24 41 return $out;
25 }
26  
27 sub loc_nx {
28 4 ▶ my ($self, $msgid, $msgid_plural, $count, %args) = @_;
Subroutine loc_nx: called CC=1 SCAR=0.0
Pod undocumented
29 # translate($self, $msgctxt, $msgid, $msgid_plural, $count, $is_n)
30 4 my $msg = $self->translate(undef, $msgid, $msgid_plural, $count, 1);
31 4 my $out = $self->expand_named( $msg, %args );
32 4 return $out;
33 }
34  
35  
36 sub loc_npx {
37 0 ▶ my ($self, $msgctxt, $msgid, $msgid_plural, $count, %args) = @_;
Subroutine loc_npx: not called CC=1 SCAR=6.9
Pod undocumented
38 # my ($self, $msgctxt, $msgid, $msgid_plural, $count, @args) = @_;
39 0 my $msg = $self->translate($msgctxt, $msgid, $msgid_plural, $count, 1);
40 0 my $out = $self->expand_named( $msg, %args );
41 0 return $out;
42 }
43  
44 95 our $lexicons = {
45 };
46  
47 sub get_lexicon {
48 305 ▶ my $self = shift;
Subroutine get_lexicon: called CC=2 SCAR=6.9
Pod undocumented
49 305 my $lang = $self->language;
50 305 ▶ if ( ! exists $lexicons->{$lang} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Data::MuForm::Localizer::lexicons->{$lang}) 41 264 305
51 41 $lexicons->{$lang} = $self->load_lexicon($lang);
52 }
53 305 return $lexicons->{$lang};
54 }
55  
56 sub load_lexicon {
57 41 ▶ my ( $self, $lang ) = @_;
Subroutine load_lexicon: called CC=1 SCAR=0.0
Pod undocumented
58  
59 41 my $file = $self->module_path;
60 41 $file =~ s/Localizer.pm//;
61 41 $file .= "Messages/$lang.po";
62 41 my $lexicon = $self->load_file($file);
63 41 return $lexicon;
64 }
65  
66 sub load_file {
67 41 ▶ my ( $self, $file ) = @_;
Subroutine load_file: called CC=51 SCAR=58.2
Pod undocumented
68  
69 41 ▶ open(IN, "<:encoding(UTF-8)", $file)
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:encoding(UTF-8)", $file or return undef 0 41 41
70 or return undef;
71  
72 41 my @entries;
73 my %entries;
74 41 my $index;
75 41 my $po;
76 41 my %buffer;
77 41 my $last_buffer;
78 41 my $line_number = 0;
79 41 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 8774 41 8815
80 8774 chomp;
81 8774 $line_number++;
82  
83 # Strip trailing \r\n chars
84 8774 s{[\r\n]*$}{};
85  
86 8774 ▶ if (/^$/) {
Branch: 14 branches
BranchTrueFalseTotal
if (/^$/) { } 2091 6683 8774
elsif (/^#\s+(.*)/ or /^#()$/) { } 2050 4633 6683
elsif (/^#\.\s*(.*)/) { } 0 4633 4633
elsif (/^#:\s+(.*)/) { } 0 4633 4633
elsif (/^#,\s+(.*)/) { } 0 4633 4633
elsif (/^#(~)?\|\s+msgctxt\s+(.*)/) { } 0 4633 4633
elsif (/^#(~)?\|\s+msgid\s+(.*)/) { } 0 4633 4633
elsif (/^#(~)?\|\s+msgid_plural\s+(.*)/) { } 0 4633 4633
elsif (/^(#~\s+)?msgctxt\s+(.*)/) { } 0 4633 4633
elsif (/^(#~\s+)?msgid\s+(.*)/) { } 2091 2542 4633
elsif (/^(#~\s+)?msgid_plural\s+(.*)/) { } 41 2501 2542
elsif (/^(?:#~\s+)?msgstr\s+(.*)/) { } 2050 451 2501
elsif (/^(?:#~\s+)?msgstr\[(\d+)\]\s+(.*)/) { } 82 369 451
elsif (/^(?:#(?:~|~\||\|)\s+)?(".*)/) { } 369 0 369
87  
88 # Empty line. End of an entry.
89  
90 2091 ▶ if (defined($po)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po) 2091 0 2091
91 $po->{fuzzy_msgctxt} = $buffer{fuzzy_msgctxt}
92 2091 ▶ if defined $buffer{fuzzy_msgctxt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"fuzzy_msgctxt"} 0 2091 2091
93 2091 ▶ $po->{fuzzy_msgid} = $buffer{fuzzy_msgid} if defined $buffer{fuzzy_msgid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"fuzzy_msgid"} 0 2091 2091
94 $po->{fuzzy_msgid_plural} = $buffer{fuzzy_msgid_plural}
95 2091 ▶ if defined $buffer{fuzzy_msgid_plural};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"fuzzy_msgid_plural"} 0 2091 2091
96 2091 ▶ $po->{msgctxt} = $buffer{msgctxt} if defined $buffer{msgctxt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgctxt"} 0 2091 2091
97 2091 ▶ $po->{msgid} = $buffer{msgid} if defined $buffer{msgid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgid"} 2091 0 2091
98 2091 ▶ $po->{msgid_plural} = $buffer{msgid_plural} if defined $buffer{msgid_plural};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgid_plural"} 41 2050 2091
99 2091 ▶ $po->{msgstr} = $buffer{msgstr} if defined $buffer{msgstr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgstr"} 2050 41 2091
100 2091 ▶ $po->{msgstr_n} = $buffer{msgstr_n} if defined $buffer{msgstr_n};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgstr_n"} 41 2050 2091
101  
102 # Save this message
103 2091 $entries{$po->{msgid}} = $po;
104 2091 push @entries, $po;
105 2091 my $index_key = join_message_key( msgid => $po->{msgid}, msgstr => $po->{msgstr}, msgctxt => $po->{msgctxt});
106 2091 $index->{$index_key} = $po;
107  
108 2091 $po = undef;
109 2091 $last_buffer = undef;
110 2091 %buffer = ();
111 }
112 }
113 ▶ elsif (/^#\s+(.*)/ or /^#()$/) {
Condition66%2/3
/^#\s+(.*)/ or /^#()$/
l!l&&r!l&&!r
205004633
MC/DC50%1/2
/^#\s+(.*)/ or /^#()$/
/^#\s+(.*)/ /^#()$/
Truth table66%2/3
A or B
A= /^#\s+(.*)/B= /^#()$/
ABresult
1X1
011
000
114  
115 # Translator comments
116 2050 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 2050 0 2050
117 2050 ▶ if (defined($po->{comment})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{'comment'}) { } 0 2050 2050
118 0 $po->{comment} = $po->{comment} . "\n$1";
119 }
120 else {
121 2050 $po->{comment} = $1;
122 }
123 }
124 elsif (/^#\.\s*(.*)/) {
125  
126 # Automatic comments
127 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
128 0 ▶ if (defined($po->{automatic})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{'automatic'}) { } 0 0 0
129 0 $po->{automatic} = $po->automatic . "\n$1";
130 }
131 else {
132 0 $po->{automatic} = $1;
133 }
134 }
135 elsif (/^#:\s+(.*)/) {
136  
137 # reference
138 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
139 0 ▶ if (defined($po->{reference})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{'reference'}) { } 0 0 0
140 0 $po->{reference} = $po->reference . "\n$1";
141 }
142 else {
143 0 $po->{reference} = $1;
144 }
145 }
146 elsif (/^#,\s+(.*)/) {
147  
148 # flags
149 0 my @flags = split /\s*[,]\s*/, $1;
150 # $po = { line_number => $line_number } unless defined($po);
151 # foreach my $flag (@flags) {
152 # $po->add_flag($flag);
153 # }
154 }
155 elsif (/^#(~)?\|\s+msgctxt\s+(.*)/) {
156 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
157 0 $buffer{fuzzy_msgctxt} = $self->dequote($2);
158 0 $last_buffer = \$buffer{fuzzy_msgctxt};
159 0 ▶ $po->{obsolete} = 1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
160 }
161 elsif (/^#(~)?\|\s+msgid\s+(.*)/) {
162 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
163 0 $buffer{fuzzy_msgid} = $self->dequote($2);
164 0 $last_buffer = \$buffer{fuzzy_msgid};
165 0 ▶ $po->{obsolete} = 1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
166 }
167 elsif (/^#(~)?\|\s+msgid_plural\s+(.*)/) {
168 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
169 0 $buffer{fuzzy_msgid_plural} = $self->dequote($2);
170 0 $last_buffer = \$buffer{fuzzy_msgid_plural};
171 0 ▶ $po->{obsolete} = 1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
172 }
173 elsif (/^(#~\s+)?msgctxt\s+(.*)/) {
174 0 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 0 0
175 0 $buffer{msgctxt} = $self->dequote($2);
176 0 $last_buffer = \$buffer{msgctxt};
177 0 ▶ $po->obsolete(1) if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
178 }
179 elsif (/^(#~\s+)?msgid\s+(.*)/) {
180 2091 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 41 2050 2091
181 2091 $buffer{msgid} = $self->dequote($2);
182 2091 $last_buffer = \$buffer{msgid};
183 2091 ▶ $po->obsolete(1) if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 2091 2091
184 }
185 elsif (/^(#~\s+)?msgid_plural\s+(.*)/) {
186 41 ▶ $po = { line_number => $line_number } unless defined($po);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po 0 41 41
187 41 $buffer{msgid_plural} = $self->dequote($2);
188 41 $last_buffer = \$buffer{msgid_plural};
189 41 ▶ $po->obsolete(1) if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 41 41
190 }
191 elsif (/^(?:#~\s+)?msgstr\s+(.*)/) {
192  
193 # translated string
194 2050 $buffer{msgstr} = $self->dequote($1);
195 2050 $last_buffer = \$buffer{msgstr};
196 }
197 elsif (/^(?:#~\s+)?msgstr\[(\d+)\]\s+(.*)/) {
198  
199 # translated string
200 82 $buffer{msgstr_n}{$1} = $self->dequote($2);
201 82 $last_buffer = \$buffer{msgstr_n}{$1};
202 }
203 elsif (/^(?:#(?:~|~\||\|)\s+)?(".*)/) {
204  
205 # continued string. Accounts for:
206 # normal : "string"
207 # obsolete : #~ "string"
208 # fuzzy : #| "string"
209 # fuzzy+obsolete : #~| "string"
210 369 $$last_buffer .= $self->dequote($1);
211 }
212 else {
213 0 warn "Strange line at $file line $line_number: [$_]\n";
214 }
215 }
216 41 ▶ if (defined($po)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po) 0 41 41
217  
218 $po->{msgctxt} = $buffer{msgctxt}
219 0 ▶ if defined $buffer{msgctxt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgctxt"} 0 0 0
220 $po->{msgid} = $buffer{msgid}
221 0 ▶ if defined $buffer{msgid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgid"} 0 0 0
222 $po->{msgid_plural} = $buffer{msgid_plural}
223 0 ▶ if defined $buffer{msgid_plural};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgid_plural"} 0 0 0
224 $po->{msgstr} = $buffer{msgstr}
225 0 ▶ if defined $buffer{msgstr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgstr"} 0 0 0
226 $po->{msgstr_n} = $buffer{msgstr_n}
227 0 ▶ if defined $buffer{msgstr_n};
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer{"msgstr_n"} 0 0 0
228  
229 # save messages
230 0 $entries{$po->{msgid}} = $po;
231 0 push @entries, $po;
232 0 my $index_key = join_message_key( msgid => $po->{msgid}, msgstr => $po->{msgstr}, msgctxt => $po->{msgctxt});
233 0 $index->{$index_key} = $po;
234  
235 }
236 41 close IN;
237  
238 # the first entry is the header. Extract information from the header msgstr
239 41 my $header_ref = $entries[0];
240 41 %{$header_ref} = (
241 msgid => $header_ref->{msgid},
242 41 %{ $self->extract_header_msgstr( $header_ref->{msgstr} ) },
243 );
244  
245 41 return $index;
246 }
247  
248 sub dequote {
249 4633 ▶ my ( $self, $string ) = @_;
Subroutine dequote: called CC=2 SCAR=6.9
Pod undocumented
250  
251 return undef
252 4633 ▶ unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 4633 4633
253  
254 4633 $string =~ s/^"(.*)"/$1/;
255 4633 $string =~ s/\\"/"/g;
256 4633 $string =~ s/(?<!(\\))\\n/\n/g; # newline
257 4633 $string =~ s/(?<!(\\))\\{2}n/\\n/g; # inline newline
258 4633 $string =~ s/(?<!(\\))\\{3}n/\\\n/g; # \ followed by newline
259 4633 $string =~ s/\\{4}n/\\\\n/g; # \ followed by inline newline
260 4633 $string =~ s/\\\\(?!n)/\\/g; # all slashes not related to a newline
261 4633 return $string;
262 }
263  
264 sub expand_named {
265 45 ▶ my ($self, $text, @args) = @_;
Subroutine expand_named: called CC=4 SCAR=14.2
Pod undocumented
266  
267 45 ▶ defined $text
Branch: 1 branch
BranchTrueFalseTotal
defined $text or return $text 0 45 45
268 or return $text;
269 45 ▶ my $arg_ref = @args == 1
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 ? : 0 45 45
@args % 2 ? : 0 45 45
270 ? $args[0]
271 : {
272 @args % 2
273 ? die 'MuForm error Arguments expected pairwise'
274 : @args
275 };
276  
277 45 my $regex = join q{|}, map { quotemeta $_ } keys %{$arg_ref};
278  
279 45 $text =~ s{
280 (
281 \{
282 ( $regex )
283 (?: [ ]* [:] ( [^\}]+ ) )?
284 \}
285 )
286 }
287 {
288 55 $self->_mangle_value($1, $arg_ref->{$2}, $3)
289 }xmsge;
290 ## use critic (EscapedMetacharacters)
291  
292 45 return $text;
293 }
294  
295 sub _mangle_value {
296 55 ▶ my ($self, $placeholder, $value, $attribute) = @_;
Subroutine _mangle_value: called CC=3 SCAR=11.0
297  
298 55 ▶ defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value or return "" 0 55 55
299 or return q{};
300 55 ▶ defined $attribute
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute or return $value 53 2 55
301 or return $value;
302 2 return $value;
303 }
304  
305  
306 sub translate {
307 304 ▶ my ($self, $msgctxt, $msgid, $msgid_plural, $count, $is_n) = @_;
Subroutine translate: called CC=9 SCAR=22.2
Pod undocumented
308  
309 304 my $lexicon = $self->get_lexicon;
310  
311 304 my $msg_key = join_message_key(
312 msgctxt => $msgctxt,
313 msgid => $msgid,
314 msgid_plural => $msgid_plural,
315 );
316  
317 304 ▶ if ( $is_n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_n) 4 300 304
318 my $plural_code = $lexicon->{ q{} }->{plural_code}
319 4 ▶ or die qq{Plural-Forms not found in lexicon};
Branch: 1 branch
BranchTrueFalseTotal
my $plural_code = $lexicon->{""}{"plural_code"} or die "Plural-Forms not found in lexicon" 0 4 4
320 4 ▶ my $multiplural_index = ref $count eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $count eq 'ARRAY' ? : 0 4 4
321 ? $self->_calculate_multiplural_index($count, $plural_code, $lexicon )
322 : $plural_code->($count);
323 my $msgstr_plural = exists $lexicon->{$msg_key}
324 4 ▶ ? $lexicon->{$msg_key}->{msgstr_plural}->[$multiplural_index] : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $lexicon->{$msg_key} ? : 0 4 4
325 4 ▶ if ( ! defined $msgstr_plural ) { # fallback
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msgstr_plural) 4 0 4
326 4 ▶ $msgstr_plural = $plural_code->($count)
Branch: 1 branch
BranchTrueFalseTotal
&$plural_code($count) ? : 3 1 4
327 ? $msgid_plural
328 : $msgid;
329 }
330 4 return $msgstr_plural;
331 }
332  
333 my $msgstr = exists $lexicon->{$msg_key}
334 ? $lexicon->{$msg_key}->{msgstr}
335 300 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $lexicon->{$msg_key} ? : 61 239 300
336 300 ▶ if ( ! defined $msgstr ) { # fallback
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msgstr) 239 61 300
337 239 $msgstr = $msgid;
338 }
339  
340 300 return $msgstr;
341 }
342  
343 sub length_or_empty_list {
344 7185 ▶ my $thing = shift;
Subroutine length_or_empty_list: called CC=3 SCAR=11.0
Pod undocumented
345 7185 ▶ defined $thing or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $thing or return 4786 2399 7185
346 2399 ▶ length $thing or return;
Branch: 1 branch
BranchTrueFalseTotal
length $thing or return 41 2358 2399
347 2358 return $thing;
348 }
349  
350 sub _calculate_multiplural_index {
351 0 ▶ my ($self, $count_ref, $plural_code, $lexicon) = @_;
Subroutine _calculate_multiplural_index: not called CC=4 SCAR=30.0
352  
353 my $nplurals = $lexicon->{ q{} }->{multiplural_nplurals}
354 0 ▶ or die qq{X-Multiplural-Nplurals not found in lexicon"};
Branch: 1 branch
BranchTrueFalseTotal
my $nplurals = $lexicon->{""}{"multiplural_nplurals"} or die "X-Multiplural-Nplurals not found in lexicon\"" 0 0 0
355 0 ▶ my @counts = @{$count_ref}
Branch: 1 branch
BranchTrueFalseTotal
my(@counts) = @{$count_ref;} or die "Count array is empty" 0 0 0
356 or die 'Count array is empty';
357 0 my $index = 0;
358 0 ▶ while (@counts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@counts) 0 0 0
359 0 $index *= $nplurals;
360 0 my $count = shift @counts;
361 0 $index += $plural_code->($count);
362 }
363  
364 0 return $index;
365 }
366  
367 sub join_message_key {
368 2395 ▶ my ( %args ) = @_;
Subroutine join_message_key: called CC=1 SCAR=0.0
Pod undocumented
369  
370 my $key = join( '{MSG}',
371 (
372 join( '{PL}',
373 length_or_empty_list( $args{msgid} ),
374 length_or_empty_list( $args{msgid_plural} ),
375 )
376 ),
377 length_or_empty_list( $args{msgctxt} )
378 2395 );
379 }
380  
381 #====== Header Extract =======
382 my $perlify_plural_forms_ref__code_ref = sub {
383 84 ▶ my $plural_forms_ref = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
384  
385 84 ${$plural_forms_ref} =~ s{ \b ( nplurals | plural | n ) \b }{\$$1}xmsg;
386  
387 84 return;
388 95 };
389  
390 my $nplurals__code_ref = sub {
391 42 ▶ my $plural_forms = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
392  
393 42 $perlify_plural_forms_ref__code_ref->(\$plural_forms);
394 42 my $code = <<"EOC";
395 my \$n = 0;
396 my (\$nplurals, \$plural);
397 $plural_forms;
398 \$nplurals;
399 EOC
400 42 my $nplurals = eval $code;
401  
402 42 return $nplurals;
403 95 };
404  
405 my $plural__code_ref = sub {
406 42 ▶ my $plural_forms = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
407  
408 42 return $plural_forms =~ m{ \b plural= ( [^;\n]+ ) }xms;
409 95 };
410  
411 my $plural_code__code_ref = sub {
412 42 ▶ my $plural_forms = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
413  
414 42 $perlify_plural_forms_ref__code_ref->(\$plural_forms);
415 42 my $code = <<"EOC";
416 sub {
417 my \$n = shift;
418  
419 my (\$nplurals, \$plural);
420 $plural_forms;
421  
422 return 0 + \$plural;
423 }
424 EOC
425 42 my $code_ref = eval $code;
426  
427 42 return $code_ref;
428 95 };
429  
430 sub extract_header_msgstr {
431 45 ▶ my ( $class, $header_msgstr ) = @_;
Subroutine extract_header_msgstr: called CC=5 SCAR=16.1
Pod undocumented
432  
433 45 ▶ defined $header_msgstr
Branch: 1 branch
BranchTrueFalseTotal
defined $header_msgstr or die "Header is not defined" 1 44 45
434 or die 'Header is not defined';
435 ## no critic (ComplexRegexes)
436 44 ▶ my ( $plural_forms ) = $header_msgstr =~ m{
Branch: 1 branch
BranchTrueFalseTotal
my($plural_forms) = $header_msgstr =~ / ^ Plural-Forms: [ ]* ( nplurals [ ]* [=] [ ]* \d+ [ ]* [;] [ ]* plural [ ]* [=] [ ]* [^;\n]+ [ ]* [;]? [ ]* ) $ /msx or die "Plural-Forms not found in header" 1 43 44
437 ^
438 Plural-Forms:
439 [ ]*
440 (
441 nplurals [ ]* [=] [ ]* \d+ [ ]* [;]
442 [ ]*
443 plural [ ]* [=] [ ]* [^;\n]+ [ ]* [;]?
444 [ ]*
445 )
446 $
447 }xms
448 or die 'Plural-Forms not found in header';
449 ## use critic (ComplexRegexes)
450 43 ▶ my ( $charset ) = $header_msgstr =~ m{
Branch: 1 branch
BranchTrueFalseTotal
my($charset) = $header_msgstr =~ / ^ Content-Type: [^;]+ [;] [ ]* charset [ ]* = [ ]* ( [^ ]+ ) [ ]* $ /msx or die "Content-Type with charset not found in header" 1 42 43
451 ^
452 Content-Type:
453 [^;]+ [;] [ ]*
454 charset [ ]* = [ ]*
455 ( [^ ]+ )
456 [ ]*
457 $
458 }xms
459 or die 'Content-Type with charset not found in header';
460 42 my ( $multiplural_nplurals ) = $header_msgstr =~ m{
461 ^ X-Multiplural-Nplurals: [ ]* ( \d+ ) [ ]* $
462 }xms;
463  
464 return {(
465 42 ▶ nplurals => $nplurals__code_ref->($plural_forms),
Branch: 1 branch
BranchTrueFalseTotal
$multiplural_nplurals ? : 0 42 42
466 plural => $plural__code_ref->($plural_forms),
467 plural_code => $plural_code__code_ref->($plural_forms),
468 charset => $charset,
469 (
470 $multiplural_nplurals
471 ? ( multiplural_nplurals => $multiplural_nplurals )
472 : ()
473 ),
474 )};
475 }
476  
477 sub module_path {
478 42 ▶ (my $filename = __PACKAGE__ ) =~ s#::#/#g;
Subroutine module_path: called CC=1 SCAR=0.0
Pod undocumented
479 42 $filename .= '.pm';
480 42 my $path = $INC{$filename};
481 42 return $path;
482 }
483  
484  
485 95 1;
486  
487 __END__
488  
489 =pod
490  
491 =encoding UTF-8
492  
493 =head1 NAME
494  
495 Data::MuForm::Localizer - Localizer
496  
497 =head1 VERSION
498  
499 version 0.04
500  
501 =head1 DESCRIPTION
502  
503 Localizer for Data::MuForm
504  
505 This code has mainly been borrowed from Locale::TextDomain::OO.
506 It requires UTF-8 and handles only .po files. It does not use
507 the various LOCALE flags for languages. Language must be set
508 on creation of the form. It does handle plurals.
509  
510 TODO: implement allowing specification of user-provided messages
511 file, and merging two message files
512  
513 sub loc_px {
514 my ($self, $msgid, %args) = @_;
515  
516 return $msgid;
517 }
518  
519 =head1 NAME
520  
521 Data::MuForm::Localizer
522  
523 =head1 AUTHOR
524  
525 Gerda Shank
526  
527 =head1 COPYRIGHT AND LICENSE
528  
529 This software is copyright (c) 2017 by Gerda Shank.
530  
531 This is free software; you can redistribute it and/or modify it under
532 the same terms as the Perl 5 programming language system itself.
533  
534 =cut
↑ Summary MuForm/Field.pm »