blib/lib/Mojolicious/Plugin/MultiLanguage.pm

filter: Statementstmt 14.5% 18 / 124 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 20.0% 5 / 25 Podpod 100% 1 / 1 totaltotal 9.4% 24 / 254 CC 40 SCAR 44.7
CC
40
Cov
9%
__ANON__
210.0
__ANON__
182.0
__ANON__
20.0
CRAP 87.6
↑ Summary
1 package Mojolicious::Plugin::MultiLanguage;
2 1 ▶use Mojo::Base "Mojolicious::Plugin";
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Mojo::Collection 'c';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::AcceptLanguage;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = "0.01";
8 1 $VERSION = eval $VERSION;
9  
10 0 ▶sub register {
Subroutine register: not called CC=4 SCAR=29.2
Pod documented
11 0 my ($self, $app, $conf) = @_;
12  
13 0 ▶ $conf->{cookie} //= {path => "/"};
Condition0%0/2
$conf->{"cookie"} //= {"path", "/"}
l!l
00
MC/DC0%0/1
$conf->{"cookie"} //= {"path", "/"}
$conf->{"cookie"}
Truth table0%0/2
A
A= $conf->{"cookie"}
Aresult
11
00
14 0 ▶ $conf->{languages} //= [qw/es fr de zh-tw/];
Condition0%0/2
$conf->{"languages"} //= ["es", "fr", "de", "zh-tw"]
l!l
00
MC/DC0%0/1
$conf->{"languages"} //= ["es", "fr", "de", "zh-tw"]
$conf->{"languages"}
Truth table0%0/2
A
A= $conf->{"languages"}
Aresult
11
00
15 0 ▶ $conf->{api_prefix} //= ["/api"];
Condition0%0/2
$conf->{"api_prefix"} //= ["/api"]
l!l
00
MC/DC0%0/1
$conf->{"api_prefix"} //= ["/api"]
$conf->{"api_prefix"}
Truth table0%0/2
A
A= $conf->{"api_prefix"}
Aresult
11
00
16  
17 state $langs_enabled = c(
18 0 'en', @{$conf->{languages}}
19 0 ▶ )->map(sub { lc $_ })->flatten->uniq;
Subroutine __ANON__: not called CC=1 SCAR=6.9
20  
21 state $langs_available = c(
22 # English
23 {
24 code => 'en',
25 name => "English",
26 native => "English",
27 dir => 'ltr',
28 index2 => 1,
29 index3 => 1,
30 },
31  
32 # Spanish
33 {
34 code => 'es',
35 name => "Spanish",
36 native => "Español",
37 dir => 'ltr',
38 index2 => 2,
39 index3 => 2,
40 },
41  
42 # German
43 {
44 code => 'de',
45 name => "German",
46 native => "Deutsch",
47 dir => 'ltr',
48 index2 => 3,
49 index3 => 3,
50 },
51  
52 # French
53 {
54 code => 'fr',
55 name => "French",
56 native => "Français",
57 dir => 'ltr',
58 index2 => 4,
59 index3 => 4,
60 },
61  
62 # Portuguese
63 {
64 code => 'pt-br',
65 name => "Portuguese",
66 native => "Português",
67 dir => 'ltr',
68 index2 => 5,
69 index3 => 5,
70 },
71  
72 # Italian
73 {
74 code => 'it',
75 name => "Italian",
76 native => "italiano",
77 dir => 'ltr',
78 index2 => 6,
79 index3 => 6,
80 },
81  
82 # Polish
83 {
84 code => 'pl',
85 name => "Polish",
86 native => "Polskie",
87 dir => 'ltr',
88 index2 => 7,
89 index3 => 7,
90 },
91  
92 # Russian
93 {
94 code => 'ru',
95 name => "Russian",
96 native => "Русский",
97 dir => 'ltr',
98 index2 => 8,
99 index3 => 8,
100 },
101  
102 # Ukrainian
103 {
104 code => 'uk',
105 name => "Ukrainian",
106 native => "Українська",
107 dir => 'ltr',
108 index2 => 9,
109 index3 => 9,
110 },
111  
112 # Finnish
113 {
114 code => 'fi',
115 name => "Finnish",
116 native => "Finnish",
117 dir => 'ltr',
118 index2 => 10,
119 index3 => 10,
120 },
121  
122 # Greek
123 {
124 code => 'el',
125 name => "Greek",
126 native => "Ελληνικά",
127 dir => 'ltr',
128 index2 => 11,
129 index3 => 11,
130 },
131  
132 # Turkish
133 {
134 code => 'tr',
135 name => "Turkish",
136 native => "Türk",
137 dir => 'ltr',
138 index2 => 12,
139 index3 => 12,
140 },
141  
142 # Arabic
143 {
144 code => 'ar',
145 name => "Arabic",
146 native => "العربية",
147 dir => 'rtl',
148 index2 => 13,
149 index3 => 13,
150 },
151  
152 # Farsi
153 {
154 code => 'fa',
155 name => "Farsi",
156 native => "हिंदी",
157 dir => 'rtl',
158 index2 => 14,
159 index3 => 14,
160 },
161  
162 # Hindi
163 {
164 code => 'hi',
165 name => "Hindi",
166 native => "हिंदी",
167 dir => 'ltr',
168 index2 => 15,
169 index3 => 15,
170 },
171  
172 # Chinese
173 {
174 code => 'zh-cn',
175 name => "Chinese (Simplified)",
176 native => "中国",
177 dir => 'ltr',
178 index2 => 16,
179 index3 => 16,
180 },
181  
182 {
183 code => 'zh-tw',
184 name => "Chinese (Traditional)",
185 native => "中国",
186 dir => 'ltr',
187 index2 => 17,
188 index3 => 17,
189 },
190  
191 # Japanese
192 {
193 code => 'ja',
194 name => "Japanese",
195 native => "日本",
196 dir => 'ltr',
197 index2 => 18,
198 index3 => 18,
199 },
200  
201 # Korean
202 {
203 code => 'ko',
204 name => "Korean",
205 native => "日本",
206 dir => 'ltr',
207 index2 => 19,
208 index3 => 19,
209 }
210 0 ▶ )->each(sub { $_->{index1} = 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
211  
212 # Default language
213 0 my $english = $langs_available->first;
214  
215 # Lookup language
216 0 ▶ my $lang_lookup = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
217 0 my ($code) = @_;
218  
219 0 ▶ $langs_available->grep(sub { lc $code eq $_->{code} })->first
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$langs_available->grep(sub { lc $code eq $_->{"code"}; } )->first or die "Language code '${code}' does not exists!" 0 0 0
220 or die "Language code '$code' does not exists!";
221 0 };
222  
223 # Enabled languages
224 0 ▶ $app->attr(languages => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 ▶ $langs_enabled->map(sub { $lang_lookup->($_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 });
227  
228 # Active languages codes
229 0 ▶ $app->attr(langs => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 ▶ $app->languages->map( sub { $_->{code} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 });
232  
233 0 ▶ my $lang_exists = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
234 0 my ($code) = @_;
235  
236 0 ▶ return 0 unless $code and $code =~ /^[a-z]{2}(-[a-z]{2})?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $code =~ /^[a-z]{2}(-[a-z]{2})?$/u 0 0 0
Condition0%0/3
$code and $code =~ /^[a-z]{2}(-[a-z]{2})?$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$code and $code =~ /^[a-z]{2}(-[a-z]{2})?$/u
$code $code =~ /^[a-z]{2}(-[a-z]{2})?$/u
Truth table0%0/3
A and B
A= $codeB= $code =~ /^[a-z]{2}(-[a-z]{2})?$/u
ABresult
0X0
100
111
237 0 ▶ $app->languages->grep(sub { $code eq $_->{code} })->size;
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 };
239  
240 # Parse Accept-Language header
241 0 ▶ $app->helper(accept_language => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 my ($c) = @_;
243  
244 0 my $header = $c->req->headers->accept_language;
245 0 HTTP::AcceptLanguage->new($header)->match(@{$app->langs});
246 0 });
247  
248 # Detect language for site via url, cookie or headers
249 0 ▶ my $detect_site = sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
250 0 my ($c, $path) = @_;
251  
252 0 ▶ my $part = $path->parts->[0] // '';
Condition0%0/2
$path->parts->[0] // ""
l!l
00
MC/DC0%0/1
$path->parts->[0] // ""
$path->parts->[0]
Truth table0%0/2
A
A= $path->parts->[0]
Aresult
11
00
253 0 my @flags = (0, $english->{code}, 0, "/");
254  
255 0 ▶ unless ($part) {
Branch: 3 branches
BranchTrueFalseTotal
if ($part) { } 0 0 0
if (do { $part eq $english->{'code'} }) { } 0 0 0
elsif (&$lang_exists($part)) { } 0 0 0
256 0 my $cookie = $c->cookie('lang');
257  
258 0 ▶ unless ($cookie) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cookie) { } 0 0 0
if (do { $cookie eq $english->{'code'} }) { } 0 0 0
elsif (&$lang_exists($cookie)) { } 0 0 0
259 0 my $accept = $c->accept_language;
260  
261 0 ▶ unless ($accept) {
Branch: 3 branches
BranchTrueFalseTotal
if ($accept) { } 0 0 0
if (do { $accept eq $english->{'code'} }) { } 0 0 0
elsif (&$lang_exists($accept)) { } 0 0 0
262 0 $app->log->debug("Unknown accept-language");
263 }
264  
265 elsif ($accept eq $english->{code}) {
266 0 @flags[1] = ($accept);
267 }
268  
269 elsif ($lang_exists->($accept)) {
270 0 @flags[1, 2, 3] = ($accept, 1, "/$accept");
271 }
272  
273 else {
274 0 $app->log->warn("Wrong accept-language: '$accept'");
275 }
276 }
277  
278 elsif ($cookie eq $english->{code}) {
279 0 @flags[1] = ($cookie);
280 }
281  
282 elsif ($lang_exists->($cookie)) {
283 0 @flags[1, 2, 3] = ($cookie, 1, "/$cookie");
284 }
285  
286 else {
287 0 $app->log->warn("Wrong cookie-language: '$cookie'");
288 }
289 }
290  
291 elsif ($part eq $english->{code}) {
292 0 @flags[0, 1, 2, 3] = (1, $part, 1, $path);
293 }
294  
295 elsif ($lang_exists->($part)) {
296 0 @flags[0, 1, 2, 3] = (1, $part, 0, $path);
297 }
298  
299 else {
300 0 $app->log->debug("No language detected");
301 }
302  
303 0 ▶ if ($flags[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags[0]) 0 0 0
304 0 shift @{$path->parts};
305 0 $path->trailing_slash(0);
306 }
307  
308 0 my $language = $lang_lookup->($flags[1]);
309 0 $c->cookie(lang => $language->{code}, $conf->{cookie});
310  
311 0 ▶ $c->redirect_to($flags[3]) and return undef if $flags[2];
Branch: 1 branch
BranchTrueFalseTotal
if $flags[2] 0 0 0
Condition0%0/5
$flags[2] && ($c->redirect_to($flags[3]) && (return undef))
!ll&&!rl&&r
000
$c->redirect_to($flags[3]) and return undef
!ll
00
MC/DC0%0/2
$flags[2] && ($c->redirect_to($flags[3]) && (return undef))
$flags[2] $c->redirect_to($flags[3])
Truth table0%0/3
A && B
A= $flags[2]B= $c->redirect_to($flags[3])
ABresult
0X0
100
111
312  
313 0 $app->log->debug("Detect site language '$language->{code}'");
314  
315 0 return $language;
316 0 };
317  
318 # Detetect language for api via headers only
319 0 ▶ my $detect_api = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
320 0 my ($c, $path) = @_;
321  
322 0 ▶ return $english if $c->req->method eq 'OPTIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method eq "OPTIONS" 0 0 0
323  
324 0 my $accept = $c->accept_language;
325  
326 0 ▶ my $language = $lang_exists->($accept)
Branch: 1 branch
BranchTrueFalseTotal
&$lang_exists($accept) ? : 0 0 0
327 ? $lang_lookup->($accept) : $english;
328  
329 0 $app->log->debug("Detect API language '$language->{code}'");
330  
331 0 return $language;
332 0 };
333  
334 0 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
335 0 my ($c) = @_;
336  
337 0 ▶ return if $c->res->code;
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->code 0 0 0
338  
339 0 my $path = $c->req->url->path;
340 0 my $is_api = grep { $path->contains($_) } @{$conf->{api_prefix}};
341  
342 0 ▶ return unless my $language = $is_api
Branch: 2 branches
BranchTrueFalseTotal
unless my $language = $is_api ? &$detect_api($c, $path) : &$detect_site($c, $path) 0 0 0
$is_api ? : 0 0 0
343 ? $detect_api->($c, $path) : $detect_site->($c, $path);
344  
345 0 $c->stash(language => $language);
346 0 });
347  
348 0 ▶ $app->hook(after_render => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
349 0 my ($c) = @_;
350  
351 0 ▶ return unless my $language = $c->stash('language');
Branch: 1 branch
BranchTrueFalseTotal
unless my $language = $c->stash("language") 0 0 0
352  
353 0 my $h = $c->res->headers;
354 0 $h->append('Vary' => "Accept-Language");
355 0 $h->content_language($language->{code});
356 0 });
357  
358 # Complete languages collection
359 0 ▶ $app->helper(languages => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
360 0 my ($c) = @_;
361  
362 0 ▶ my $language = $c->stash('language') // $english;
Condition0%0/3
$c->stash("language") // $english
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->stash("language") // $english
$c->stash("language") $english
Truth table0%0/3
A // B
A= $c->stash("language")B= $english
ABresult
1X1
011
000
363  
364 0 ▶ $app->languages->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
365 0 ▶ $_->{active} = $_->{code} eq $language->{code} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'code'} eq $language->{'code'} ? : 0 0 0
366 0 });
367 0 });
368  
369 # Reimplement 'url_for' helper
370 0 my $mojo_url_for = *Mojolicious::Controller::url_for{CODE};
371  
372 0 ▶ my $lang_url_for = sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
373 0 my ($c, @args) = @_;
374  
375 0 my $url = $c->$mojo_url_for(@args);
376  
377 0 ▶ return $url if $url->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $url->is_abs 0 0 0
378  
379 0 ▶ shift @args if @args % 2 && !ref $args[0] or @args > 1 && ref $args[-1];
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 and not ref $args[0] or @args > 1 and ref $args[-1] 0 0 0
Condition0%0/9
@args % 2 and not ref $args[0] or @args > 1 and ref $args[-1]
l!l&&r!l&&!r
000
@args % 2 and not ref $args[0]
!ll&&!rl&&r
000
@args > 1 and ref $args[-1]
!ll&&!rl&&r
000
MC/DC0%0/4
@args % 2 and not ref $args[0] or @args > 1 and ref $args[-1]
@args % 2 not ref $args[0] @args > 1 ref $args[-1]
Truth table0%0/7
A and not(B) or C and D
A= @args % 2B= not ref $args[0]C= @args > 1D= ref $args[-1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
380 0 ▶ my %params = @args == 1 ? %{$args[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 0 0
381  
382 0 ▶ return $url unless my $language = $c->stash('language');
Branch: 1 branch
BranchTrueFalseTotal
unless my $language = $c->stash("language") 0 0 0
383 0 ▶ my $code = $params{lang} // $language->{code};
Condition0%0/3
$params{"lang"} // $language->{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"lang"} // $language->{"code"}
$params{"lang"} $language->{"code"}
Truth table0%0/3
A // B
A= $params{"lang"}B= $language->{"code"}
ABresult
1X1
011
000
384  
385 0 ▶ return $url if $code eq $english->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $code eq $english->{"code"} 0 0 0
386  
387 0 ▶ my $path = $url->path // [];
Condition0%0/2
$url->path // []
l!l
00
MC/DC0%0/1
$url->path // []
$url->path
Truth table0%0/2
A
A= $url->path
Aresult
11
00
388  
389 0 ▶ unless ($path->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->[0]) { } 0 0 0
390 0 $path->parts([$code]);
391 }
392  
393 else {
394 0 ▶ my $exists = $c->languages->grep(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 $path->contains(sprintf "/%s", $_->{code})
396 0 })->size;
397  
398 0 ▶ unshift @{$path->parts}, $code unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 0 0
399 }
400  
401 0 return $url;
402 0 };
403  
404 {
405 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
406 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
407  
408 0 *Mojolicious::Controller::url_for = $lang_url_for;
409 }
410 }
411  
412 1 1;
413  
414 __END__
↑ Summary