line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormFu::Localize; |
2
|
|
|
|
|
|
|
|
3
|
400
|
|
|
400
|
|
1536
|
use strict; |
|
400
|
|
|
|
|
510
|
|
|
400
|
|
|
|
|
10545
|
|
4
|
400
|
|
|
400
|
|
1375
|
use warnings; |
|
400
|
|
|
|
|
440
|
|
|
400
|
|
|
|
|
15556
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '2.05'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
400
|
|
|
400
|
|
1511
|
use HTML::FormFu::Util qw( require_class ); |
|
400
|
|
|
|
|
431
|
|
|
400
|
|
|
|
|
18505
|
|
9
|
400
|
|
|
400
|
|
2390
|
use List::Util 1.33 qw( any ); |
|
400
|
|
|
|
|
10486
|
|
|
400
|
|
|
|
|
19841
|
|
10
|
400
|
|
|
400
|
|
181316
|
use List::MoreUtils qw( pairwise ); |
|
400
|
|
|
|
|
2838595
|
|
|
400
|
|
|
|
|
2100
|
|
11
|
400
|
|
|
400
|
|
180390
|
use Scalar::Util qw( weaken isweak blessed ); |
|
400
|
|
|
|
|
586
|
|
|
400
|
|
|
|
|
21232
|
|
12
|
400
|
|
|
400
|
|
1594
|
use Exporter qw( import ); |
|
400
|
|
|
|
|
533
|
|
|
400
|
|
|
|
|
8891
|
|
13
|
400
|
|
|
400
|
|
1402
|
use Carp qw( croak ); |
|
400
|
|
|
|
|
529
|
|
|
400
|
|
|
|
|
216309
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub localize { |
26
|
60
|
|
|
60
|
0
|
139
|
my ( $self, @original_strings ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
60
|
|
|
|
|
128
|
@original_strings = grep {defined} @original_strings; |
|
68
|
|
|
|
|
240
|
|
29
|
|
|
|
|
|
|
|
30
|
60
|
100
|
|
|
|
193
|
if ( !$self->{has_default_localize_object} ) { |
31
|
40
|
|
|
|
|
155
|
$self->add_default_localize_object; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
60
|
|
|
|
|
88
|
my @localized_strings; |
35
|
|
|
|
|
|
|
|
36
|
60
|
|
|
|
|
85
|
foreach my $localize_data ( @{ $self->{localize_data} } ) { |
|
60
|
|
|
|
|
178
|
|
37
|
62
|
|
|
|
|
203
|
my $localize_object = $self->get_localize_object($localize_data); |
38
|
|
|
|
|
|
|
|
39
|
62
|
|
|
|
|
114
|
eval { |
40
|
62
|
|
|
|
|
276
|
@localized_strings = $localize_object->localize(@original_strings); |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
62
|
100
|
|
|
|
6863
|
next if $@; |
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
|
55
|
0
|
33
|
|
|
217
|
if ( !$localize_data->{dies_on_missing_key} |
|
|
|
33
|
|
|
|
|
51
|
|
|
|
|
|
|
&& scalar(@original_strings) == scalar(@localized_strings) |
52
|
0
|
|
|
0
|
|
0
|
&& scalar( any { !$_ } pairwise { $a eq $b } @original_strings, |
|
0
|
|
|
|
|
0
|
|
53
|
|
|
|
|
|
|
@localized_strings ) == 0 |
54
|
|
|
|
|
|
|
) |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
|
|
0
|
next; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
55
|
50
|
|
|
|
153
|
last if @localized_strings; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
60
|
100
|
|
|
|
910
|
if ( !@localized_strings ) { |
63
|
5
|
|
|
|
|
14
|
@localized_strings = @original_strings; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
60
|
100
|
|
|
|
368
|
return wantarray ? @localized_strings : $localized_strings[0]; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub add_localize_object { |
70
|
2
|
|
|
2
|
0
|
589
|
my ( $self, @objects ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
8
|
croak 'no arguments given' if @_ < 2; |
73
|
|
|
|
|
|
|
|
74
|
2
|
|
|
|
|
4
|
foreach my $localize_object (@objects) { |
75
|
2
|
|
|
|
|
3
|
my $dies_on_missing_key = undef; |
76
|
|
|
|
|
|
|
|
77
|
2
|
50
|
|
|
|
9
|
if ( blessed $localize_object) { |
78
|
2
|
|
|
|
|
7
|
$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
|
|
|
|
|
4
|
push @{ $self->{localize_data} }, |
|
2
|
|
|
|
|
9
|
|
85
|
|
|
|
|
|
|
{ |
86
|
|
|
|
|
|
|
localize_object => $localize_object, |
87
|
|
|
|
|
|
|
dies_on_missing_key => $dies_on_missing_key, |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
2
|
100
|
33
|
|
|
11
|
if ( !exists $self->{weaken_localize_object} |
|
|
|
66
|
|
|
|
|
91
|
|
|
|
|
|
|
|| $self->{weaken_localize_object} != 0 |
92
|
0
|
|
|
|
|
0
|
&& !isweak @{ $self->{localize_data} }[-1]->{localize_object} ) |
93
|
|
|
|
|
|
|
{ |
94
|
1
|
|
|
|
|
1
|
weaken @{ $self->{localize_data} }[-1]->{localize_object}; |
|
1
|
|
|
|
|
5
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
1
|
|
|
|
|
3
|
delete $self->{weaken_localize_object}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
6
|
return $self; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub add_localize_object_from_class { |
105
|
1
|
|
|
1
|
0
|
2
|
my ( $self, @class ) = @_; |
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
2
|
$self->{weaken_localize_object} = 0; |
108
|
|
|
|
|
|
|
return $self->add_localize_object( |
109
|
1
|
|
|
|
|
2
|
map { $self->get_localize_object_from_class($_) } @class ); |
|
1
|
|
|
|
|
3
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub get_localize_object_from_class { |
113
|
41
|
|
|
41
|
0
|
78
|
my ( $self, $class ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
41
|
|
|
|
|
159
|
require_class($class); |
116
|
|
|
|
|
|
|
|
117
|
41
|
|
|
|
|
1326
|
my $languages = $self->languages; |
118
|
|
|
|
|
|
|
|
119
|
41
|
|
|
|
|
308
|
return $class->get_handle(@$languages); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub get_localize_object_dies_on_missing_key { |
124
|
2
|
|
|
2
|
0
|
4
|
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
|
|
|
|
|
3
|
my $testkey = 'html_formfu_missing_key_test'; |
140
|
|
|
|
|
|
|
|
141
|
2
|
|
|
|
|
3
|
eval { $localize_object->localize($testkey) }; |
|
2
|
|
|
|
|
12
|
|
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
|
|
|
440
|
my $dies_on_missing_key = $@ ? 1 : 0; |
144
|
|
|
|
|
|
|
|
145
|
2
|
|
|
|
|
4
|
return $dies_on_missing_key; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub add_default_localize_object { |
149
|
40
|
|
|
40
|
0
|
70
|
my ($self) = @_; |
150
|
|
|
|
|
|
|
|
151
|
40
|
|
|
|
|
1141
|
my $localize_object |
152
|
|
|
|
|
|
|
= $self->get_localize_object_from_class( $self->localize_class ); |
153
|
|
|
|
|
|
|
|
154
|
40
|
|
|
|
|
6005
|
my $dies_on_missing_key = 1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# put FormFu localize object in first place |
157
|
40
|
|
|
|
|
70
|
unshift @{ $self->{localize_data} }, |
|
40
|
|
|
|
|
243
|
|
158
|
|
|
|
|
|
|
{ |
159
|
|
|
|
|
|
|
localize_object => $localize_object, |
160
|
|
|
|
|
|
|
dies_on_missing_key => $dies_on_missing_key, |
161
|
|
|
|
|
|
|
}; |
162
|
|
|
|
|
|
|
|
163
|
40
|
|
|
|
|
112
|
$self->{has_default_localize_object} = 1; |
164
|
|
|
|
|
|
|
|
165
|
40
|
|
|
|
|
79
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub get_localize_object { |
169
|
62
|
|
|
62
|
0
|
104
|
my ( $self, $localize_data ) = @_; |
170
|
|
|
|
|
|
|
|
171
|
62
|
50
|
|
|
|
332
|
if ( !blessed $localize_data->{localize_object} ) { |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
$localize_data->{localize_object} |
174
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
$localize_data->{localize_object} ); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
62
|
|
|
|
|
117
|
return $localize_data->{localize_object}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |