| 1 | package Locale::TextDomain::OO::Extract::Process::Plugin::MO; ## no critic (TidyCode) | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 1 | ▶ | use Encode qw(find_encoding); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 1 | ▶ | use Locale::MO::File; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 1 | ▶ | use Locale::TextDomain::OO::Util::ExtractHeader; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 1 | ▶ | use Locale::TextDomain::OO::Util::JoinSplitLexiconKeys; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 1 | ▶ | use Moo; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | 1 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | 1 | ▶ | use MooX::Types::MooseLike::Base qw(HashRef Str); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '2.007'; | |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | 1 | has category => ( | |||||||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||||||
| 19 | isa => Str, | ||||||||||||||||||||||||
| 20 | lazy => 1, | ||||||||||||||||||||||||
| 21 | default => q{}, | ||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | 1 | has domain => ( | |||||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||||||
| 26 | isa => Str, | ||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||
| 28 | default => q{}, | ||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||
| 31 | 1 | has language => ( | |||||||||||||||||||||||
| 32 | is => 'rw', | ||||||||||||||||||||||||
| 33 | isa => Str, | ||||||||||||||||||||||||
| 34 | lazy => 1, | ||||||||||||||||||||||||
| 35 | default => 'i-default', | ||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | has project => ( | ||||||||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||||||||
| 40 | 0 | ▶ | isa => sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 41 | 0 | my $project = shift; | |||||||||||||||||||||||
| 42 | 0 | ▶ | defined $project | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | or return; | ||||||||||||||||||||||||
| 44 | 0 | return Str->($project); | |||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||
| 46 | 1 | ); | |||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | has lexicon_ref => ( | ||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||
| 50 | isa => HashRef, | ||||||||||||||||||||||||
| 51 | lazy => 1, | ||||||||||||||||||||||||
| 52 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 53 | 1 | ); | |||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | 0 | ▶ | sub clear { | ||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | 0 | $self->category( q{} ); | |||||||||||||||||||||||
| 59 | 0 | $self->domain( q{} ); | |||||||||||||||||||||||
| 60 | 0 | $self->language('i-default'); | |||||||||||||||||||||||
| 61 | 0 | $self->project(undef); | |||||||||||||||||||||||
| 62 | 0 | $self->lexicon_ref( {} ); | |||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | 0 | ▶ | sub slurp { | ||||||||||||||||||||||
|
Subroutine
slurp: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||
| 68 | 0 | my ( $self, $filename ) = @_; | |||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | 0 | ▶ | defined $filename | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 71 | or confess 'Undef is not a name of a mo file'; | ||||||||||||||||||||||||
| 72 | 0 | my $mo = Locale::MO::File->new( filename => $filename ); | |||||||||||||||||||||||
| 73 | 0 | $mo->read_file; | |||||||||||||||||||||||
| 74 | 0 | my $messages_ref = $mo->get_messages; | |||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | my $header = Locale::TextDomain::OO::Util::ExtractHeader | ||||||||||||||||||||||||
| 77 | ->instance | ||||||||||||||||||||||||
| 78 | ->extract_header_msgstr( | ||||||||||||||||||||||||
| 79 | $messages_ref->[0]->{msgstr} | ||||||||||||||||||||||||
| 80 | 0 | ▶ | || confess "No header found in file $filename", | ||||||||||||||||||||||
|
Condition0%0/2
$messages_ref->[0]{'msgstr'} || confess("No header found in file $filename")
MC/DC0%0/1
$messages_ref->[0]{'msgstr'} || confess("No header found in file $filename")
$messages_ref->[0]{'msgstr'}
Truth table0%0/2
A
A= $messages_ref->[0]{'msgstr'}
| |||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||
| 82 | 0 | my $encode_obj = find_encoding( $header->{charset} ); | |||||||||||||||||||||||
| 83 | 0 | my $nplurals = $header->{nplurals}; | |||||||||||||||||||||||
| 84 | 0 | my $plural = $header->{plural}; | |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||
| 86 | 0 | ▶ | my $decode_code = sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||
| 87 | 0 | my $text = shift; | |||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||
| 89 | 0 | ▶ | defined $text | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 90 | or return; | ||||||||||||||||||||||||
| 91 | 0 | ▶ | length $text | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 92 | or return q{}; | ||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||
| 94 | 0 | return $encode_obj->decode($text); | |||||||||||||||||||||||
| 95 | 0 | }; | |||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | 0 | my $key_util = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys->instance; | |||||||||||||||||||||||
| 98 | 0 | my $index = 0; | |||||||||||||||||||||||
| 99 | 0 | for my $message_ref ( @{$messages_ref} ) { | |||||||||||||||||||||||
| 100 | $self | ||||||||||||||||||||||||
| 101 | ->lexicon_ref | ||||||||||||||||||||||||
| 102 | ->{ | ||||||||||||||||||||||||
| 103 | $key_util->join_lexicon_key({ | ||||||||||||||||||||||||
| 104 | category => $self->category, | ||||||||||||||||||||||||
| 105 | domain => $self->domain, | ||||||||||||||||||||||||
| 106 | language => $self->language, | ||||||||||||||||||||||||
| 107 | project => $self->project, | ||||||||||||||||||||||||
| 108 | }) | ||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||
| 110 | ->{ | ||||||||||||||||||||||||
| 111 | $key_util->join_message_key({ | ||||||||||||||||||||||||
| 112 | msgctxt => scalar $decode_code->( $message_ref->{msgctxt} ), | ||||||||||||||||||||||||
| 113 | msgid => scalar $decode_code->( $message_ref->{msgid} ), | ||||||||||||||||||||||||
| 114 | msgid_plural => scalar $decode_code->( $message_ref->{msgid_plural} ), | ||||||||||||||||||||||||
| 115 | }) | ||||||||||||||||||||||||
| 116 | } = { | ||||||||||||||||||||||||
| 117 | ( | ||||||||||||||||||||||||
| 118 | $index++ | ||||||||||||||||||||||||
| 119 | ? () | ||||||||||||||||||||||||
| 120 | : ( | ||||||||||||||||||||||||
| 121 | nplurals => $nplurals, | ||||||||||||||||||||||||
| 122 | plural => $plural, | ||||||||||||||||||||||||
| 123 | ) | ||||||||||||||||||||||||
| 124 | ), | ||||||||||||||||||||||||
| 125 | ( | ||||||||||||||||||||||||
| 126 | exists $message_ref->{msgstr_plural} | ||||||||||||||||||||||||
| 127 | ? ( | ||||||||||||||||||||||||
| 128 | msgstr_plural => [ | ||||||||||||||||||||||||
| 129 | map { | ||||||||||||||||||||||||
| 130 | 0 | scalar $decode_code->( $message_ref->{msgstr_plural}->[$_] ); | |||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||
| 132 | 0 .. ( $nplurals - 1 ) | ||||||||||||||||||||||||
| 133 | ] | ||||||||||||||||||||||||
| 134 | ) | ||||||||||||||||||||||||
| 135 | 0 | ▶ | : ( msgstr => scalar $decode_code->( $message_ref->{msgstr} ) ) | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 136 | ), | ||||||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||
| 143 | 0 | ▶ | sub spew { | ||||||||||||||||||||||
|
Subroutine
spew: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||
| 144 | 0 | my ( $self, $filename ) = @_; | |||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | 0 | ▶ | defined $filename | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 147 | or confess 'Undef is not a name of a mo file'; | ||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||
| 149 | 0 | my $key_util = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys->instance; | |||||||||||||||||||||||
| 150 | 0 | my $lexicon_key = $key_util | |||||||||||||||||||||||
| 151 | ->instance | ||||||||||||||||||||||||
| 152 | ->join_lexicon_key({ | ||||||||||||||||||||||||
| 153 | category => $self->category, | ||||||||||||||||||||||||
| 154 | domain => $self->domain, | ||||||||||||||||||||||||
| 155 | language => $self->language, | ||||||||||||||||||||||||
| 156 | project => $self->project, | ||||||||||||||||||||||||
| 157 | }); | ||||||||||||||||||||||||
| 158 | 0 | ▶ | my $messages_ref = $self->lexicon_ref->{$lexicon_key} | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 159 | or confess sprintf | ||||||||||||||||||||||||
| 160 | 'No lexicon found for category "%s", domain "%s", language "%s" and project "%s"', | ||||||||||||||||||||||||
| 161 | $self->category, | ||||||||||||||||||||||||
| 162 | $self->domain, | ||||||||||||||||||||||||
| 163 | $self->language | ||||||||||||||||||||||||
| 164 | ( defined $self->project ? $self->project : 'undef' ); | ||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||
| 166 | my $header = Locale::TextDomain::OO::Util::ExtractHeader | ||||||||||||||||||||||||
| 167 | ->instance | ||||||||||||||||||||||||
| 168 | ->extract_header_msgstr( | ||||||||||||||||||||||||
| 169 | $messages_ref->{ q{} }->{msgstr} | ||||||||||||||||||||||||
| 170 | 0 | ▶ | || confess 'No header set.', | ||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$messages_ref->{''}{'msgstr'} || confess('No header set.')
MC/DC0%0/1
$messages_ref->{''}{'msgstr'} || confess('No header set.')
$messages_ref->{''}{'msgstr'}
Truth table0%0/2
A
A= $messages_ref->{''}{'msgstr'}
| |||||||||||||||||||||||||
| 171 | ) | ||||||||||||||||||||||||
| 172 | or confess sprintf | ||||||||||||||||||||||||
| 173 | 'No header found in lexicon of category "%s", domain "%s", language "%s" and project "%s"', | ||||||||||||||||||||||||
| 174 | $self->category, | ||||||||||||||||||||||||
| 175 | $self->domain, | ||||||||||||||||||||||||
| 176 | $self->language, | ||||||||||||||||||||||||
| 177 | ( defined $self->project ? $self->project : 'undef' ); | ||||||||||||||||||||||||
| 178 | 0 | my $charset = $header->{charset}; | |||||||||||||||||||||||
| 179 | 0 | my $encode_obj = find_encoding($charset); | |||||||||||||||||||||||
| 180 | 0 | my $nplurals = $header->{nplurals}; | |||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||
| 182 | my $message_ref = $self | ||||||||||||||||||||||||
| 183 | ->lexicon_ref | ||||||||||||||||||||||||
| 184 | ->{ | ||||||||||||||||||||||||
| 185 | 0 | $key_util->join_lexicon_key({ | |||||||||||||||||||||||
| 186 | category => $self->category, | ||||||||||||||||||||||||
| 187 | domain => $self->domain, | ||||||||||||||||||||||||
| 188 | language => $self->language, | ||||||||||||||||||||||||
| 189 | project => $self->project, | ||||||||||||||||||||||||
| 190 | }) | ||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | my %mo_key_of | ||||||||||||||||||||||||
| 194 | 0 | = map { $_ => undef } | |||||||||||||||||||||||
| 195 | qw( msgctxt msgid msgid_plural msgstr msgstr_plural ); | ||||||||||||||||||||||||
| 196 | my $mo = Locale::MO::File->new( | ||||||||||||||||||||||||
| 197 | filename => $filename, | ||||||||||||||||||||||||
| 198 | encoding => $charset, | ||||||||||||||||||||||||
| 199 | messages => [ | ||||||||||||||||||||||||
| 200 | map { ## no critic (ComplexMappings) | ||||||||||||||||||||||||
| 201 | 0 | my $return_ref = $key_util->join_message( $_, $message_ref->{$_} ); | |||||||||||||||||||||||
| 202 | 0 | delete @{$return_ref}{ | |||||||||||||||||||||||
| 203 | grep { | ||||||||||||||||||||||||
| 204 | 0 | ! exists $mo_key_of{$_}; | |||||||||||||||||||||||
| 205 | 0 | } keys %{$return_ref} | |||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||
| 207 | 0 | $return_ref; | |||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||
| 209 | sort | ||||||||||||||||||||||||
| 210 | 0 | keys %{$message_ref} | |||||||||||||||||||||||
| 211 | ], | ||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||
| 213 | 0 | $mo->write_file; | |||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||
| 215 | 0 | return; | |||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||
| 218 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||
| 220 | 1 | 1; | |||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||
| 222 | __END__ | ||||||||||||||||||||||||