| 1 | package Locale::TextDomain::OO::Lexicon::Role::File; ## no critic (TidyCode) | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 22 | ▶ | use Carp qw(confess); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 22 | ▶ | use Encode qw(decode FB_CROAK); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 22 | ▶ | use English qw(-no_match_vars $OS_ERROR); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 22 | ▶ | use Locale::TextDomain::OO::Singleton::Lexicon; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 22 | ▶ | use Locale::TextDomain::OO::Util::ExtractHeader; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 22 | ▶ | use Locale::TextDomain::OO::Util::JoinSplitLexiconKeys; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 22 | ▶ | use Moo::Role; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 22 | ▶ | use MooX::Types::MooseLike::Base qw(CodeRef); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 22 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 22 | ▶ | use namespace::autoclean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 22 | our $VERSION = '1.034'; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 22 | with qw( | |||||||||||||||||
| 19 | Locale::TextDomain::OO::Lexicon::Role::GettextToMaketext | ||||||||||||||||||
| 20 | Locale::TextDomain::OO::Role::Logger | ||||||||||||||||||
| 21 | ); | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 22 | requires qw( | |||||||||||||||||
| 24 | read_messages | ||||||||||||||||||
| 25 | ); | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | has decode_code => ( | ||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||
| 29 | isa => CodeRef, | ||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||
| 31 | 20 | ▶ | default => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 32 | 772 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 33 | 772 | my ($charset, $text) = @_; | |||||||||||||||||
| 34 | 772 | ▶ | defined $text | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | or return $text; | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 772 | return decode( $charset, $text, FB_CROAK ); | |||||||||||||||||
| 38 | 20 | }; | |||||||||||||||||
| 39 | }, | ||||||||||||||||||
| 40 | 22 | ); | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 39 | ▶ | sub _decode_messages { | ||||||||||||||||
|
Subroutine
_decode_messages: called CC=10 SCAR=23.1
| |||||||||||||||||||
| 43 | 39 | my ($self, $messages_ref) = @_; | |||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 39 | my $charset = lc $messages_ref->[0]->{charset}; | |||||||||||||||||
| 46 | 39 | for my $value ( @{$messages_ref} ) { | |||||||||||||||||
| 47 | 296 | for my $key ( qw( msgid msgid_plural msgstr ) ) { | |||||||||||||||||
| 48 | 888 | ▶ | if ( exists $value->{$key} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 49 | 553 | for my $text ( $value->{$key} ) { | |||||||||||||||||
| 50 | 553 | $text = $self->decode_code->($charset, $text); | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | 296 | ▶ | if ( exists $value->{msgstr_plural} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 92 | my $got = @{ $value->{msgstr_plural} }; | |||||||||||||||||
| 56 | 92 | my $expected = $messages_ref->[0]->{nplurals}; | |||||||||||||||||
| 57 | $got <= $expected or confess sprintf | ||||||||||||||||||
| 58 | 'Count of msgstr_plural=%s but nplurals=%s for msgid="%s" msgid_plural="%s"', | ||||||||||||||||||
| 59 | $got, | ||||||||||||||||||
| 60 | $expected, | ||||||||||||||||||
| 61 | ( exists $value->{msgid} ? $value->{msgid} : q{} ), | ||||||||||||||||||
| 62 | 92 | ▶ | ( exists $value->{msgid_plural} ? $value->{msgid_plural} : q{} ); | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 63 | 91 | for my $text ( @{ $value->{msgstr_plural} } ) { | |||||||||||||||||
| 64 | 219 | $text = $self->decode_code->($charset, $text); | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | } | ||||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 38 | return; | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 25 | ▶ | sub _my_glob { | ||||||||||||||||
|
Subroutine
_my_glob: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 73 | 25 | my ($self, $file) = @_; | |||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 25 | my $dirname = $file->parent; | |||||||||||||||||
| 76 | 25 | my $filename = $file->basename; | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | # only one * allowed at all | ||||||||||||||||||
| 79 | 25 | my $dir_star_count = () = $dirname =~ m{ [*] }xmsg; | |||||||||||||||||
| 80 | 25 | my $file_star_count = () = $filename =~ m{ [*] }xmsg; | |||||||||||||||||
| 81 | 25 | my $count = $dir_star_count + $file_star_count; | |||||||||||||||||
| 82 | 25 | ▶ | $count | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 83 | or return $file; | ||||||||||||||||||
| 84 | 24 | ▶ | $count > 1 | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | and confess 'Only one * in dirname/filename is allowd to reference the language'; | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | # one * in filename | ||||||||||||||||||
| 88 | 24 | ▶ | if ( $file_star_count ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | 2 | ( my $file_regex = quotemeta $filename ) =~ s{\\[*]}{.*?}xms; | |||||||||||||||||
| 90 | return +( | ||||||||||||||||||
| 91 | 2 | sort $dirname->children( qr{\A $file_regex \z}xms ) | |||||||||||||||||
| 92 | ); | ||||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | # one * in dir | ||||||||||||||||||
| 96 | # split that dir into left, inner with * and right | ||||||||||||||||||
| 97 | 22 | my ( $left_dir, $inner_dir, $right_dir ) | |||||||||||||||||
| 98 | = split qr{( [^/*]* [*] [^/]* )}xms, $dirname; | ||||||||||||||||||
| 99 | 22 | ( my $inner_dir_regex = quotemeta $inner_dir ) =~ s{\\[*]}{.*?}xms; | |||||||||||||||||
| 100 | my @left_and_inner_dirs | ||||||||||||||||||
| 101 | 22 | = path($left_dir)->children( qr{$inner_dir_regex}xms ); | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | return +( | ||||||||||||||||||
| 104 | sort | ||||||||||||||||||
| 105 | grep { | ||||||||||||||||||
| 106 | 106 | $_->is_file; | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | map { | ||||||||||||||||||
| 109 | 22 | path("$_$right_dir")->child($filename); | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | @left_and_inner_dirs | ||||||||||||||||||
| 112 | ); | ||||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | 28 | ▶ | sub _run_extra_commands { | ||||||||||||||||
|
Subroutine
_run_extra_commands: called CC=5 SCAR=16.3
| |||||||||||||||||||
| 116 | 28 | my ($self, $identifier, $instance, $next_data_code) = @_; | |||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 28 | ▶ | if ( $identifier eq 'merge_lexicon' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 1 | my ( $from1, $from2, $to ) = ( | |||||||||||||||||
| 120 | $next_data_code->(), | ||||||||||||||||||
| 121 | $next_data_code->(), | ||||||||||||||||||
| 122 | $next_data_code->(), | ||||||||||||||||||
| 123 | ); | ||||||||||||||||||
| 124 | 1 | $instance->merge_lexicon( $from1, $from2, $to ); | |||||||||||||||||
| 125 | 1 | return 1; | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | 27 | ▶ | if ( $identifier eq 'copy_lexicon' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 128 | 0 | my ( $from, $to ) = ( $next_data_code->(), $next_data_code->() ); | |||||||||||||||||
| 129 | 0 | $instance->copy_lexicon( $from, $to ); | |||||||||||||||||
| 130 | 0 | return 1; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | 27 | ▶ | if ( $identifier eq 'move_lexicon' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 133 | 1 | my ( $from, $to ) = ( $next_data_code->(), $next_data_code->() ); | |||||||||||||||||
| 134 | 1 | $instance->move_lexicon( $from, $to ); | |||||||||||||||||
| 135 | 1 | return 1; | |||||||||||||||||
| 136 | } | ||||||||||||||||||
| 137 | 26 | ▶ | if ( $identifier eq 'delete_lexicon' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | 1 | my $name = $next_data_code->(); | |||||||||||||||||
| 139 | 1 | $instance->delete_lexicon($name); | |||||||||||||||||
| 140 | 1 | return 1; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 25 | return; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | 21 | ▶ | sub lexicon_ref { | ||||||||||||||||
|
Subroutine
lexicon_ref: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||
| 147 | 21 | my ($self, $file_lexicon_ref) = @_; | |||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | 21 | my $instance = Locale::TextDomain::OO::Singleton::Lexicon->instance; | |||||||||||||||||
| 150 | 21 | ▶ | $self->logger and $instance->logger( $self->logger ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 151 | my $search_dirs = $file_lexicon_ref->{search_dirs} | ||||||||||||||||||
| 152 | 21 | ▶ | or confess 'Hash key "search_dirs" expected'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 153 | 21 | my $header_util = Locale::TextDomain::OO::Util::ExtractHeader->instance; | |||||||||||||||||
| 154 | 21 | my $key_util = Locale::TextDomain::OO::Util::JoinSplitLexiconKeys->instance; | |||||||||||||||||
| 155 | 21 | my $data = $file_lexicon_ref->{data}; | |||||||||||||||||
| 156 | 21 | my $index = 0; | |||||||||||||||||
| 157 | DATA: | ||||||||||||||||||
| 158 | 21 | ▶ | while ( $index < @{ $file_lexicon_ref->{data} } ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | 28 | my $identifier = $data->[ $index++ ]; | |||||||||||||||||
| 160 | $self->_run_extra_commands( | ||||||||||||||||||
| 161 | $identifier, | ||||||||||||||||||
| 162 | $instance, | ||||||||||||||||||
| 163 | 6 | ▶ | sub { return $data->[ $index++ ] }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 164 | 28 | ▶ | ) and next DATA; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 165 | 25 | my ( $lexicon_key, $lexicon_value ) | |||||||||||||||||
| 166 | = ( $identifier, $data->[ $index++ ] ); | ||||||||||||||||||
| 167 | 25 | for my $dir ( @{ $search_dirs } ) { | |||||||||||||||||
| 168 | 25 | my $file = path( $dir, $lexicon_value ); | |||||||||||||||||
| 169 | 25 | my @files = $self->_my_glob($file); | |||||||||||||||||
| 170 | 25 | for ( @files ) { | |||||||||||||||||
| 171 | 40 | my $filename = $_->canonpath; | |||||||||||||||||
| 172 | 40 | my $lexicon_language_key = $lexicon_key; | |||||||||||||||||
| 173 | 40 | my $language = $filename; | |||||||||||||||||
| 174 | 40 | my @parts = split m{[*]}xms, $file; | |||||||||||||||||
| 175 | 40 | ▶ | if ( @parts == 2 ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 176 | 39 | substr $language, 0, length $parts[0], q{}; | |||||||||||||||||
| 177 | 39 | substr $language, - length $parts[1], length $parts[1], q{}; | |||||||||||||||||
| 178 | 39 | $lexicon_language_key =~ s{[*]}{$language}xms; | |||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | 40 | my $messages_ref = $self->read_messages($filename); | |||||||||||||||||
| 181 | my $header_msgstr = $messages_ref->[0]->{msgstr} | ||||||||||||||||||
| 182 | 40 | ▶ | or confess 'msgstr of header not found'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 183 | 40 | my $header_ref = $messages_ref->[0]; | |||||||||||||||||
| 184 | 40 | %{$header_ref} = ( | |||||||||||||||||
| 185 | msgid => $header_ref->{msgid}, | ||||||||||||||||||
| 186 | 40 | %{ $header_util->extract_header_msgstr( $header_ref->{msgstr} ) }, | |||||||||||||||||
| 187 | ); | ||||||||||||||||||
| 188 | $file_lexicon_ref->{gettext_to_maketext} | ||||||||||||||||||
| 189 | 40 | ▶ | and $self->gettext_to_maketext($messages_ref); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 190 | $file_lexicon_ref->{decode} | ||||||||||||||||||
| 191 | 40 | ▶ | and $self->_decode_messages($messages_ref); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 192 | $instance->data->{$lexicon_language_key} = { | ||||||||||||||||||
| 193 | map { ## no critic (ComplexMappings) | ||||||||||||||||||
| 194 | 303 | my $message_ref = $_; | |||||||||||||||||
| 195 | my $msg_key = $key_util->join_message_key({( | ||||||||||||||||||
| 196 | map { | ||||||||||||||||||
| 197 | 303 | $_ => delete $message_ref->{$_}; | |||||||||||||||||
| 198 | } | ||||||||||||||||||
| 199 | qw( msgctxt msgid msgid_plural ) | ||||||||||||||||||
| 200 | )}); | ||||||||||||||||||
| 201 | 303 | ( $msg_key => $message_ref ); | |||||||||||||||||
| 202 | 39 | } @{$messages_ref} | |||||||||||||||||
| 203 | }; | ||||||||||||||||||
| 204 | 39 | ▶ | $self->logger and $self->logger->( | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 205 | qq{Lexicon "$lexicon_language_key" loaded from file "$filename".}, | ||||||||||||||||||
| 206 | { | ||||||||||||||||||
| 207 | object => $self, | ||||||||||||||||||
| 208 | type => 'debug', | ||||||||||||||||||
| 209 | event => 'lexicon,load', | ||||||||||||||||||
| 210 | }, | ||||||||||||||||||
| 211 | ); | ||||||||||||||||||
| 212 | } | ||||||||||||||||||
| 213 | } | ||||||||||||||||||
| 214 | } | ||||||||||||||||||
| 215 | |||||||||||||||||||
| 216 | 20 | return $self; | |||||||||||||||||
| 217 | } | ||||||||||||||||||
| 218 | |||||||||||||||||||
| 219 | 22 | 1; | |||||||||||||||||
| 220 | |||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||