| 1 | package Locale::Babelfish; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl I18n using https://github.com/nodeca/babelfish format. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '2.13'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw/ confess /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use File::Find qw( find ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use YAML::SyckWrapper qw( load_yaml ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Locale::Babelfish::Phrase::Parser (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Locale::Babelfish::Phrase::Compiler (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use parent qw( Class::Accessor::Fast ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | MTIME_INDEX => 9, | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | __PACKAGE__->mk_accessors( qw( | |||||||||||||||||||||||||||||||||||||||||
| 29 | dictionaries | ||||||||||||||||||||||||||||||||||||||||||
| 30 | fallbacks | ||||||||||||||||||||||||||||||||||||||||||
| 31 | fallback_cache | ||||||||||||||||||||||||||||||||||||||||||
| 32 | dirs | ||||||||||||||||||||||||||||||||||||||||||
| 33 | suffix | ||||||||||||||||||||||||||||||||||||||||||
| 34 | default_locale | ||||||||||||||||||||||||||||||||||||||||||
| 35 | watch | ||||||||||||||||||||||||||||||||||||||||||
| 36 | watchers | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | my $parser = Locale::Babelfish::Phrase::Parser->new(); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | my $compiler = Locale::Babelfish::Phrase::Compiler->new(); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub _built_config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_built_config: called CC=5 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | my ( $cfg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | dictionaries => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | fallbacks => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | fallback_cache => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | suffix => $cfg->{suffix} // 'yaml', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | default_locale => $cfg->{default_locale} // 'en_US', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | watch => $cfg->{watch} || 0, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | watchers => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | %{ $cfg // {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
$cfg->{"suffix"} // "yaml"
$cfg->{"default_locale"} // "en_US"
$cfg->{'watch'} || 0
$cfg // {}
MC/DC0%0/1
$cfg->{"suffix"} // "yaml"
$cfg->{"suffix"}
MC/DC0%0/1
$cfg->{"default_locale"} // "en_US"
$cfg->{"default_locale"}
MC/DC0%0/1
$cfg->{'watch'} || 0
$cfg->{'watch'}
MC/DC0%0/1
$cfg // {}
$cfg
Truth table50%1/2
A
A= $cfg->{"suffix"}
Truth table50%1/2
A
A= $cfg->{"default_locale"}
Truth table50%1/2
A
A= $cfg->{'watch'}
Truth table50%1/2
A
A= $cfg
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | my ( $class, $cfg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | _cfg => $cfg, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | %{ _built_config( $cfg ) }, | |||||||||||||||||||||||||||||||||||||||||
| 63 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | $self->load_dictionaries; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | $self->locale( $self->{default_locale} ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub locale { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
locale: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | return $self->{locale} if scalar(@_) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | $self->{locale} = $self->detect_locale( $_[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub on_watcher_change { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_watcher_change: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | delete $self->{keys %$self}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my %new_cfg = %{ _built_config( $self->{_cfg} ) }; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | while( my ( $key, $value ) = each %new_cfg ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->load_dictionaries; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->locale( $self->{default_locale} ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub look_for_watchers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
look_for_watchers: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | return unless $self->{watch}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $ok = 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | while ( my ( $file, $mtime ) = each %{ $self->watchers } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $new_mtime = (stat($file))[MTIME_INDEX]; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( !defined( $mtime ) || !defined( $new_mtime ) || $new_mtime != $mtime ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $mtime && defined $new_mtime or $new_mtime != $mtime
defined $mtime && defined $new_mtime
MC/DC0%0/3
not defined $mtime && defined $new_mtime or $new_mtime != $mtime
defined $mtime
defined $new_mtime
$new_mtime != $mtime
Truth table0%0/4
not(A && B) or C
A= defined $mtimeB= defined $new_mtimeC= $new_mtime != $mtime
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $ok = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $self->on_watcher_change(); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 48 | ▶ | sub t_or_undef { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
t_or_undef: called CC=17 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 48 | my ( $self, $dictname_key, $params, $custom_locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 48 | ▶ | confess 'No dictname_key' unless $dictname_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | # запрещаем ключи не ASCII | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 48 | ▶ | confess("wrong dictname_key: $dictname_key") if $dictname_key =~ m/\P{ASCII}/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 48 | ▶ | my $locale = $custom_locale ? $self->detect_locale( $custom_locale ) : $self->{locale}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 48 | my $r = $self->{dictionaries}->{$locale}->{$dictname_key}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 48 | ▶ | if ( defined $r ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 45 | ▶ | if ( ref( $r ) eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 11 | $self->{dictionaries}->{$locale}->{$dictname_key} = $r = $compiler->compile( | |||||||||||||||||||||||||||||||||||||||||
| 120 | $parser->parse( $$r, $locale ), | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | elsif ( ref( $r ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 25 | $self->{dictionaries}{$locale}{$dictname_key} = $r = _process_list_items( $r, $locale ); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # fallbacks | ||||||||||||||||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | ▶ | $self->{fallback_cache}->{$locale} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"fallback_cache"}{$locale} //= {}
MC/DC100%1/1
$self->{"fallback_cache"}{$locale} //= {}
$self->{"fallback_cache"}{$locale}
Truth table100%2/2
A
A= $self->{"fallback_cache"}{$locale}
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | # в кэше может быть undef, чтобы не пробегать локали для несуществующих ключей повторно. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | ▶ | if ( exists $self->{fallback_cache}->{$locale}->{$dictname_key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $r = $self->{fallback_cache}->{$locale}->{$dictname_key}; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | my @fallback_locales = @{ $self->{fallbacks}->{$locale} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"fallbacks"}{$locale} // []
MC/DC100%1/1
$self->{"fallbacks"}{$locale} // []
$self->{"fallbacks"}{$locale}
Truth table100%2/2
A
A= $self->{"fallbacks"}{$locale}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | for ( @fallback_locales ) { | |||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | $r = $self->{dictionaries}->{$_}->{$dictname_key}; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ( defined $r ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | ▶ | if ( ref( $r ) eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $self->{dictionaries}->{$_}->{$dictname_key} = $r = $compiler->compile( | |||||||||||||||||||||||||||||||||||||||||
| 141 | $parser->parse( $$r, $_ ), | ||||||||||||||||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | elsif ( ref( $r ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $self->{dictionaries}{$locale}{$dictname_key} = $r | |||||||||||||||||||||||||||||||||||||||||
| 146 | = _process_list_items( $r, $locale ); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | $self->{fallback_cache}->{$locale}->{$dictname_key} = $r; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 48 | ▶ | if ( ref( $r ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 43 | my $flat_params = {}; | |||||||||||||||||||||||||||||||||||||||||
| 157 | # Переводим хэш параметров в "плоскую форму" так как в babelfish они имеют вид params.key.subkey | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 43 | ▶ | if ( defined($params) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | # переданный скаляр превращаем в хэш { count, value }. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 36 | ▶ | if ( ref($params) eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | $flat_params = { | |||||||||||||||||||||||||||||||||||||||||
| 162 | count => $params, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | value => $params, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 33 | _flat_hash_keys( $params, '', $flat_params ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 43 | return $r->( $flat_params ); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | return $r; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 48 | ▶ | sub t { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
t: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 48 | ▶ | return $self->t_or_undef( @_ ) || "[$_[0]]"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->t_or_undef(@_) || "[$_[0]]"
MC/DC100%1/1
$self->t_or_undef(@_) || "[$_[0]]"
$self->t_or_undef(@_)
Truth table100%2/2
A
A= $self->t_or_undef(@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | sub has_any_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_any_value: called CC=10 SCAR=38.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | my ( $self, $dictname_key, $custom_locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | # запрещаем ключи не ASCII | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | ▶ | confess("wrong dictname_key: $dictname_key") if $dictname_key =~ m/\P{ASCII}/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | my $locale = $custom_locale ? $self->detect_locale( $custom_locale ) : $self->{locale}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | return 1 if $self->{dictionaries}->{$locale}->{$dictname_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $self->{fallback_cache}->{$locale} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"fallback_cache"}{$locale} //= {}
MC/DC0%0/1
$self->{"fallback_cache"}{$locale} //= {}
$self->{"fallback_cache"}{$locale}
Truth table0%0/2
A
A= $self->{"fallback_cache"}{$locale}
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | return ( ( defined $self->{fallback_cache}->{$locale}->{$dictname_key} ) ? 1 : 0 ) | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if exists $self->{fallback_cache}->{$locale}->{$dictname_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | my @fallback_locales = @{ $self->{fallbacks}->{$locale} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"fallbacks"}{$locale} // []
MC/DC0%0/1
$self->{"fallbacks"}{$locale} // []
$self->{"fallbacks"}{$locale}
Truth table0%0/2
A
A= $self->{"fallbacks"}{$locale}
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | for ( @fallback_locales ) { | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return 1 if defined $self->{dictionaries}->{$_}->{$dictname_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | ▶ | sub load_dictionaries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_dictionaries: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | for my $dir ( @{$self->dirs} ) { | |||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | my $fdir = File::Spec->rel2abs( $dir ); | |||||||||||||||||||||||||||||||||||||||||
| 212 | find( { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | follow => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | no_chdir => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 28 | ▶ | wanted => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 28 | my $file = File::Spec->rel2abs( $File::Find::name ); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 28 | ▶ | return unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 16 | my ( $volume, $directories, $base ) = File::Spec->splitpath( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 16 | my @tmp = split m/\./, $base; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 16 | my $cur_suffix = pop @tmp; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 16 | ▶ | return unless $cur_suffix eq $self->suffix; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 16 | my $lang = pop @tmp; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 16 | ▶ | pop @tmp if $tmp[-1] eq 'tt'; # словари вида formatting.tt.ru_RU.yaml - имеют имя formatting | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 16 | ▶ | if ( $tmp[-1] eq 'js') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | # словари .js перекрывают одноимённые словари без суффикса | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # если это нежелательное поведение - словарь с суффиксом .tt перекроет одноимённый .js, и будет доступен только на сервере | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | pop @tmp; # словари вида formatting.js.ru_RU.yaml - имеют имя formatting | |||||||||||||||||||||||||||||||||||||||||
| 231 | # и не загружаются, если есть аналогичный tt. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | return if -f File::Spec->catpath( $volume, $directories, join('.', @tmp). ".tt.$lang.$cur_suffix" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 16 | my $dictname = join('.', @tmp); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 16 | my $subdir = File::Spec->catpath( $volume, $directories, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 16 | ▶ | if ( $subdir =~ m/\A\Q$fdir\E[\\\/](.+)\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | $dictname = "$1$dictname"; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 16 | $self->load_dictionary($dictname, $lang, $file); | |||||||||||||||||||||||||||||||||||||||||
| 241 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | }, $dir ); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | $self->prepare_to_compile; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 16 | ▶ | sub load_dictionary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_dictionary: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 16 | my ( $self, $dictname, $lang, $file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 16 | ▶ | $self->dictionaries->{$lang} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->dictionaries->{$lang} //= {}
MC/DC100%1/1
$self->dictionaries->{$lang} //= {}
$self->dictionaries->{$lang}
Truth table100%2/2
A
A= $self->dictionaries->{$lang}
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 16 | my $yaml = load_yaml( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 16 | _flat_hash_keys( $yaml, "$dictname.", $self->dictionaries->{$lang} ); | |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 16 | ▶ | return unless $self->watch; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $self->watchers->{$file} = (stat($file))[MTIME_INDEX]; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 90 | ▶ | sub phrase_need_compilation { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
phrase_need_compilation: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 90 | my ( undef, $phrase, $key ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 90 | ▶ | die "L10N: $key is undef" unless defined $phrase; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 90 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
ref $phrase eq '' && $phrase =~ / (?: \(\( | \#\{ | \\\\ )/x
MC/DC100%2/2
ref $phrase eq '' && $phrase =~ / (?: \(\( | \#\{ | \\\\ )/x
ref $phrase eq ''
$phrase =~ / (?: \(\( | \#\{ | \\\\ )/x
Truth table100%3/3
A && B
A= ref $phrase eq ''B= $phrase =~ / (?: \(\( | \#\{ | \\\\ )/x
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | && ref($phrase) eq '' | ||||||||||||||||||||||||||||||||||||||||||
| 268 | && $phrase =~ m/ (?: \(\( | \#\{ | \\\\ )/x | ||||||||||||||||||||||||||||||||||||||||||
| 269 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | sub prepare_to_compile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_to_compile: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | ▶ | while ( my ($lang, $dic) = each(%{ $self->{dictionaries} }) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 4 | ▶ | while ( my ($key, $value) = each(%$dic) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 90 | ▶ | if ( $self->phrase_need_compilation( $value, $key ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 30 | $dic->{$key} = \$value; # отложенная компиляция | |||||||||||||||||||||||||||||||||||||||||
| 280 | #my $ast = $parser->parse($value, $lang); | ||||||||||||||||||||||||||||||||||||||||||
| 281 | #$dic->{$key} = $compiler->compile( $ast ); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 7 | ▶ | sub detect_locale { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detect_locale: called CC=8 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 7 | my ( $self, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | ▶ | return $locale if $self->dictionaries->{$locale}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | my @alt_locales = grep { $_ =~ m/\A\Q$locale\E[\-_]/i } keys %{ $self->dictionaries }; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | ▶ | confess "only one alternative locale allowed: ", join ',', @alt_locales | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | if @alt_locales > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | my $alt_locale = $alt_locales[0]; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 2 | ▶ | if ( $alt_locale && $self->dictionaries->{$alt_locale} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$alt_locale and $self->dictionaries->{$alt_locale}
MC/DC0%0/2
$alt_locale and $self->dictionaries->{$alt_locale}
$alt_locale
$self->dictionaries->{$alt_locale}
Truth table33%1/3
A and B
A= $alt_localeB= $self->dictionaries->{$alt_locale}
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | # сделаем locale dictionary ссылкой на alt locale dictinary. | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # это ускорит работу всех t с указанием языка типа "ru" вместо локали "ru_RU". | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | $self->dictionaries->{$locale} = $self->dictionaries->{$alt_locale}; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | $self->fallback_cache->{$locale} = $self->fallback_cache->{$alt_locale} | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | ▶ | if exists $self->fallback_cache->{$alt_locale}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | $self->fallbacks->{$locale} = $self->fallbacks->{$alt_locale} | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | ▶ | if exists $self->fallbacks->{$alt_locale}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | return $locale; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | return $self->{default_locale} if $self->dictionaries->{ $self->{default_locale} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | confess "bad locale: $locale and bad default_locale: $self->{default_locale}."; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 2 | ▶ | sub set_fallback { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_fallback: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 2 | my ( $self, $locale, @fallback_locales ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 2 | ▶ | return unless scalar( @fallback_locales ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | $locale = $self->detect_locale( $locale ); | |||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | @fallback_locales = @{ $fallback_locales[0] } if 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @fallback_locales == 1 and ref $fallback_locales[0] eq "ARRAY"
MC/DC0%0/2
scalar @fallback_locales == 1 and ref $fallback_locales[0] eq "ARRAY"
scalar @fallback_locales == 1
ref $fallback_locales[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @fallback_locales == 1B= ref $fallback_locales[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | && scalar( @fallback_locales ) == 1 | ||||||||||||||||||||||||||||||||||||||||||
| 323 | && ref( $fallback_locales[0] ) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 324 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | $self->fallbacks->{ $locale } = \@fallback_locales; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | delete $self->{fallback_cache}->{ $locale }; | |||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 79 | ▶ | sub _flat_hash_keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flat_hash_keys: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 79 | my ( $hash, $prefix, $store ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 79 | ▶ | while ( my ($key, $value) = each(%$hash) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 173 | ▶ | if (ref($value) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 30 | _flat_hash_keys( $value, "$prefix$key.", $store ); | |||||||||||||||||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 143 | $store->{"$prefix$key"} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 79 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 25 | ▶ | sub _process_list_items { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_list_items: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 25 | my ( $r, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | 25 | my @compiled_items; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 25 | for my $item ( @{ $r } ) { | |||||||||||||||||||||||||||||||||||||||||
| 351 | 42 | ▶ | if ( ref $item eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 36 | push @compiled_items, _process_nested_hash_item( $item, $locale ); | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | ▶ | elsif ( ref $item ne 'HASH' && defined $item ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $item ne "HASH" and defined $item
MC/DC50%1/2
ref $item ne "HASH" and defined $item
ref $item ne "HASH"
defined $item
Truth table66%2/3
A and B
A= ref $item ne "HASH"B= defined $item
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 4 | push @compiled_items, $compiler->compile( $parser->parse( $item, $locale ) ); | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 2 | push @compiled_items, $item; | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 26 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 26 | my $results = []; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 26 | my @params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 26 | for my $item ( @compiled_items ) { | |||||||||||||||||||||||||||||||||||||||||
| 367 | 44 | push @{ $results }, __get_compiled_value($item, @params); | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 26 | return $results; | |||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 120 | ▶ | sub __get_compiled_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__get_compiled_value: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 120 | my ($item, @params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 373 | 120 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 120 | ▶ | if ( ref( $item ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 16 | $result = $item->(@params); | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | # Нужно скомпилить значения в хэшрефе | ||||||||||||||||||||||||||||||||||||||||||
| 379 | elsif ( ref( $item ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 66 | my $new_item = {}; | |||||||||||||||||||||||||||||||||||||||||
| 381 | 66 | ▶ | while ( my ( $key, $value ) = each ( %$item ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 160 | ▶ | if ( ref ($value) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 36 | $new_item->{$key} = $value->(@params); | |||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | elsif ( ref( $value ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 16 | my $sub_item = {}; | |||||||||||||||||||||||||||||||||||||||||
| 387 | 16 | foreach my $sub_key (keys %$value) { | |||||||||||||||||||||||||||||||||||||||||
| 388 | 48 | $sub_item->{$sub_key} = __get_compiled_value($value->{$sub_key}, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 16 | $new_item->{$key} = $sub_item; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | elsif ( ref( $value ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 16 | my $sub_item = []; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 16 | foreach my $sub_value (@$value) { | |||||||||||||||||||||||||||||||||||||||||
| 395 | 28 | push @$sub_item, __get_compiled_value($sub_value, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 16 | $new_item->{$key} = $sub_item; | |||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 92 | $new_item->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 66 | $result = $new_item; | |||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 38 | $result = $item; | |||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | 120 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | return $results; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 25 | }; | |||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | 80 | ▶ | sub _process_nested_hash_item { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_nested_hash_item: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 80 | my ( $hashref, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | 80 | ▶ | while ( my ( $key, $value ) = each ( %$hashref ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 206 | my $compiled_value = _process_nested_item_value( $value, $locale ); | |||||||||||||||||||||||||||||||||||||||||
| 422 | 206 | $hashref->{$key} = $compiled_value; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | 80 | return $hashref; | |||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 16 | ▶ | sub _process_nested_array_item { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_nested_array_item: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 16 | my ( $arrayref, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 16 | my $arrayref_out = []; | |||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 16 | foreach my $value (@$arrayref ) { | |||||||||||||||||||||||||||||||||||||||||
| 433 | 28 | my $compiled_value = _process_nested_item_value( $value, $locale ); | |||||||||||||||||||||||||||||||||||||||||
| 434 | 28 | push @$arrayref_out, $compiled_value; | |||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 16 | return $arrayref_out; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 234 | ▶ | sub _process_nested_item_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_nested_item_value: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 234 | my ( $item_value, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 234 | ▶ | my $compiled_value = ref $item_value eq 'HASH' ? _process_nested_hash_item( $item_value, $locale ) : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | ref $item_value eq 'ARRAY' ? _process_nested_array_item( $item_value, $locale ) : | ||||||||||||||||||||||||||||||||||||||||||
| 445 | $compiler->compile( $parser->parse( $item_value, $locale ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 234 | return $compiled_value; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | __END__ | ||||||||||||||||||||||||||||||||||||||||||