| 1 | 410 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Localize; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Localization | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 410 | $HTML::FormFu::Localize::VERSION = '2.09'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 410 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 410 | ▶ | use HTML::FormFu::Util qw( require_class ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 410 | ▶ | use List::Util 1.33 qw( any ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 410 | ▶ | use List::SomeUtils qw( pairwise ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 410 | ▶ | use Scalar::Util qw( weaken isweak blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 410 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 410 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 410 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||
| 16 | localize | ||||||||||||||||||||||||||||||||||||||||||
| 17 | add_localize_object | ||||||||||||||||||||||||||||||||||||||||||
| 18 | add_localize_object_from_class | ||||||||||||||||||||||||||||||||||||||||||
| 19 | get_localize_object_from_class | ||||||||||||||||||||||||||||||||||||||||||
| 20 | get_localize_object_dies_on_missing_key | ||||||||||||||||||||||||||||||||||||||||||
| 21 | add_default_localize_object | ||||||||||||||||||||||||||||||||||||||||||
| 22 | get_localize_object | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 62 | ▶ | sub localize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
localize: called CC=10 SCAR=24.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 62 | my ( $self, @original_strings ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 62 | @original_strings = grep {defined} @original_strings; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 62 | ▶ | if ( !$self->{has_default_localize_object} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 41 | $self->add_default_localize_object; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 62 | my @localized_strings; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 62 | foreach my $localize_data ( @{ $self->{localize_data} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 37 | 64 | my $localize_object = $self->get_localize_object($localize_data); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 64 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 40 | 64 | @localized_strings = $localize_object->localize(@original_strings); | |||||||||||||||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 64 | ▶ | next if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # NOTE: | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # As FormFu uses L10N to return messages based on artificial message | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # ids (instead of english language as message ids) the assumption | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # that we just got a result from Locale::Maketext with AUTO = 1 seems | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # to be safe when localize returns the same string as handed over. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 57 | ▶ | if ( !$localize_data->{dies_on_missing_key} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings and scalar &any(sub {
not $_;
}
, &pairwise(sub {
$a eq $b;
}
, \@original_strings, \@localized_strings)) == 0
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings
MC/DC0%0/3
not $localize_data->{"dies_on_missing_key"} and scalar @original_strings == scalar @localized_strings and scalar &any(sub {
not $_;
}
, &pairwise(sub {
$a eq $b;
}
, \@original_strings, \@localized_strings)) == 0
not $localize_data->{"dies_on_missing_key"}
scalar @original_strings == scalar @localized_strings
scalar &any(sub {
not $_;
}
, &pairwise(sub {
$a eq $b;
}
, \@original_strings, \@localized_strings)) == 0
Truth table25%1/4
not(A) and B and C
A= not $localize_data->{"dies_on_missing_key"}B= scalar @original_strings == scalar @localized_stringsC= scalar &any(sub {
not $_;
}
, &pairwise(sub {
$a eq $b;
}
, \@original_strings, \@localized_strings)) == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | && scalar(@original_strings) == scalar(@localized_strings) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | && scalar( any { !$_ } pairwise { $a eq $b } @original_strings, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | @localized_strings ) == 0 | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 57 | ▶ | last if @localized_strings; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 62 | ▶ | if ( !@localized_strings ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | @localized_strings = @original_strings; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 62 | ▶ | return wantarray ? @localized_strings : $localized_strings[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub add_localize_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_localize_object: called CC=7 SCAR=20.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | my ( $self, @objects ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | croak 'no arguments given' if @_ < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | foreach my $localize_object (@objects) { | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my $dies_on_missing_key = undef; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | if ( blessed $localize_object) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | $dies_on_missing_key | |||||||||||||||||||||||||||||||||||||||||
| 79 | = $self->get_localize_object_dies_on_missing_key( | ||||||||||||||||||||||||||||||||||||||||||
| 80 | $localize_object); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # add external localize object to the end of the list | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | push @{ $self->{localize_data} }, | |||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | localize_object => $localize_object, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | dies_on_missing_key => $dies_on_missing_key, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if ( !exists $self->{weaken_localize_object} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not exists $self->{"weaken_localize_object"} or $self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
$self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
MC/DC33%1/3
not exists $self->{"weaken_localize_object"} or $self->{"weaken_localize_object"} != 0 and not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
not exists $self->{"weaken_localize_object"}
$self->{"weaken_localize_object"} != 0
not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
Truth table50%2/4
not(A) or B and not(C)
A= not exists $self->{"weaken_localize_object"}B= $self->{"weaken_localize_object"} != 0C= not builtin::is_weak(@{$$self{"localize_data"};}[-1]->{"localize_object"})
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | || $self->{weaken_localize_object} != 0 | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | && !isweak @{ $self->{localize_data} }[-1]->{localize_object} ) | |||||||||||||||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | weaken @{ $self->{localize_data} }[-1]->{localize_object}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | delete $self->{weaken_localize_object}; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | sub add_localize_object_from_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_localize_object_from_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | my ( $self, @class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | $self->{weaken_localize_object} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 108 | return $self->add_localize_object( | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | map { $self->get_localize_object_from_class($_) } @class ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 42 | ▶ | sub get_localize_object_from_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_localize_object_from_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 42 | my ( $self, $class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 42 | require_class($class); | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 42 | my $languages = $self->languages; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 42 | return $class->get_handle(@$languages); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | sub get_localize_object_dies_on_missing_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_localize_object_dies_on_missing_key: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | my ( $self, $localize_object ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # NOTE: | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # Findout how this class reacts on missing entries | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # this is an issue with catalyst and po-style localization | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # (in pm-style localization, you could set | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # $Hello::I18N::en::Lexicon{_AUTO} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # to avoid autocreating missing keys) | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # HINT: | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # Never use underscores for the beginning of the testkey as they | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # will lead Locale::Maketext to croak even if _AUTO is on (1) as | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # Locale::Maketext useses underscores to identify text for | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # processing via the AUTO-function (_compile). | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | my $testkey = 'html_formfu_missing_key_test'; | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | eval { $localize_object->localize($testkey) }; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | my $dies_on_missing_key = $@ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | return $dies_on_missing_key; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 41 | ▶ | sub add_default_localize_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_default_localize_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 41 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 41 | my $localize_object | |||||||||||||||||||||||||||||||||||||||||
| 152 | = $self->get_localize_object_from_class( $self->localize_class ); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 41 | my $dies_on_missing_key = 1; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | # put FormFu localize object in first place | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 41 | unshift @{ $self->{localize_data} }, | |||||||||||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | localize_object => $localize_object, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | dies_on_missing_key => $dies_on_missing_key, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 41 | $self->{has_default_localize_object} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 41 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 64 | ▶ | sub get_localize_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_localize_object: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 64 | my ( $self, $localize_data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 64 | ▶ | if ( !blessed $localize_data->{localize_object} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | $localize_data->{localize_object} | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | = $self->get_localize_object_from_class( $self->localize_class ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | $localize_data->{dies_on_missing_key} | ||||||||||||||||||||||||||||||||||||||||||
| 177 | = $self->get_localize_object_dies_on_missing_key( | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $localize_data->{localize_object} ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 64 | return $localize_data->{localize_object}; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 410 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||||||||||||||||