| 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
Condition33%1/3
$share_dir and -d $share_dir
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
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | my $trans_dir = $share_dir->child('translations'); | |||||||||||||||||||||||||||
| 16 | 4 | ▶ | return unless -d $trans_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | my $manager = $self->manager; | |||||||||||||||||||||||||||
| 19 | 4 | my $app = $manager->app; | |||||||||||||||||||||||||||
| 20 | 4 | ▶ | $app->{i18n_lexicons} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$app->{"i18n_lexicons"} //= {}
MC/DC100%1/1
$app->{"i18n_lexicons"} //= {}
$app->{"i18n_lexicons"}
Truth table100%2/2
A
A= $app->{"i18n_lexicons"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$@ or ref $data ne "HASH"
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"
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | $app->{i18n_lexicons}{$lang} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$app->{"i18n_lexicons"}{$lang} //= {}
MC/DC100%1/1
$app->{"i18n_lexicons"}{$lang} //= {}
$app->{"i18n_lexicons"}{$lang}
Truth table100%2/2
A
A= $app->{"i18n_lexicons"}{$lang}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | my $out_dir = $app->home->child('share', 'i18n'); | |||||||||||||||||||||||||||
| 56 | 4 | ▶ | $out_dir->make_path unless -d $out_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||