| 1 | package Punk::Plugin::I18n; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | our $VERSION = '0.70'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Punk (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use File::Raw (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | # Loaded for its side effect: it registers the 'json' plugin that | ||||||||||||||||||||||||||||
| 13 | # File::Raw::slurp uses below. Nothing is imported. | ||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use File::Raw::JSON (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 266 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 266 | my ($class, $app, $opts) = @_; | |||||||||||||||||||||||||||
| 18 | # uncoverable condition right | ||||||||||||||||||||||||||||
| 19 | 266 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||
|
Condition100%1/2
$opts ||= {}
MC/DC100%0/1
$opts ||= {}
$opts
Truth table100%1/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 20 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 21 | 266 | ▶ | ref $opts eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | or die "Punk::Plugin::I18n: options must be a hashref\n"; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 266 | my $dir = $opts->{dir}; | |||||||||||||||||||||||||||
| 25 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 26 | # uncoverable condition left | ||||||||||||||||||||||||||||
| 27 | # uncoverable condition right | ||||||||||||||||||||||||||||
| 28 | 266 | ▶ | defined $dir && length $dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%1/3
defined $dir and length $dir
MC/DC100%0/2
defined $dir and length $dir
defined $dir
length $dir
Truth table100%1/3
A and B
A= defined $dirB= length $dir
| |||||||||||||||||||||||||||||
| 29 | or die "Punk::Plugin::I18n: `dir` is required - it is where the " | ||||||||||||||||||||||||||||
| 30 | . "catalogues live (plugin 'I18n' => { dir => 'i18n', " | ||||||||||||||||||||||||||||
| 31 | . "default => 'en' })\n"; | ||||||||||||||||||||||||||||
| 32 | 266 | ▶ | -d $dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | or die "Punk::Plugin::I18n: the catalogue directory '$dir' does not " | ||||||||||||||||||||||||||||
| 34 | . "exist - a missing directory is a deploy that shipped without " | ||||||||||||||||||||||||||||
| 35 | . "its translations, and it should fail here rather than serve " | ||||||||||||||||||||||||||||
| 36 | . "every page in the default language\n"; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 39 | 155 | ▶ | opendir my $dh, $dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | or die "Punk::Plugin::I18n: cannot read '$dir': $!\n"; | ||||||||||||||||||||||||||||
| 41 | 155 | my @files = sort grep { /\.json\z/ } readdir $dh; | |||||||||||||||||||||||||||
| 42 | 155 | closedir $dh; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | @files | ||||||||||||||||||||||||||||
| 45 | 155 | ▶ | or die "Punk::Plugin::I18n: no *.json catalogues in '$dir' - a plugin " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | . "with nothing to translate is a typo in `dir`, not a working " | ||||||||||||||||||||||||||||
| 47 | . "default\n"; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 154 | my %cats; | |||||||||||||||||||||||||||
| 50 | 154 | for my $f (@files) { | |||||||||||||||||||||||||||
| 51 | 202 | (my $tag = $f) =~ s/\.json\z//; | |||||||||||||||||||||||||||
| 52 | 202 | $tag =~ tr/A-Z/a-z/; | |||||||||||||||||||||||||||
| 53 | 202 | my $path = File::Spec->catfile($dir, $f); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 202 | my $data = eval { File::Raw::slurp($path, plugin => 'json') }; | |||||||||||||||||||||||||||
| 56 | 202 | ▶ | if (!defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | my $why = $@; | |||||||||||||||||||||||||||
| 58 | 1 | $why =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 59 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | die -r $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | ? "Punk::Plugin::I18n: '$path' will not parse: $why\n" | ||||||||||||||||||||||||||||
| 62 | : "Punk::Plugin::I18n: cannot read '$path': $!\n"; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 201 | ▶ | ref $data eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or die "Punk::Plugin::I18n: '$path' is not a JSON object - a " | ||||||||||||||||||||||||||||
| 66 | . "catalogue is a map of key to translation\n"; | ||||||||||||||||||||||||||||
| 67 | 200 | $cats{$tag} = $data; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 152 | my $ok = eval { $class->_build($app, \%cats, $opts); 1 }; | |||||||||||||||||||||||||||
| 71 | 152 | ▶ | return 1 if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 118 | my $why = $@; | |||||||||||||||||||||||||||
| 74 | 118 | $why =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 75 | 118 | die "$why (catalogues from '$dir')\n"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 13 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||