OO/Translator.pm

filter: Statementstmt 92.3% 108 / 117 Branchbran 67.1% 51 / 76 Conditioncond 52.6% 10 / 19 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 89.4% 17 / 19 Podpod 100% 3 / 3 totaltotal 78.4% 193 / 246 CC 47 SCAR 30.9
CC
47
Cov
80%
translate
43.5
load_plugins
10.3
__ANON__
6.0
CRAP 22.1
↑ Summary OO/Lexicon/Role/StoreFile.pm »
1 package Locale::TextDomain::OO::Translator; ## no critic (TidyCode)
2
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use Class::Load qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Locale::TextDomain::OO::Singleton::Lexicon;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Locale::TextDomain::OO::Util::JoinSplitLexiconKeys;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use MooX::Types::MooseLike::Base qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 34 our $VERSION = '1.035';
15
16 34 with qw(
17 Locale::TextDomain::OO::Role::Logger
18 );
19
20 34 my $loaded_plugins;
21 36 ▶sub load_plugins {
Subroutine load_plugins: called CC=10 SCAR=23.3
Pod documented
22 36 my ( $class, @args ) = @_;
23
24 0 ▶ my %arg_of = @args == 1 ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 36 36
25 36 my $plugins = delete $arg_of{plugins};
26 36 ▶ if ( $plugins ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugins) 33 3 36
27 33 ▶ ref $plugins eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $plugins eq "ARRAY" or confess("Attribute plugins expected as ArrayRef") 33 0 33
28 or confess 'Attribute plugins expected as ArrayRef';
29
30 33 my $current_plugins = join ', ', sort @{$plugins};
31 33 ▶ if ( defined $loaded_plugins ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loaded_plugins) { } 2 31 33
32 2 ▶ length $current_plugins
Branch: 1 branch
BranchTrueFalseTotal
length $current_plugins and $loaded_plugins ne $current_plugins and confess("Too late to load plugins $current_plugins.", " Another method new was called before with plugins $loaded_plugins") 1 1 2
Condition66%2/3
length $current_plugins and $loaded_plugins ne $current_plugins
!ll&&!rl&&r
011
MC/DC50%1/2
length $current_plugins and $loaded_plugins ne $current_plugins
length $current_plugins $loaded_plugins ne $current_plugins
Truth table66%2/3
A and B
A= length $current_pluginsB= $loaded_plugins ne $current_plugins
ABresult
0X0
100
111
33 and $loaded_plugins ne $current_plugins
34 and confess
35 "Too late to load plugins $current_plugins.",
36 " Another method new was called before with plugins $loaded_plugins";
37 }
38 else {
39 31 for my $plugin ( @{$plugins} ) {
40 34 ▶ my $package = ( 0 == index $plugin, q{+} )
Branch: 1 branch
BranchTrueFalseTotal
0 == index($plugin, '+') ? : 0 34 34
41 ? $plugin
42 : "Locale::TextDomain::OO::Plugin::$plugin";
43 34 with $package;
44 }
45 31 $loaded_plugins = $current_plugins;
46 }
47 }
48 35 ▶ if ( ! defined $loaded_plugins ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loaded_plugins) 2 33 35
49 2 $loaded_plugins = q{};
50 }
51
52 35 return \%arg_of;
53 }
54
55 34 has language => (
56 is => 'rw',
57 isa => Str,
58 default => 'i-default',
59 );
60
61 34 has [ qw( category domain ) ] => (
62 is => 'rw',
63 isa => Str,
64 default => q{},
65 );
66
67 has project => (
68 is => 'rw',
69 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my $project = shift;
71 0 ▶ defined $project
Branch: 1 branch
BranchTrueFalseTotal
defined $project or return 0 0 0
72 or return;
73 0 return Str->($project);
74 },
75 34 );
76
77 has filter => (
78 is => 'rw',
79 3 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
80 3 my $arg = shift;
81 # Undef
82 3 ▶ defined $arg
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or return 2 1 3
83 or return;
84 # CodeRef
85 2 ▶ ref $arg eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq "CODE" and return 2 0 2
86 and return;
87 0 confess "$arg is not Undef or CodeRef";
88 },
89 34 );
90
91 9 ▶sub _calculate_multiplural_index {
Subroutine _calculate_multiplural_index: called CC=4 SCAR=13.9
92 9 my ($self, $count_ref, $plural_code, $lexicon, $lexicon_key) = @_;
93
94 my $nplurals = $lexicon->{ q{} }->{multiplural_nplurals}
95 9 ▶ or confess qq{X-Multiplural-Nplurals not found in lexicon "$lexicon_key"};
Branch: 1 branch
BranchTrueFalseTotal
my $nplurals = $lexicon->{""}{"multiplural_nplurals"} or confess(qq[X-Multiplural-Nplurals not found in lexicon "$lexicon_key"]) 9 0 9
96 9 ▶ my @counts = @{$count_ref}
Branch: 1 branch
BranchTrueFalseTotal
my(@counts) = @{$count_ref;} or confess("Count array is empty") 9 0 9
97 or confess 'Count array is empty';
98 9 my $index = 0;
99 9 ▶ while (@counts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@counts) 18 9 27
100 18 $index *= $nplurals;
101 18 my $count = shift @counts;
102 18 $index += $plural_code->($count);
103 }
104
105 9 return $index;
106 }
107
108 # The reason we need that here is "gettext_to_maketext => 1" during load lexicon.
109 # That escaps all [ ] before it is changing %1 to [_1] or similar.
110 # And so all none gettext strings are also involved.
111 60 ▶my $escape_maketext = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
112 60 my $string = shift;
113
114 60 ▶ defined $string
Branch: 1 branch
BranchTrueFalseTotal
defined $string or return $string 27 33 60
115 or return $string;
116 27 $string =~ s{ ( [\[\]] ) }{~$1}xmsg;
117
118 27 return $string;
119 34 };
120 0 ▶my $unescape_maketext = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
121 0 my $string = shift;
122
123 0 ▶ defined $string
Branch: 1 branch
BranchTrueFalseTotal
defined $string or return $string 0 0 0
124 or return $string;
125 0 $string =~ s{ [~] ( [\[\]] ) }{$1}xmsg;
126
127 0 return $string;
128 34 };
129
130 162 ▶sub translate { ## no critic (ExcessComplexity ManyArgs)
Subroutine translate: called CC=29 SCAR=37.7
Pod documented
131 162 my ($self, $msgctxt, $msgid, $msgid_plural, $count, $is_n, $plural_callback) = @_;
132
133 162 my $key_util = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys->instance;
134 my $lexicon_key = $key_util->join_lexicon_key({(
135 map {
136 162 $_ => $self->$_;
137 }
138 qw( language category domain project )
139 )});
140 162 my $lexicon = Locale::TextDomain::OO::Singleton::Lexicon->instance->data;
141 $lexicon = exists $lexicon->{$lexicon_key}
142 162 ▶ ? $lexicon->{$lexicon_key}
Branch: 1 branch
BranchTrueFalseTotal
exists $lexicon->{$lexicon_key} ? : 158 4 162
143 : ();
144 162 my $ext_lexicon = do {
145 162 my $lexicon_class = $lexicon->{ q{} }->{lexicon_class};
146 162 ▶ $lexicon_class ? load_class($lexicon_class)->instance : ();
Branch: 1 branch
BranchTrueFalseTotal
$lexicon_class ? : 0 162 162
147 };
148
149 162 my $msg_key = $key_util->join_message_key({
150 msgctxt => $msgctxt,
151 msgid => $msgid,
152 msgid_plural => $msgid_plural,
153 });
154 20 ▶ my $maketext_msg_key = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 20 return $key_util->join_message_key({
156 msgctxt => $escape_maketext->($msgctxt),
157 msgid => $escape_maketext->($msgid),
158 msgid_plural => $escape_maketext->($msgid_plural),
159 });
160 162 };
161 my $msg_ref
162 = exists $lexicon->{$msg_key}
163 ? $lexicon->{$msg_key}
164 : exists $lexicon->{ $maketext_msg_key->() }
165 ? {
166 msgstr => $unescape_maketext->(
167 $lexicon->{ $maketext_msg_key->() }->{msgstr},
168 162 ▶ ),
Branch: 3 branches
BranchTrueFalseTotal
exists $lexicon->{$msg_key} ? : 142 20 162
exists $$lexicon{&$maketext_msg_key()} ? : 0 20 20
$ext_lexicon ? : 0 20 20
Condition0%0/2
$ext_lexicon->fetch_from_lexicon($lexicon_key, $msg_key) or {"msgstr", &$unescape_maketext($ext_lexicon->fetch_from_lexicon($lexicon_key, &$maketext_msg_key()))}
l!l
00
MC/DC0%0/1
$ext_lexicon->fetch_from_lexicon($lexicon_key, $msg_key) or {"msgstr", &$unescape_maketext($ext_lexicon->fetch_from_lexicon($lexicon_key, &$maketext_msg_key()))}
$ext_lexicon->fetch_from_lexicon($lexicon_key, $msg_key)
Truth table0%0/2
A
A= $ext_lexicon->fetch_from_lexicon($lexicon_key, $msg_key)
Aresult
11
00
169 }
170 : $ext_lexicon
171 ? $ext_lexicon->fetch_from_lexicon($lexicon_key, $msg_key)
172 || {
173 msgstr => $unescape_maketext->(
174 $ext_lexicon->fetch_from_lexicon( $lexicon_key, $maketext_msg_key->() ),
175 ),
176 }
177 : ();
178 162 ▶ if ( $plural_callback ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plural_callback) { } 4 158 162
elsif ($is_n) { } 55 103 158
179 $plural_callback->(
180 $lexicon->{ q{} }->{plural_code}
181 4 ▶ || confess qq{Plural-Forms not found in lexicon "$lexicon_key"},
Condition50%1/2
$lexicon->{''}{'plural_code'} || confess(qq[Plural-Forms not found in lexicon "$lexicon_key"])
l!l
40
MC/DC0%0/1
$lexicon->{''}{'plural_code'} || confess(qq[Plural-Forms not found in lexicon "$lexicon_key"])
$lexicon->{''}{'plural_code'}
Truth table50%1/2
A
A= $lexicon->{''}{'plural_code'}
Aresult
11
00
182 );
183 }
184 elsif ( $is_n ) {
185 my $plural_code = $lexicon->{ q{} }->{plural_code}
186 55 ▶ or confess qq{Plural-Forms not found in lexicon "$lexicon_key"};
Branch: 1 branch
BranchTrueFalseTotal
my $plural_code = $lexicon->{""}{"plural_code"} or confess(qq[Plural-Forms not found in lexicon "$lexicon_key"]) 55 0 55
187 55 ▶ my $multiplural_index
Branch: 1 branch
BranchTrueFalseTotal
ref $count eq 'ARRAY' ? : 9 46 55
188 = ref $count eq 'ARRAY'
189 ? $self->_calculate_multiplural_index($count, $plural_code, $lexicon, $lexicon_key)
190 : $plural_code->($count);
191 55 my $msgstr_plural = $msg_ref->{msgstr_plural}->[$multiplural_index];
192 55 ▶ if ( ! defined $msgstr_plural || ! length $msgstr_plural ) { # fallback
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msgstr_plural and length $msgstr_plural) 4 51 55
Condition66%2/3
defined $msgstr_plural and length $msgstr_plural
!ll&&!rl&&r
4051
MC/DC50%1/2
defined $msgstr_plural and length $msgstr_plural
defined $msgstr_plural length $msgstr_plural
Truth table66%2/3
A and B
A= defined $msgstr_pluralB= length $msgstr_plural
ABresult
0X0
100
111
193 4 ▶ $msgstr_plural = $plural_code->($count)
Branch: 1 branch
BranchTrueFalseTotal
&$plural_code($count) ? : 2 2 4
194 ? $msgid_plural
195 : $msgid;
196 4 ▶ my $text = $lexicon
Branch: 1 branch
BranchTrueFalseTotal
$lexicon ? : 4 0 4
197 ? qq{Using lexicon "$lexicon_key".}
198 : qq{Lexicon "$lexicon_key" not found.};
199 4 ▶ $self->language ne 'i-default'
Branch: 4 branches
BranchTrueFalseTotal
$self->language ne "i-default" and $self->logger and $self->logger->(sprintf("%s msgstr_plural not found for msgctxt=%s, msgid=%s, msgid_plural=%s.", $text, defined $msgctxt ? qq["$msgctxt"] : "undef", defined $msgid ? qq["$msgid"] : "undef", defined $msgid_plural ? qq["$msgid_plural"] : "undef"), {"object", $self, "type", "warn", "event", "translation,fallback"}) 0 4 4
defined $msgctxt ? : 0 0 0
defined $msgid ? : 0 0 0
defined $msgid_plural ? : 0 0 0
Condition33%1/3
$self->language ne "i-default" and $self->logger
!ll&&!rl&&r
400
MC/DC0%0/2
$self->language ne "i-default" and $self->logger
$self->language ne "i-default" $self->logger
Truth table33%1/3
A and B
A= $self->language ne "i-default"B= $self->logger
ABresult
0X0
100
111
200 and $self->logger
201 and $self->logger->(
202 (
203 sprintf
204 '%s msgstr_plural not found for msgctxt=%s, msgid=%s, msgid_plural=%s.',
205 $text,
206 ( defined $msgctxt ? qq{"$msgctxt"} : 'undef' ),
207 ( defined $msgid ? qq{"$msgid"} : 'undef' ),
208 ( defined $msgid_plural ? qq{"$msgid_plural"} : 'undef' ),
209 ),
210 {
211 object => $self,
212 type => 'warn',
213 event => 'translation,fallback',
214 },
215 );
216 }
217 55 return $msgstr_plural;
218 }
219 my $msgstr = exists $msg_ref->{msgstr}
220 ? $msg_ref->{msgstr}
221 107 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $msg_ref->{'msgstr'} ? : 90 17 107
222 107 ▶ if ( ! defined $msgstr || ! length $msgstr ) { # fallback
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msgstr and length $msgstr) 17 90 107
Condition66%2/3
defined $msgstr and length $msgstr
!ll&&!rl&&r
17090
MC/DC50%1/2
defined $msgstr and length $msgstr
defined $msgstr length $msgstr
Truth table66%2/3
A and B
A= defined $msgstrB= length $msgstr
ABresult
0X0
100
111
223 17 $msgstr = $msgid;
224 17 ▶ my $text = $lexicon
Branch: 1 branch
BranchTrueFalseTotal
$lexicon ? : 17 0 17
225 ? qq{Using lexicon "$lexicon_key".}
226 : qq{Lexicon "$lexicon_key" not found.};
227 17 ▶ $self->language ne 'i-default'
Branch: 3 branches
BranchTrueFalseTotal
$self->language ne "i-default" and $self->logger and $self->logger->(sprintf("%s msgstr not found for msgctxt=%s, msgid=%s.", $text, defined $msgctxt ? qq["$msgctxt"] : "undef", defined $msgid ? qq["$msgid"] : "undef"), {"object", $self, "type", "warn", "event", "translation,fallback"}) 7 10 17
defined $msgctxt ? : 0 7 7
defined $msgid ? : 7 0 7
Condition66%2/3
$self->language ne "i-default" and $self->logger
!ll&&!rl&&r
1007
MC/DC50%1/2
$self->language ne "i-default" and $self->logger
$self->language ne "i-default" $self->logger
Truth table66%2/3
A and B
A= $self->language ne "i-default"B= $self->logger
ABresult
0X0
100
111
228 and $self->logger
229 and $self->logger->(
230 (
231 sprintf
232 '%s msgstr not found for msgctxt=%s, msgid=%s.',
233 $text,
234 ( defined $msgctxt ? qq{"$msgctxt"} : 'undef' ),
235 ( defined $msgid ? qq{"$msgid"} : 'undef' ),
236 ),
237 {
238 object => $self,
239 type => 'warn',
240 event => 'translation,fallback',
241 },
242 );
243 }
244
245 107 return $msgstr;
246 }
247
248 3 ▶sub run_filter {
Subroutine run_filter: called CC=2 SCAR=6.9
Pod documented
249 3 my ( $self, $translation_ref ) = @_;
250
251 3 ▶ $self->filter
Branch: 1 branch
BranchTrueFalseTotal
$self->filter or return $self 2 1 3
252 or return $self;
253 2 $self->filter->($self, $translation_ref);
254
255 2 return $self;
256 }
257
258 34 __PACKAGE__->meta->make_immutable;
259
260 34 1;
261
262 __END__
↑ Summary OO/Lexicon/Role/StoreFile.pm »