lib/Mojolicious/Plugin/I18N.pm

filter: Statementstmt 96.2% 127 / 132 Branchbran 73.0% 38 / 52 Conditioncond 70.7% 29 / 41 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 100% 20 / 20 Podpod 100% 1 / 1 totaltotal 84.0% 226 / 269 CC 44 SCAR 21.9
CC
44
Cov
86%
__ANON__
16.1
__ANON__
13.1
_load_module
12.2
CRAP 8.9
↑ Summary
1 package Mojolicious::Plugin::I18N;
2 5 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use I18N::LangTags::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = '1.6';
9  
10 # "Can we have Bender burgers again?
11 # No, the cat shelter’s onto me."
12 5 ▶sub register {
Subroutine register: called CC=3 SCAR=11.1
Pod documented
13 5 my ($plugin, $app, $conf) = @_;
14  
15 # Initialize
16 5 ▶ my $namespace = $conf->{namespace} || ( (ref $app) . '::I18N' );
Condition50%1/2
$conf->{'namespace'} || ref($app) . '::I18N'
l!l
50
MC/DC0%0/1
$conf->{'namespace'} || ref($app) . '::I18N'
$conf->{'namespace'}
Truth table50%1/2
A
A= $conf->{'namespace'}
Aresult
11
00
17 5 ▶ my $default = $conf->{default } || 'en';
Condition100%2/2
$conf->{'default'} || 'en'
l!l
32
MC/DC100%1/1
$conf->{'default'} || 'en'
$conf->{'default'}
Truth table100%2/2
A
A= $conf->{'default'}
Aresult
11
00
18 5 $default =~ tr/-A-Z/_a-z/;
19 5 $default =~ tr/_a-z0-9//cd;
20  
21 5 my $langs = $conf->{support_url_langs};
22 5 my $hosts = $conf->{support_hosts };
23  
24 # Default Handler
25 42 ▶ my $handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 shift->stash->{i18n} =
27 42 Mojolicious::Plugin::I18N::_Handler->new(namespace => $namespace, default => $default)
28 ;
29 5 };
30  
31 # Add hook
32 $app->hook(
33 42 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=12 SCAR=27.8
34 42 my $self = shift;
35  
36 # Handler
37 42 $handler->( $self );
38  
39 # Header detection
40 my @languages = $conf->{no_header_detect}
41 ? ()
42 42 ▶ : I18N::LangTags::implicate_supers(
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'no_header_detect'} ? : 0 42 42
43 I18N::LangTags::Detect->http_accept_langs(
44 $self->req->headers->accept_language
45 )
46 )
47 ;
48  
49 # Host detection
50 42 ▶ my $host = $self->req->headers->header('X-Host') || $self->req->headers->host;
Condition33%1/3
$self->req->headers->header('X-Host') || $self->req->headers->host
l!l&&r!l&&!r
0420
MC/DC0%0/2
$self->req->headers->header('X-Host') || $self->req->headers->host
$self->req->headers->header('X-Host') $self->req->headers->host
Truth table33%1/3
A || B
A= $self->req->headers->header('X-Host')B= $self->req->headers->host
ABresult
1X1
011
000
51 42 ▶ if ($conf->{support_hosts} && $host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"support_hosts"} and $host) 0 42 42
Condition33%1/3
$conf->{"support_hosts"} and $host
!ll&&!rl&&r
4200
MC/DC0%0/2
$conf->{"support_hosts"} and $host
$conf->{"support_hosts"} $host
Truth table33%1/3
A and B
A= $conf->{"support_hosts"}B= $host
ABresult
0X0
100
111
52 0 warn $host;
53 0 $host =~ s/^www\.//; # hack
54 0 ▶ if (my $lang = $conf->{support_hosts}->{ $host }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = $conf->{"support_hosts"}{$host}) 0 0 0
55 0 $self->app->log->debug("Found language $lang, Host header is $host");
56  
57 0 unshift @languages, $lang;
58 }
59 }
60  
61 # Set default language
62 42 ▶ $self->stash(lang_default => $languages[0]) if $languages[0];
Branch: 1 branch
BranchTrueFalseTotal
if $languages[0] 12 30 42
63  
64 # URL detection
65 42 ▶ if (my $path = $self->req->url->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $self->req->url->path) 42 0 42
66 42 my $part = $path->parts->[0];
67  
68 42 ▶ if ($part && $langs && grep { $part eq $_ } @$langs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part and $langs and grep {$part eq $_;} @$langs) 12 30 42
Condition100%6/6
$part and $langs and grep {$part eq $_;} @$langs
!ll&&!rl&&r
191112
$part and $langs
!ll&&!rl&&r
71223
MC/DC100%3/3
$part and $langs and grep {$part eq $_;} @$langs
$part $langs grep {$part eq $_;} @$langs
Truth table100%4/4
A and B and C
A= $partB= $langsC= grep {$part eq $_;} @$langs
ABCresult
0XX0
10X0
1100
1111
69 # Ignore static files
70 12 ▶ return if $self->res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->res->code 0 12 12
71  
72 12 $self->app->log->debug("Found language $part in URL $path");
73  
74 12 unshift @languages, $part;
75  
76 # Save lang in stash
77 12 $self->stash(lang => $part);
78  
79 # Clean path
80 12 shift @{$path->parts};
81 12 $path->trailing_slash(0);
82 }
83 }
84  
85 # Languages
86 42 $self->languages(@languages, $default);
87 }
88 5 );
89  
90 # Add "languages" helper
91 96 ▶ $app->helper(languages => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
92 96 my $self = shift;
93  
94 96 ▶ $handler->( $self ) unless $self->stash('i18n');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("i18n") 0 96 96
95  
96 96 $self->stash->{i18n}->languages(@_);
97 5 });
98  
99 # Add "l" helper
100 48 ▶ $app->helper(l => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
101 48 my $self = shift;
102  
103 48 ▶ $handler->( $self ) unless $self->stash('i18n');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stash("i18n") 0 48 48
104  
105 48 $self->stash->{i18n}->localize(@_);
106 5 });
107  
108 # Reimplement "url_for" helper
109 5 my $mojo_url_for = *Mojolicious::Controller::url_for{CODE};
110  
111 58 ▶ my $i18n_url_for = sub {
Subroutine __ANON__: called CC=13 SCAR=25.8
112 58 my $self = shift;
113 58 my $url = $self->$mojo_url_for(@_);
114  
115 # Absolute URL
116 58 ▶ return $url if $url->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $url->is_abs 1 57 58
117  
118 # Discard target if present
119 57 ▶ shift if (@_ % 2 && !ref $_[0]) || (@_ > 1 && ref $_[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 and not ref $_[0] or @_ > 1 and ref $_[-1] 46 11 57
Condition100%9/9
@_ % 2 and not ref $_[0] or @_ > 1 and ref $_[-1]
l!l&&r!l&&!r
45111
@_ % 2 and not ref $_[0]
!ll&&!rl&&r
11145
@_ > 1 and ref $_[-1]
!ll&&!rl&&r
1011
MC/DC100%4/4
@_ % 2 and not ref $_[0] or @_ > 1 and ref $_[-1]
@_ % 2 not ref $_[0] @_ > 1 ref $_[-1]
Truth table71%5/7
A and not(B) or C and D
A= @_ % 2B= not ref $_[0]C= @_ > 1D= ref $_[-1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
120  
121 # Unveil params
122 57 ▶ my %params = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 55 57
123  
124 # Detect lang
125 57 ▶ if (my $lang = $params{lang} || $self->stash('lang')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = $params{"lang"} || $self->stash("lang")) 16 41 57
Condition100%3/3
$params{'lang'} || $self->stash('lang')
l!l&&r!l&&!r
61041
MC/DC100%2/2
$params{'lang'} || $self->stash('lang')
$params{'lang'} $self->stash('lang')
Truth table100%3/3
A || B
A= $params{'lang'}B= $self->stash('lang')
ABresult
1X1
011
000
126 16 ▶ my $path = $url->path || [];
Condition50%1/2
$url->path || []
l!l
160
MC/DC0%0/1
$url->path || []
$url->path
Truth table50%1/2
A
A= $url->path
Aresult
11
00
127  
128 # Root
129 16 ▶ if (!$path->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $path->[0]) { } 6 10 16
elsif (ref $langs ne 'ARRAY' or not scalar grep({$path->contains("/$_");} @$langs)) { } 10 0 10
130 6 $path->parts([ $lang ]);
131 }
132  
133 # No language detected
134 30 ▶ elsif ( ref $langs ne 'ARRAY' or not scalar grep { $path->contains("/$_") } @$langs ) {
Condition33%1/3
ref $langs ne "ARRAY" or not scalar grep({$path->contains("/$_");} @$langs)
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $langs ne "ARRAY" or not scalar grep({$path->contains("/$_");} @$langs)
ref $langs ne "ARRAY" not scalar grep({$path->contains("/$_");} @$langs)
Truth table33%1/3
A or not(B)
A= ref $langs ne "ARRAY"B= not scalar grep({$path->contains("/$_");} @$langs)
ABresult
1X1
011
000
135 10 unshift @{ $path->parts }, $lang;
136 }
137 }
138  
139 57 $url;
140 5 };
141  
142 {
143 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
144 5 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 5 *Mojolicious::Controller::url_for = $i18n_url_for;
147 }
148 }
149  
150 package Mojolicious::Plugin::I18N::_Handler;
151 5 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 5 ▶use constant DEBUG => $ENV{MOJO_I18N_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_I18N_DEBUG'} || 0
l!l
05
MC/DC0%0/1
$ENV{'MOJO_I18N_DEBUG'} || 0
$ENV{'MOJO_I18N_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_I18N_DEBUG'}
Aresult
11
00
154  
155 # "Robot 1-X, save my friends! And Zoidberg!"
156 96 ▶sub languages {
Subroutine languages: called CC=4 SCAR=13.9
157 96 my ($self, @languages) = @_;
158  
159 96 ▶ unless (@languages) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@languages) 38 58 96
160 38 my $lang = $self->{language};
161  
162 # lang such as en-us
163 38 $lang =~ s/_/-/g;
164  
165 38 return $lang;
166 }
167  
168 # Handle
169 58 my $namespace = $self->{namespace};
170  
171 # Load Lang Module
172 58 $self->_load_module($namespace => $_) for @languages;
173  
174 58 ▶ if (my $handle = $namespace->get_handle(@languages)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $namespace->get_handle(@languages)) 58 0 58
175 58 ▶ $handle->fail_with(sub { $_[1] });
Subroutine __ANON__: called CC=1 SCAR=0.0
176 58 $self->{handle} = $handle;
177 58 $self->{language} = $handle->language_tag;
178 }
179  
180 58 return $self;
181 }
182  
183 48 ▶sub localize {
Subroutine localize: called CC=2 SCAR=7.0
184 48 my $self = shift;
185 48 my $key = shift;
186 48 ▶ return $key unless my $handle = $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless my $handle = $self->{"handle"} 0 48 48
187 48 return $handle->maketext($key, @_);
188 }
189  
190 98 ▶sub _load_module {
Subroutine _load_module: called CC=12 SCAR=25.0
191 98 my $self = shift;
192  
193 98 my($namespace, $lang) = @_;
194 98 ▶ return unless $namespace && $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $namespace and $lang 0 98 98
Condition33%1/3
$namespace and $lang
!ll&&!rl&&r
0098
MC/DC0%0/2
$namespace and $lang
$namespace $lang
Truth table33%1/3
A and B
A= $namespaceB= $lang
ABresult
0X0
100
111
195  
196 # lang such as en-us
197 98 $lang =~ s/-/_/g;
198  
199 98 ▶ unless ($namespace->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace->can("new")) 3 95 98
200 3 DEBUG && warn("Load default namespace $namespace");
201  
202 3 (my $file = $namespace) =~ s{::|'}{/}g;
203 3 eval qq(require "$file.pm");
204  
205 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 1 3
206 2 DEBUG && warn("Create default namespace $namespace");
207  
208 2 ▶ eval "package $namespace; use base 'Locale::Maketext'; 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
209 2 ▶ die qq/Couldn't initialize I18N default class "$namespace": $@/ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
210 }
211 }
212  
213 98 for ($self->{default}, $lang) {
214 196 my $module = "${namespace}::$_";
215 196 ▶ unless ($module->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module->can("new")) 15 181 196
216 15 DEBUG && warn("Load the I18N class $module");
217  
218 15 (my $file = $module) =~ s{::|'}{/}g;
219 15 eval qq(require "$file.pm");
220  
221 15 my $default = $self->{default};
222 15 ▶ if ($@ || not eval "\%${module}::Lexicon") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not eval "%${module}::Lexicon") 14 1 15
Condition66%2/3
$@ or not eval "%${module}::Lexicon"
l!l&&r!l&&!r
1401
MC/DC50%1/2
$@ or not eval "%${module}::Lexicon"
$@ not eval "%${module}::Lexicon"
Truth table66%2/3
A or not(B)
A= $@B= not eval "%${module}::Lexicon"
ABresult
1X1
011
000
223 14 ▶ if ($_ eq $default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $default) 1 13 14
224 1 DEBUG && warn("Create the I18N class $module");
225  
226 1 ▶ eval "package ${module}; use base '$namespace';" . 'our %Lexicon = (_AUTO => 1); 1;';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 1 ▶ die qq/Couldn't initialize I18N class "$namespace": $@/ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
228 }
229 }
230 }
231 }
232 }
233  
234 5 1;
235  
236 __END__
↑ Summary