Valiant/I18N.pm

filter: Statementstmt 93.2% 138 / 148 Branchbran 78.8% 41 / 52 Conditioncond 74.1% 23 / 31 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 89.6% 26 / 29 Podpod 11.1% 1 / 9 totaltotal 82.9% 239 / 288 CC 40 SCAR 21.4
CC
40
Cov
87%
translate
20.9
_lookup_translation_by_count
10.3
add_locale_path
5.0
CRAP 8.5
« Valiant/HTML/Util/FormTags.pm ↑ Summary Valiant/HTML/Util/Form.pm »
1 package Valiant::I18N;
2  
3 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use Data::Localize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use Data::Localize::MultiLevel;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Valiant::Util 'throw_exception', 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use namespace::autoclean -also => ['throw_exception', 'debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 our $dl;
13 47 our %locale_paths;
14  
15 #TODO add -namespace='namespace to use to find the locale dir'
16 sub import {
17 169 ▶ my $class = shift;
Subroutine import: called CC=1 SCAR=0.0
18 169 my $target = caller;
19 169 $class->init;
20 169 $class->add_locale_path(_locale_path_from_module($target)); #TODO Should we also look it parent directories?
21  
22 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 169 ▶ *{"${target}::_t"} = sub { $class->make_tag(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _t: called CC=1 SCAR=0.0
24 }
25  
26 sub dl {
27 7055 ▶ my $self_or_class = shift;
Subroutine dl: called CC=3 SCAR=11.3
Pod undocumented
28 7055 ▶ my $class = ref($self_or_class) ? ref($self_or_class) : $self_or_class;
Branch: 1 branch
BranchTrueFalseTotal
ref $self_or_class ? : 0 7055 7055
29 7055 ▶ $dl ||= $class->init;
Condition66%2/3
$Valiant::I18N::dl ||= $class->init
l!l&&r!l&&!r
705320
MC/DC0%0/2
$Valiant::I18N::dl ||= $class->init
$Valiant::I18N::dl $class->init
Truth table66%2/3
A ||= B
A= $Valiant::I18N::dlB= $class->init
ABresult
1X1
011
000
30 7055 return $dl;
31 }
32  
33 sub init {
34 171 ▶ my $class = shift;
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
35 171 ▶ return if $dl;
Branch: 1 branch
BranchTrueFalseTotal
if $Valiant::I18N::dl 125 46 171
36 46 $dl = Data::Localize->new;
37 46 my @detected_languages = $dl->languages;
38 46 ▶ $dl->set_languages('en', @detected_languages) unless grep { /^en$/ } @detected_languages;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^en$/;} @detected_languages 0 46 46
39 46 $class->add_locale_path(_locale_path_from_module($class)); #TODO do we need to load the $class @ISA as well?
40 46 return $dl;
41 }
42  
43 sub add_locale_path {
44 215 ▶ my ($class, $path) = @_;
Subroutine add_locale_path: called CC=5 SCAR=16.1
Pod undocumented
45 215 ▶ return if $locale_paths{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $Valiant::I18N::locale_paths{$path} 97 118 215
46 118 debug 2, "Found new possible locales path at $path";
47 118 my @found = glob($path);
48 118 ▶ my $flag = @found ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 101 17 118
49 118 ▶ if($flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 1) { } 101 17 118
50 101 debug 1, "Translation files found at $path";
51 101 $class->dl->add_localizer(Data::Localize::MultiLevel->new(paths => [$path]));
52 } else {
53 17 debug 2, "No translation files found at $path";
54 }
55 118 $locale_paths{$path} = $flag;
56 118 ▶ return $flag == 1 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$flag == 1 ? : 101 17 118
57 }
58  
59 sub _module_path {
60 215 ▶ my $class_name = shift;
Subroutine _module_path: called CC=2 SCAR=6.9
61 215 my @parts = split '::', $class_name;
62 215 my $path = join '/', @parts;
63 215 debug 3, "trying to find module path for class '$class_name' via 'INC{${path}.pm}'";
64 215 my $inc = $INC{"${path}.pm"};
65 215 ▶ unless($inc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inc) 4 211 215
66 4 debug 3, "Can't find ${path}.pm from parts: @{[ join ', ', @parts ]} in INC keys: @{[ join ', ', keys %INC ]}";
67 }
68 215 return $inc;
69 }
70  
71 sub _locale_path_from_module {
72 215 ▶ my $class = shift;
Subroutine _locale_path_from_module: called CC=3 SCAR=11.0
73 215 my $module_path = _module_path($class);
74 215 ▶ debug 2, "Found a path '@{[ $module_path||'NotFound' ]}' for class '$class'";
Condition100%2/2
$module_path || 'NotFound'
l!l
2114
MC/DC100%1/1
$module_path || 'NotFound'
$module_path
Truth table100%2/2
A
A= $module_path
Aresult
11
00
75 215 my ($vol, $dir, $file) = File::Spec->splitpath($module_path);
76 #my $locale_path = File::Spec->catfile($dir, 'locale','*.*');
77 215 my $locale_path = File::Spec->catpath($vol, $dir, File::Spec->catfile('locale', '*.*'));
78 215 ▶ debug 2, "module path '@{[ $module_path||'NotFound' ]}' resolves to path '$locale_path' with vol:$vol, dir:$dir and file:$file";
Condition100%2/2
$module_path || 'NotFound'
l!l
2114
MC/DC100%1/1
$module_path || 'NotFound'
$module_path
Truth table100%2/2
A
A= $module_path
Aresult
11
00
79 215 return $locale_path;
80 }
81  
82 sub _lookup_translation_by_count {
83 121 ▶ my ($self, $count, $original, $translated, %args) = @_;
Subroutine _lookup_translation_by_count: called CC=9 SCAR=23.3
84 121 ▶ $translated = $translated->{zero} if $count == 0 and $translated->{zero};
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 and $translated->{"zero"} 0 121 121
Condition33%1/3
$count == 0 and $translated->{"zero"}
!ll&&!rl&&r
12100
MC/DC0%0/2
$count == 0 and $translated->{"zero"}
$count == 0 $translated->{"zero"}
Truth table33%1/3
A and B
A= $count == 0B= $translated->{"zero"}
ABresult
0X0
100
111
85 121 ▶ $translated = $translated->{one} if $count == 1 and $translated->{one};
Branch: 1 branch
BranchTrueFalseTotal
if $count == 1 and $translated->{"one"} 1 120 121
Condition66%2/3
$count == 1 and $translated->{"one"}
!ll&&!rl&&r
12001
MC/DC50%1/2
$count == 1 and $translated->{"one"}
$count == 1 $translated->{"one"}
Truth table66%2/3
A and B
A= $count == 1B= $translated->{"one"}
ABresult
0X0
100
111
86 121 ▶ $translated = $translated->{other} if $count > 1 and $translated->{other};
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 and $translated->{"other"} 120 1 121
Condition66%2/3
$count > 1 and $translated->{"other"}
!ll&&!rl&&r
10120
MC/DC50%1/2
$count > 1 and $translated->{"other"}
$count > 1 $translated->{"other"}
Truth table66%2/3
A and B
A= $count > 1B= $translated->{"other"}
ABresult
0X0
100
111
87  
88 121 ▶ throw_exception('MissingCountKey', tag=>$original, count=>$count) if ref $translated;
Branch: 1 branch
BranchTrueFalseTotal
if ref $translated 0 121 121
89  
90 # Ok, need to do any variable subsitutions again. Just stole this from
91 # Data::Localize::Format::NamedArgs
92  
93 # TODO this has an error when $args{$1} is 0
94 #$translated =~ s/\{\{([^}]+)\}\}/ $args{$1} || '' /gex;
95 121 ▶ $translated =~ s/\{\{([^}]+)\}\}/ defined($args{$1}) ? $args{$1}: '' /gex;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$1} ? : 121 0 121
96  
97 121 debug 1, "Resolved count translation; $translated";
98 121 return $translated;
99 }
100  
101 sub translate {
102 1820 ▶ my ($self, $key, %args) = @_;
Subroutine translate: called CC=19 SCAR=30.4
Pod documented
103  
104 1820 ▶ my @defaults = @{ delete($args{default})||[] };
Branch: 1 branch
BranchTrueFalseTotal
delete $args{"default"} or [] 0 1820 1820
105 1820 ▶ my $scope = delete($args{scope})||'';
Condition100%2/2
delete $args{'scope'} || ''
l!l
3491471
MC/DC100%1/1
delete $args{'scope'} || ''
delete $args{'scope'}
Truth table100%2/2
A
A= delete $args{'scope'}
Aresult
11
00
106 1820 my $count = $args{count};
107  
108 # TODO work around 0 count bug in Data::Localize until I can get a fix in
109 1820 ▶ $args{count} = 'zero' if defined($count) && $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count and $count == 0 10 1810 1820
Condition100%3/3
defined $count and $count == 0
!ll&&!rl&&r
392141810
MC/DC100%2/2
defined $count and $count == 0
defined $count $count == 0
Truth table100%3/3
A and B
A= defined $countB= $count == 0
ABresult
0X0
100
111
110  
111 1820 ▶ $scope = join('.',@{$scope}) if (ref($scope)||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scope || "") eq "ARRAY" 348 1472 1820
Condition100%2/2
ref $scope || ''
l!l
3481472
MC/DC100%1/1
ref $scope || ''
ref $scope
Truth table100%2/2
A
A= ref $scope
Aresult
11
00
112  
113 # TODO deal with $count
114  
115 # $key can be either a string or a tag.
116 1820 ▶ $key = $$key if $self->is_i18n_tag($key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_i18n_tag($key) 1820 0 1820
117 1820 ▶ $key = "${scope}.${key}" if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 349 1471 1820
118  
119 1820 debug 1 , "Trying to translate '$key' with defaults: [@{[ join ',', @defaults]}]";
120
121 1820 debug 1, "Trying to translate: '$key'";
122 1820 my $translated = $self->dl->localize($key, \%args);
123  
124 # If $translated is a hashref that means we need to apply the $count
125 1820 ▶ $translated = $self->_lookup_translation_by_count($count, $key, $translated, %args)
Branch: 1 branch
BranchTrueFalseTotal
if ref $translated and defined $count 0 1820 1820
Condition33%1/3
ref $translated and defined $count
!ll&&!rl&&r
182000
MC/DC0%0/2
ref $translated and defined $count
ref $translated defined $count
Truth table33%1/3
A and B
A= ref $translatedB= defined $count
ABresult
0X0
100
111
126 if ref($translated) && defined($count);
127  
128 # Is this a bug in Data::Localize? Seems like ->localize just returns
129 # the $key if it fails to actually localize. I would think it should
130 # return undef;
131  
132 1820 debug 1, "Proposed translation: '$translated'";
133 1820 ▶ unless($translated eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($translated eq $key) 1 1819 1820
134 1 debug 1, "Translated '$key' to '$translated'";
135 1 return $translated;
136 }
137  
138 1819 ▶ return $translated unless $translated eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $translated eq $key 0 1819 1819
139  
140 # Ok if we got here that means the $key failed to localize. So we will
141 # iterate over $args{defaults}. If a defaut is a tag we try to localize
142 # it. First tag to localize is returned. If however we encounter a
143 # default that is not a tag we just return that without trying to localize
144 # it. So you should stick your ultimate fallback string at the very end
145 # of the defaults list.
146 #
147 1819 debug 1, "Trying to translate defaults: [@{[ join ',', @defaults]}]";
148  
149 1819 foreach my $default(@defaults) {
150 6239 debug 2, "Trying to translate default: '$default'";
151  
152 6239 ▶ unless ($self->is_i18n_tag($default)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_i18n_tag($default)) 1105 5134 6239
153 1105 debug 1, "'$default' is just a string, so using that.";
154 1105 return $default;
155 }
156  
157 5134 my $tag = $$default;
158 5134 my $translated = $self->dl->localize($tag, \%args);
159 5134 ▶ $translated = $self->_lookup_translation_by_count($count, $tag, $translated, %args)
Branch: 1 branch
BranchTrueFalseTotal
if ref $translated and defined $count 121 5013 5134
Condition66%2/3
ref $translated and defined $count
!ll&&!rl&&r
50130121
MC/DC50%1/2
ref $translated and defined $count
ref $translated defined $count
Truth table66%2/3
A and B
A= ref $translatedB= defined $count
ABresult
0X0
100
111
160 if ref($translated) and defined($count);
161  
162 5134 debug 2, "Proposed translation: '$translated'";
163 5134 ▶ unless($translated eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($translated eq $tag) 714 4420 5134
164 714 debug 1, "Translated '$default' to '$translated'";
165 714 return $translated;
166 }
167 }
168  
169 # If we get this far that means the key and defaults can't be translated. So return
170 # message if it exists as a raw untranslated string.
171  
172 #return $message if $message;
173
174 0 ▶ my $list = join (', ', $key, map { $$_ if $self->is_i18n_tag($_) } @defaults);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_i18n_tag($_) 0 0 0
175 0 my $path = join ',', $self->valid_paths;
176  
177 0 throw_exception General => (msg=>"Can't find a translation for key in ($list) at paths ($path)");
178 }
179  
180 sub valid_paths {
181 0 ▶ return grep { $locale_paths{$_} > 0 } keys %locale_paths;
Subroutine valid_paths: not called CC=1 SCAR=6.9
Pod undocumented
182 }
183  
184 sub detect_languages_from_header {
185 0 ▶ my ($class, $header) = @_;
Subroutine detect_languages_from_header: not called CC=1 SCAR=6.9
Pod undocumented
186 0 return $class->dl->detect_languages_from_header($header);
187 }
188  
189 sub set_languages {
190 0 ▶ my ($class, @languages) = @_;
Subroutine set_languages: not called CC=1 SCAR=6.9
Pod undocumented
191 0 $class->dl->set_languages(@languages);
192 }
193  
194 sub is_i18n_tag {
195 8956 ▶ my ($class, $tag) = @_;
Subroutine is_i18n_tag: called CC=3 SCAR=11.0
Pod undocumented
196 8956 ▶ return (ref($tag)||'') eq 'Valiant::I18N::Tag' ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
(ref $tag || '') eq 'Valiant::I18N::Tag' ? : 7306 1650 8956
Condition100%2/2
ref $tag || ''
l!l
73161640
MC/DC100%1/1
ref $tag || ''
ref $tag
Truth table100%2/2
A
A= ref $tag
Aresult
11
00
197 }
198  
199 sub make_tag($) {
200 7599 ▶ my ($class, $tag) = @_;
Subroutine make_tag: called CC=1 SCAR=0.0
Pod undocumented
201 7599 return bless \$tag, 'Valiant::I18N::Tag';
202 }
203  
204 package Valiant::I18N::Tag;
205  
206 use overload (
207 47 ne => \&not_equals,
208 eq => \&equals,
209 bool => \&is_true,
210 '""' => \&stringify,
211 47 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 sub stringify {
214 20413 ▶ my ($self) = @_;
Subroutine stringify: called CC=1 SCAR=0.0
215 20413 return $$self;
216 }
217  
218 31 ▶sub is_true { return ${$_[0]} }
Subroutine is_true: called CC=1 SCAR=0.0
219  
220 sub equals {
221 10 ▶ my ($self, $target) = @_;
Subroutine equals: called CC=1 SCAR=0.0
222 10 return $$self eq "$target";
223 }
224  
225 sub not_equals {
226 1 ▶ my ($self, $target) = @_;
Subroutine not_equals: called CC=1 SCAR=0.0
227 1 return $$self ne "$target";
228 }
229  
230 47 1;
231  
232 =head1 NAME
233  
234 Valiant::I18N - Translations
235  
236 =head1 SYNOPSIS
237  
238 package Example::Person;
239  
240 use Valiant::I18N;
241  
242 my $tag = _t 'translation_tag';
243  
244 my $translated_string = Valiant::I18N->translate($tag, %args);
245  
246 =head1 DESCRIPTION
247  
248 You should see L<Valiant> for some details on how this is used with the core
249 validations code.
250  
251 =head1 CLASS METHODS
252  
253 This package defines the following class methods
254  
255 =head2 translate ($key, %args)
256  
257 Given a translation tag will attempt to return a translated string. Also accepts arguments as
258 a hash of values. The follow hash keys has special meaning (all others will be passed as placeholder
259 values for the translation string).
260  
261 =over 4
262  
263 =item default
264  
265 An array of translation tags to check if the passed key doesn't resolve. First one
266 to resolve gets returned.
267  
268 =item scope
269  
270 key namespace that will be prepended to the translation tag for lookup.
271  
272 =item count
273  
274 an integer to specify pluralization subkeys.
275  
276 =back
277  
278 =head1 SEE ALSO
279
280 L<Valiant>
281  
282 =head1 AUTHOR
283
284 See L<Valiant>
285  
286 =head1 COPYRIGHT & LICENSE
287
288 See L<Valiant>
289  
290 =cut
« Valiant/HTML/Util/FormTags.pm ↑ Summary Valiant/HTML/Util/Form.pm »