Usul/L10N.pm

filter: Statementstmt 87.3% 124 / 142 Branchbran 67.3% 35 / 52 Conditioncond 53.3% 24 / 45 MC/DCmcdc 20.0% 5 / 25 Subroutinesub 75.0% 24 / 32 Podpod 100% 5 / 5 totaltotal 72.0% 217 / 301 CC 48 SCAR 19.2
CC
48
Cov
77%
__ANON__
15.8
__ANON__
12.8
localize
11.9
CRAP 6.8
« Usul/TraitFor/Usage.pm ↑ Summary Usul/TraitFor/ConnectInfo.pm »
1 package Class::Usul::L10N;
2  
3 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Class::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Class::Usul::Constants qw( FALSE LANG NUL SEP TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Class::Usul::Functions qw( assert is_arrayref
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 is_hashref merge_attributes );
9 3 ▶use Class::Usul::Types qw( ArrayRef Bool HashRef Logger SimpleStr Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::DataClass::Types qw( Directory Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Gettext::Constants qw( CONTEXT_SEP LOCALE_DIRS );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Spec::Functions qw( tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Unexpected::Functions qw( inflate_placeholders );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Public attributes
19 0 ▶has 'l10n_attributes' => is => 'lazy', isa => HashRef, builder => sub { {} };
Subroutine _build_l10n_attributes: not called CC=1 SCAR=1.2
20  
21 3 has 'locale' => is => 'lazy', isa => SimpleStr, default => LANG;
22  
23 has 'localedir' => is => 'lazy', isa => Path, coerce => TRUE,
24 0 ▶ builder => sub { LOCALE_DIRS->[ 0 ] };
Subroutine _build_localedir: not called CC=1 SCAR=1.2
25  
26 has 'log' => is => 'ro', isa => Logger,
27 0 ▶ builder => sub { Class::Null->new };
Subroutine _build_log: not called CC=1 SCAR=1.2
28  
29 has 'tempdir' => is => 'lazy', isa => Directory, coerce => TRUE,
30 0 ▶ builder => sub { tmpdir };
Subroutine _build_tempdir: not called CC=1 SCAR=1.2
31  
32 # Private attributes
33 2 ▶has '_domains' => is => 'lazy', isa => ArrayRef[Str], builder => sub {
Subroutine _build__domains: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=2 SCAR=7.6
34 2 ▶ $_[ 0 ]->l10n_attributes->{domains} // [ 'messages' ] },
Condition50%2/4
$_[0]->l10n_attributes->{"domains"} // ["messages"]
l!l
20
$_[0]->l10n_attributes->{"domains"} // ["messages"]
l!l
30
MC/DC0%0/1
$_[0]->l10n_attributes->{"domains"} // ["messages"]
$_[0]->l10n_attributes->{"domains"}
MC/DC0%0/1
$_[0]->l10n_attributes->{"domains"} // ["messages"]
$_[0]->l10n_attributes->{"domains"}
Truth table50%1/2
A
A= $_[0]->l10n_attributes->{"domains"}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]->l10n_attributes->{"domains"}
Aresult
11
00
35 3 reader => 'domains';
36  
37 5 ▶has '_source_name' => is => 'lazy', isa => SimpleStr, builder => sub {
Subroutine _build__source_name: called CC=2 SCAR=7.6
38 5 ▶ $_[ 0 ]->l10n_attributes->{source_name} // 'po' },
Condition50%1/2
$_[0]->l10n_attributes->{"source_name"} // "po"
l!l
05
MC/DC0%0/1
$_[0]->l10n_attributes->{"source_name"} // "po"
$_[0]->l10n_attributes->{"source_name"}
Truth table50%1/2
A
A= $_[0]->l10n_attributes->{"source_name"}
Aresult
11
00
39 3 reader => 'source_name';
40  
41 3 ▶has '_use_country' => is => 'lazy', isa => Bool, builder => sub {
Subroutine _build__use_country: called CC=2 SCAR=7.6
42 3 ▶ $_[ 0 ]->l10n_attributes->{use_country} // FALSE },
Condition50%1/2
$_[0]->l10n_attributes->{"use_country"} // 0
l!l
03
MC/DC0%0/1
$_[0]->l10n_attributes->{"use_country"} // 0
$_[0]->l10n_attributes->{"use_country"}
Truth table50%1/2
A
A= $_[0]->l10n_attributes->{"use_country"}
Aresult
11
00
43 3 reader => 'use_country';
44  
45 # Class attributes
46 3 my $domain_cache = {}; my $locale_cache = {};
47  
48 # Private methods
49 19 ▶my $_extract_lang_from = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
50 19 my ($self, $locale) = @_;
51  
52 19 ▶ exists $locale_cache->{ $locale } and return $locale_cache->{ $locale };
Branch: 1 branch
BranchTrueFalseTotal
exists $locale_cache->{$locale} and return $locale_cache->{$locale} 16 3 19
53  
54 3 ▶ my $sep = $self->use_country ? '.' : '_';
Branch: 1 branch
BranchTrueFalseTotal
$self->use_country ? : 0 3 3
55 3 my $lang = (split m{ \Q$sep\E }msx, $locale.$sep )[ 0 ];
56  
57 3 return $locale_cache->{ $locale } = $lang;
58 3 };
59  
60 19 ▶my $_load_domains = sub {
Subroutine __ANON__: called CC=10 SCAR=25.5
61 19 my ($self, $args) = @_; my $charset;
62  
63 0 ▶ assert $self, sub { $args->{locale} }, 'No locale id';
Subroutine __ANON__: not called CC=1 SCAR=1.2
64  
65 19 ▶ my $locale = $args->{locale} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $locale = $args->{"locale"} or return 19 0 19
66 19 my $lang = $self->$_extract_lang_from( $locale );
67 19 ▶ my $names = $args->{domains} // $args->{domain_names} // $self->domains;
Condition33%2/6
($args->{"domains"} // $args->{"domain_names"}) // $self->domains
l!l&&r!l&&!r
0190
$args->{"domains"} // $args->{"domain_names"}
l!l&&r!l&&!r
0019
MC/DC0%0/3
($args->{"domains"} // $args->{"domain_names"}) // $self->domains
$args->{"domains"} $args->{"domain_names"} $self->domains
Truth table25%1/4
A // B // C
A= $args->{"domains"}B= $args->{"domain_names"}C= $self->domains
ABCresult
1XX1
01X1
0011
0000
68 19 ▶ my @names = grep { defined and length } @{ $names };
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
69 19 my $key = $lang.SEP.(join '+', @names );
70  
71 19 ▶ defined $domain_cache->{ $key } and return $domain_cache->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
defined $domain_cache->{$key} and return $domain_cache->{$key} 16 3 19
72  
73 3 my $attrs = { %{ $self->l10n_attributes }, builder => $self,
74 source_name => $self->source_name, };
75  
76 3 ▶ defined $self->localedir and $attrs->{localedir} = $self->localedir;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->localedir and $attrs->{"localedir"} = $self->localedir 3 0 3
77  
78 3 ▶ $locale =~ m{ \A (?: [a-z][a-z] )
Branch: 1 branch
BranchTrueFalseTotal
$locale =~ / \A (?: [a-z][a-z] ) (?: (?:_[A-Z][A-Z] )? \. ( [-_A-Za-z0-9]+ )? )? (?: \@[-_A-Za-z0-9=;]+ )? \z /msx and $charset = $1 2 1 3
79 (?: (?:_[A-Z][A-Z] )? \. ( [-_A-Za-z0-9]+ )? )?
80 (?: \@[-_A-Za-z0-9=;]+ )? \z }msx and $charset = $1;
81 3 ▶ $charset and $attrs->{charset} = $charset;
Branch: 1 branch
BranchTrueFalseTotal
$charset and $attrs->{"charset"} = $charset 0 3 3
82  
83 3 ▶ my $domain = try { File::Gettext->new( $attrs )->load( $lang, @names ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
84 0 ▶ catch { $self->log->error( $_ ); return };
Subroutine __ANON__: not called CC=1 SCAR=2.6
85  
86 3 ▶ return $domain ? $domain_cache->{ $key } = $domain : undef;
Branch: 1 branch
BranchTrueFalseTotal
$domain ? : 3 0 3
87 3 };
88  
89 18 ▶my $_gettext = sub {
Subroutine __ANON__: called CC=14 SCAR=27.6
90 18 my ($self, $key, $args) = @_;
91  
92 18 ▶ my $count = $args->{count} || 1;
Condition100%2/2
$args->{'count'} || 1
l!l
117
MC/DC100%1/1
$args->{'count'} || 1
$args->{'count'}
Truth table100%2/2
A
A= $args->{'count'}
Aresult
11
00
93 18 ▶ my $default = $args->{no_default} ? NUL : $key;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'no_default'} ? : 0 18 18
94 my $domain = $self->$_load_domains( $args )
95 18 ▶ or return ($default, $args->{plural_key})[ $count > 1 ] // $default;
Branch: 1 branch
BranchTrueFalseTotal
my $domain = $self->$_load_domains($args) or return ($default, $args->{"plural_key"})[$count > 1] // $default 18 0 18
Condition0%0/3
($default, $args->{"plural_key"})[$count > 1] // $default
l!l&&r!l&&!r
000
MC/DC0%0/2
($default, $args->{"plural_key"})[$count > 1] // $default
($default, $args->{"plural_key"})[$count > 1] $default
Truth table0%0/3
A // B
A= ($default, $args->{"plural_key"})[$count > 1]B= $default
ABresult
1X1
011
000
96 # Select either singular or plural translation
97 18 my ($nplurals, $plural) = (1, 0);
98  
99 18 ▶ if ($count > 1) { # Some languages have more than one plural form
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 1) 1 17 18
100 1 ($nplurals, $plural) = $domain->{plural_func}->( $count );
101 1 ▶ defined $nplurals or $nplurals = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $nplurals or $nplurals = 0 1 0 1
102 1 ▶ defined $plural or $plural = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $plural or $plural = 0 1 0 1
103 1 ▶ $plural > $nplurals and $plural = $nplurals;
Branch: 1 branch
BranchTrueFalseTotal
$plural > $nplurals and $plural = $nplurals 0 1 1
104 }
105  
106 my $id = defined $args->{context}
107 18 ▶ ? $args->{context}.CONTEXT_SEP.$key : $key;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'context'} ? : 2 16 18
108 18 ▶ my $msgs = $domain->{ $self->source_name } // {};
Condition100%2/2
$$domain{$self->source_name} // {}
l!l
99
MC/DC100%1/1
$$domain{$self->source_name} // {}
$$domain{$self->source_name}
Truth table100%2/2
A
A= $$domain{$self->source_name}
Aresult
11
00
109 18 ▶ my $msg = $msgs->{ $id } // {};
Condition100%2/2
$msgs->{$id} // {}
l!l
810
MC/DC100%1/1
$msgs->{$id} // {}
$msgs->{$id}
Truth table100%2/2
A
A= $msgs->{$id}
Aresult
11
00
110  
111 18 ▶ return @{ $msg->{msgstr} // [] }[ $plural ] // $default;
Condition80%4/5
@{$msg->{"msgstr"} // [];}[$plural] // $default
l!l&&r!l&&!r
8100
$msg->{"msgstr"} // []
l!l
810
MC/DC0%0/2
@{$msg->{"msgstr"} // [];}[$plural] // $default
@{$msg->{"msgstr"} // [];}[$plural] $default
MC/DC100%1/1
$msg->{"msgstr"} // []
$msg->{"msgstr"}
Truth table66%2/3
A // B
A= @{$msg->{"msgstr"} // [];}[$plural]B= $default
ABresult
1X1
011
000
Truth table100%2/2
A
A= $msg->{"msgstr"}
Aresult
11
00
112 3 };
113  
114 # Construction
115 5 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
116 5 my ($orig, $class, @args) = @_; my $attr = $orig->( $class, @args );
117  
118 5 ▶ my $builder = $attr->{builder} or return $attr;
Branch: 1 branch
BranchTrueFalseTotal
my $builder = $attr->{"builder"} or return $attr 4 1 5
119 4 ▶ my $config = $builder->can( 'config' ) ? $builder->config : {};
Branch: 1 branch
BranchTrueFalseTotal
$builder->can('config') ? : 4 0 4
120 4 my $keys = [ qw( l10n_attributes locale localedir tempdir ) ];
121  
122 4 merge_attributes $attr, $builder, [ 'log' ];
123 4 merge_attributes $attr, $config, $keys;
124  
125 4 return $attr;
126 3 };
127  
128 # Public methods
129 1 ▶sub get_po_header {
Subroutine get_po_header: called CC=5 SCAR=17.8
Pod documented
130 1 my ($self, $args) = @_;
131  
132 1 ▶ my $domain = $self->$_load_domains( $args // {} ) or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $domain = $self->$_load_domains($args // {}) or return {} 1 0 1
Condition50%1/2
$args // {}
l!l
10
MC/DC0%0/1
$args // {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
133 1 ▶ my $header = $domain->{po_header} or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $header = $domain->{"po_header"} or return {} 1 0 1
134  
135 1 ▶ return $header->{msgstr} // {};
Condition50%1/2
$header->{"msgstr"} // {}
l!l
10
MC/DC0%0/1
$header->{"msgstr"} // {}
$header->{"msgstr"}
Truth table50%1/2
A
A= $header->{"msgstr"}
Aresult
11
00
136 }
137  
138 0 ▶sub invalidate_cache {
Subroutine invalidate_cache: not called CC=1 SCAR=6.9
Pod documented
139 0 $domain_cache = {}; return;
140 }
141  
142 0 ▶sub loc {
Subroutine loc: not called CC=1 SCAR=6.9
Pod documented
143 0 my $self = shift; return $self->localizer( $self->locale, @_ );
144 }
145  
146 18 ▶sub localize {
Subroutine localize: called CC=8 SCAR=24.8
Pod documented
147 18 my ($self, $key, $args) = @_;
148  
149 18 ▶ defined $key or return; $key = "${key}"; chomp $key; $args //= {};
Branch: 1 branch
BranchTrueFalseTotal
defined $key or return 18 0 18
Condition50%1/2
$args //= {}
l!l
180
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
150  
151 # Lookup the message using the supplied key from the po file
152 18 my $text = $self->$_gettext( $key, $args );
153  
154 18 ▶ if (defined $args->{params} and ref $args->{params} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"params"} and ref $args->{"params"} eq "ARRAY") 12 6 18
Condition66%2/3
defined $args->{"params"} and ref $args->{"params"} eq "ARRAY"
!ll&&!rl&&r
6012
MC/DC50%1/2
defined $args->{"params"} and ref $args->{"params"} eq "ARRAY"
defined $args->{"params"} ref $args->{"params"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $args->{"params"}B= ref $args->{"params"} eq "ARRAY"
ABresult
0X0
100
111
155 12 ▶ 0 > index $text, '[_' and return $text;
Branch: 1 branch
BranchTrueFalseTotal
index($text, "[_") == -1 and return $text 8 4 12
156  
157 # Expand positional parameters of the form [_<n>]
158 return inflate_placeholders [ '[?]', '[]', $args->{no_quote_bind_values}],
159 4 $text, @{ $args->{params} };
160 }
161  
162 6 ▶ 0 > index $text, '{' and return $text;
Branch: 1 branch
BranchTrueFalseTotal
index($text, "{") == -1 and return $text 6 0 6
163  
164 # Expand named parameters of the form {param_name}
165 0 my %args = %{ $args }; my $re = join '|', map { quotemeta $_ } keys %args;
166  
167 0 ▶ $text =~ s{ \{($re)\} }{ defined $args{ $1 } ? $args{ $1 } : "{${1}?}" }egmx;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$1} ? : 0 0 0
168  
169 0 return $text;
170 }
171  
172 7 ▶sub localizer {
Subroutine localizer: called CC=5 SCAR=16.4
Pod documented
173 7 my ($self, $locale, $key, @args) = @_; my $car = $args[ 0 ];
174  
175 7 ▶ my $args = (is_hashref $car) ? { %{ $car } }
Branch: 2 branches
BranchTrueFalseTotal
&is_hashref($car) ? : 1 6 7
&is_arrayref($car) ? : 1 5 6
176 : { params => (is_arrayref $car) ? $car : [ @args ] };
177  
178 7 ▶ $args->{locale } //= $locale;
Condition33%1/3
$args->{"locale"} //= $locale
l!l&&r!l&&!r
070
MC/DC0%0/2
$args->{"locale"} //= $locale
$args->{"locale"} $locale
Truth table33%1/3
A //= B
A= $args->{"locale"}B= $locale
ABresult
1X1
011
000
179 7 ▶ $args->{no_quote_bind_values} //= TRUE;
Condition50%1/2
$args->{"no_quote_bind_values"} //= 1
l!l
07
MC/DC0%0/1
$args->{"no_quote_bind_values"} //= 1
$args->{"no_quote_bind_values"}
Truth table50%1/2
A
A= $args->{"no_quote_bind_values"}
Aresult
11
00
180  
181 7 return $self->localize( $key, $args );
182 }
183  
184 3 1;
185  
186 __END__
« Usul/TraitFor/Usage.pm ↑ Summary Usul/TraitFor/ConnectInfo.pm »