I18N/Action/I18N.pm

filter: Statementstmt 100% 37 / 37 Branchbran 57.1% 8 / 14 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 79.1% 57 / 72 CC 14 SCAR 21.9
CC
14
Cov
84%
after_load
12.2
_write_merged
4.0
BEGIN
1.0
CRAP 8.9
« I18N.pm ↑ Summary
1 package Mojolicious::Plugin::Fondation::I18N::Action::I18N;
2 2 $Mojolicious::Plugin::Fondation::I18N::Action::I18N::VERSION = '0.02';
3 # ABSTRACT: Scans all plugins for translations, merges them, writes share/i18n/<lang>.json
4  
5 2 ▶use Mojo::Base 'Mojolicious::Plugin::Fondation::Action::Base', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ---------------------------------------------------------------------------
9 # after_load -- called by Fondation Manager for each plugin
10 # ---------------------------------------------------------------------------
11  
12 4 ▶sub after_load ($self, $long, $conf, $share_dir) {
Subroutine after_load: called CC=11 SCAR=25.0
Pod undocumented
13 4 ▶ return unless $share_dir && -d $share_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $share_dir and -d $share_dir 0 4 4
Condition33%1/3
$share_dir and -d $share_dir
!ll&&!rl&&r
004
MC/DC0%0/2
$share_dir and -d $share_dir
$share_dir -d $share_dir
Truth table33%1/3
A and B
A= $share_dirB= -d $share_dir
ABresult
0X0
100
111
14  
15 4 my $trans_dir = $share_dir->child('translations');
16 4 ▶ return unless -d $trans_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $trans_dir 0 4 4
17  
18 4 my $manager = $self->manager;
19 4 my $app = $manager->app;
20 4 ▶ $app->{i18n_lexicons} //= {};
Condition100%2/2
$app->{"i18n_lexicons"} //= {}
l!l
22
MC/DC100%1/1
$app->{"i18n_lexicons"} //= {}
$app->{"i18n_lexicons"}
Truth table100%2/2
A
A= $app->{"i18n_lexicons"}
Aresult
11
00
21  
22 4 my $added = 0;
23 4 for my $file ($trans_dir->list({dir => 0})->each) {
24 6 my $basename = $file->basename;
25 6 ▶ next unless $basename =~ /^([a-z]{2}(?:[_-][a-z]{2})?)\.json$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $basename =~ /^([a-z]{2}(?:[_-][a-z]{2})?)\.json$/iu 0 6 6
26 6 my $lang = lc($1);
27  
28 6 my $data = eval { decode_json($file->slurp) };
29 6 ▶ next if $@ || ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $data ne "HASH" 0 6 6
Condition33%1/3
$@ or ref $data ne "HASH"
l!l&&r!l&&!r
006
MC/DC0%0/2
$@ or ref $data ne "HASH"
$@ ref $data ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $data ne "HASH"
ABresult
1X1
011
000
30  
31 6 ▶ $app->{i18n_lexicons}{$lang} //= {};
Condition100%2/2
$app->{"i18n_lexicons"}{$lang} //= {}
l!l
24
MC/DC100%1/1
$app->{"i18n_lexicons"}{$lang} //= {}
$app->{"i18n_lexicons"}{$lang}
Truth table100%2/2
A
A= $app->{"i18n_lexicons"}{$lang}
Aresult
11
00
32 6 %{$app->{i18n_lexicons}{$lang}} = (
33 6 %{$app->{i18n_lexicons}{$lang}},
34 %$data,
35 );
36 6 $added += scalar(keys %$data);
37 }
38  
39 4 ▶ return unless $added;
Branch: 1 branch
BranchTrueFalseTotal
unless $added 0 4 4
40  
41 4 $self->log->debug(sprintf('%s: %d translations loaded', $long, $added));
42  
43 # Write merged files after each plugin -- last write has everything
44 4 _write_merged($manager);
45 }
46  
47 # ---------------------------------------------------------------------------
48 # _write_merged -- write merged per-language JSON files to share/i18n/
49 # ---------------------------------------------------------------------------
50  
51 4 ▶sub _write_merged ($manager) {
Subroutine _write_merged: called CC=4 SCAR=13.9
52 4 my $app = $manager->app;
53 4 ▶ my $lexicons = $app->{i18n_lexicons} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lexicons = $app->{"i18n_lexicons"} or return 4 0 4
54  
55 4 my $out_dir = $app->home->child('share', 'i18n');
56 4 ▶ $out_dir->make_path unless -d $out_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $out_dir 1 3 4
57  
58 4 for my $lang (sort keys %$lexicons) {
59 6 my $file = $out_dir->child("$lang.json");
60 6 $file->spurt(encode_json($lexicons->{$lang}));
61 }
62 }
63  
64 2 1;
« I18N.pm ↑ Summary