line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
404
|
|
|
404
|
|
2952
|
use strict; |
|
404
|
|
|
|
|
899
|
|
|
404
|
|
|
|
|
21309
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::FormFu::Localize; |
4
|
|
|
|
|
|
|
# ABSTRACT: Localization |
5
|
|
|
|
|
|
|
$HTML::FormFu::Localize::VERSION = '2.07'; |
6
|
404
|
|
|
404
|
|
2455
|
use warnings; |
|
404
|
|
|
|
|
847
|
|
|
404
|
|
|
|
|
13879
|
|
7
|
|
|
|
|
|
|
|
8
|
404
|
|
|
404
|
|
2354
|
use HTML::FormFu::Util qw( require_class ); |
|
404
|
|
|
|
|
838
|
|
|
404
|
|
|
|
|
23246
|
|
9
|
404
|
|
|
404
|
|
2498
|
use List::Util 1.33 qw( any ); |
|
404
|
|
|
|
|
11639
|
|
|
404
|
|
|
|
|
24350
|
|
10
|
404
|
|
|
404
|
|
228951
|
use List::MoreUtils qw( pairwise ); |
|
404
|
|
|
|
|
5022629
|
|
|
404
|
|
|
|
|
2738
|
|
11
|
404
|
|
|
404
|
|
426885
|
use Scalar::Util qw( weaken isweak blessed ); |
|
404
|
|
|
|
|
1030
|
|
|
404
|
|
|
|
|
26834
|
|
12
|
404
|
|
|
404
|
|
2519
|
use Exporter qw( import ); |
|
404
|
|
|
|
|
892
|
|
|
404
|
|
|
|
|
10483
|
|
13
|
404
|
|
|
404
|
|
2232
|
use Carp qw( croak ); |
|
404
|
|
|
|
|
885
|
|
|
404
|
|
|
|
|
325635
|
|
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
|
62
|
|
|
62
|
0
|
253
|
my ( $self, @original_strings ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
62
|
|
|
|
|
217
|
@original_strings = grep {defined} @original_strings; |
|
70
|
|
|
|
|
312
|
|
29
|
|
|
|
|
|
|
|
30
|
62
|
100
|
|
|
|
269
|
if ( !$self->{has_default_localize_object} ) { |
31
|
41
|
|
|
|
|
262
|
$self->add_default_localize_object; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
62
|
|
|
|
|
152
|
my @localized_strings; |
35
|
|
|
|
|
|
|
|
36
|
62
|
|
|
|
|
125
|
foreach my $localize_data ( @{ $self->{localize_data} } ) { |
|
62
|
|
|
|
|
241
|
|
37
|
64
|
|
|
|
|
253
|
my $localize_object = $self->get_localize_object($localize_data); |
38
|
|
|
|
|
|
|
|
39
|
64
|
|
|
|
|
151
|
eval { |
40
|
64
|
|
|
|
|
375
|
@localized_strings = $localize_object->localize(@original_strings); |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
64
|
100
|
|
|
|
9942
|
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
|
57
|
0
|
33
|
|
|
266
|
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
|
57
|
50
|
|
|
|
212
|
last if @localized_strings; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
62
|
100
|
|
|
|
213
|
if ( !@localized_strings ) { |
63
|
5
|
|
|
|
|
16
|
@localized_strings = @original_strings; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
62
|
100
|
|
|
|
430
|
return wantarray ? @localized_strings : $localized_strings[0]; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub add_localize_object { |
70
|
2
|
|
|
2
|
0
|
1055
|
my ( $self, @objects ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
10
|
croak 'no arguments given' if @_ < 2; |
73
|
|
|
|
|
|
|
|
74
|
2
|
|
|
|
|
6
|
foreach my $localize_object (@objects) { |
75
|
2
|
|
|
|
|
5
|
my $dies_on_missing_key = undef; |
76
|
|
|
|
|
|
|
|
77
|
2
|
50
|
|
|
|
10
|
if ( blessed $localize_object) { |
78
|
2
|
|
|
|
|
8
|
$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
|
|
|
|
|
5
|
push @{ $self->{localize_data} }, |
|
2
|
|
|
|
|
12
|
|
85
|
|
|
|
|
|
|
{ |
86
|
|
|
|
|
|
|
localize_object => $localize_object, |
87
|
|
|
|
|
|
|
dies_on_missing_key => $dies_on_missing_key, |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
2
|
100
|
33
|
|
|
14
|
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
|
|
|
|
|
3
|
weaken @{ $self->{localize_data} }[-1]->{localize_object}; |
|
1
|
|
|
|
|
6
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
1
|
|
|
|
|
4
|
delete $self->{weaken_localize_object}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
9
|
return $self; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub add_localize_object_from_class { |
105
|
1
|
|
|
1
|
0
|
4
|
my ( $self, @class ) = @_; |
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
4
|
$self->{weaken_localize_object} = 0; |
108
|
|
|
|
|
|
|
return $self->add_localize_object( |
109
|
1
|
|
|
|
|
72
|
map { $self->get_localize_object_from_class($_) } @class ); |
|
1
|
|
|
|
|
8
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub get_localize_object_from_class { |
113
|
42
|
|
|
42
|
0
|
150
|
my ( $self, $class ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
42
|
|
|
|
|
235
|
require_class($class); |
116
|
|
|
|
|
|
|
|
117
|
42
|
|
|
|
|
1589
|
my $languages = $self->languages; |
118
|
|
|
|
|
|
|
|
119
|
42
|
|
|
|
|
367
|
return $class->get_handle(@$languages); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub get_localize_object_dies_on_missing_key { |
124
|
2
|
|
|
2
|
0
|
5
|
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
|
|
|
|
|
4
|
my $testkey = 'html_formfu_missing_key_test'; |
140
|
|
|
|
|
|
|
|
141
|
2
|
|
|
|
|
4
|
eval { $localize_object->localize($testkey) }; |
|
2
|
|
|
|
|
63
|
|
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
|
|
|
605
|
my $dies_on_missing_key = $@ ? 1 : 0; |
144
|
|
|
|
|
|
|
|
145
|
2
|
|
|
|
|
5
|
return $dies_on_missing_key; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub add_default_localize_object { |
149
|
41
|
|
|
41
|
0
|
134
|
my ($self) = @_; |
150
|
|
|
|
|
|
|
|
151
|
41
|
|
|
|
|
1334
|
my $localize_object |
152
|
|
|
|
|
|
|
= $self->get_localize_object_from_class( $self->localize_class ); |
153
|
|
|
|
|
|
|
|
154
|
41
|
|
|
|
|
12883
|
my $dies_on_missing_key = 1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# put FormFu localize object in first place |
157
|
41
|
|
|
|
|
101
|
unshift @{ $self->{localize_data} }, |
|
41
|
|
|
|
|
296
|
|
158
|
|
|
|
|
|
|
{ |
159
|
|
|
|
|
|
|
localize_object => $localize_object, |
160
|
|
|
|
|
|
|
dies_on_missing_key => $dies_on_missing_key, |
161
|
|
|
|
|
|
|
}; |
162
|
|
|
|
|
|
|
|
163
|
41
|
|
|
|
|
153
|
$self->{has_default_localize_object} = 1; |
164
|
|
|
|
|
|
|
|
165
|
41
|
|
|
|
|
127
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub get_localize_object { |
169
|
64
|
|
|
64
|
0
|
185
|
my ( $self, $localize_data ) = @_; |
170
|
|
|
|
|
|
|
|
171
|
64
|
50
|
|
|
|
501
|
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
|
64
|
|
|
|
|
192
|
return $localize_data->{localize_object}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
__END__ |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=pod |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=encoding UTF-8 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 NAME |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
HTML::FormFu::Localize - Localization |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 VERSION |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
version 2.07 |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 AUTHOR |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Carl Franks <cpan@fireartist.com> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This software is copyright (c) 2018 by Carl Franks. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
209
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |