Valiant/Translation.pm

filter: Statementstmt 91.3% 84 / 92 Branchbran 64.2% 18 / 28 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 84.4% 119 / 141 CC 17 SCAR 20.0
CC
17
Cov
84%
human_label_name
12.2
human_attribute_name
9.0
BEGIN
1.0
CRAP 7.4
« Valiant/Filterable.pm ↑ Summary Valiant/Validator/With.pm »
1 package Valiant::Translation;
2  
3 41 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 41 with 'Valiant::Naming';
8  
9 123 ▶sub i18n_class { 'Valiant::I18N' }
Subroutine i18n_class: called CC=1 SCAR=0.0
Pod documented
10  
11 has 'i18n' => (
12 is => 'ro',
13 required => 1,
14 lazy => 1,
15 123 ▶ default => sub { Module::Runtime::use_module(shift->i18n_class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 41 );
17  
18 1008 ▶sub i18n_scope { 'valiant' }
Subroutine i18n_scope: called CC=1 SCAR=0.0
Pod documented
19  
20 432 ▶sub human_attribute_name {
Subroutine human_attribute_name: called CC=9 SCAR=22.0
Pod documented
21 432 my ($self, $attribute, $options) = @_;
22 432 ▶ return undef unless defined($attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 432 432
23 432 debug 1, "Begin building human name for attribute '$attribute'";
24  
25 # TODO I think we need to clean $option here so I don't need to manually
26 # set count=>1 as I do below.
27 #
28  
29 432 my @defaults = ();
30 432 my $i18n_scope = $self->i18n_scope;
31 432 my @parts = split /\./, $attribute;
32 432 my $attribute_name = pop @parts;
33 432 ▶ my $namespace = join '/', @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 20 412 432
34 432 my $attributes_scope = "${i18n_scope}.attributes";
35  
36 432 ▶ if($self->can('i18n_lookup')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("i18n_lookup")) 431 1 432
37 431 debug 2, "Building defaults for attributes '$attribute'";
38 431 ▶ if($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 20 411 431
39 @defaults = map {
40 20 my $class = $_;
41 20 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.${attribute_name}"
42 20 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
43 } else {
44 @defaults = map {
45 443 my $class = $_;
46 443 "${attributes_scope}.${\$class->model_name->i18n_key}.${attribute}"
47 411 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
48 }
49 }
50  
51 432 @defaults = map { $self->i18n->make_tag($_) } (@defaults, "attributes.${attribute}");
52  
53 # Not sure if this should move up above the preceeding map...
54  
55 432 ▶ if(exists $options->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default"}) 230 202 432
56 230 my $default = delete $options->{default};
57 230 ▶ my @default = ref($default) ? @$default : ($default);
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 0 230 230
58 230 push @defaults, @default;
59 }
60 # The final default is just our best attempt to make a name out of the actual
61 # attribute name. This is passed as a plain string so we don't actually try
62 # to localize it.
63 432 push @defaults, $self->_humanize_attribute($attribute);
64  
65 432 my $key = shift @defaults;
66 432 $options->{default} = \@defaults;
67 432 ▶ $options->{count} = 1 unless exists $options->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"count"} 356 76 432
68  
69 432 ▶ return my $localized = $self->i18n->translate($key, %{$options||+{}});
Condition50%1/2
$options || {}
l!l
4320
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
70 }
71  
72 33 ▶sub human_label_name {
Subroutine human_label_name: called CC=9 SCAR=25.0
Pod documented
73 33 my ($self, $attribute, $options) = @_;
74 33 ▶ return undef unless defined($attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 33 33
75 33 debug 1, "Begin building human name for label '$attribute'";
76  
77 # TODO I think we need to clean $option here so I don't need to manually
78 # set count=>1 as I do below.
79 #
80  
81 33 my @defaults = ();
82 33 my $i18n_scope = $self->i18n_scope;
83 33 my @parts = split /\./, $attribute;
84 33 my $attribute_name = pop @parts;
85 33 ▶ my $namespace = join '/', @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 33 33
86 33 my $attributes_scope = "${i18n_scope}.labels";
87  
88 33 ▶ if($self->can('i18n_lookup')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("i18n_lookup")) 33 0 33
89 33 debug 2, "Building defaults for labels '$attribute'";
90 33 ▶ if($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 0 33 33
91 @defaults = map {
92 0 my $class = $_;
93 0 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.${attribute_name}"
94 0 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
95 } else {
96 @defaults = map {
97 33 my $class = $_;
98 33 "${attributes_scope}.${\$class->model_name->i18n_key}.${attribute}"
99 33 } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup;
100 }
101 }
102  
103 33 @defaults = map { $self->i18n->make_tag($_) } (@defaults, "labels.${attribute}");
104  
105 # Not sure if this should move up above the preceeding map...
106  
107 33 ▶ if(exists $options->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default"}) 0 33 33
108 0 my $default = delete $options->{default};
109 0 ▶ my @default = ref($default) ? @$default : ($default);
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 0 0 0
110 0 push @defaults, @default;
111 }
112
113 # The final default is just our best attempt to make a name out of the actual
114 # attribute name. This is passed as a plain string so we don't actually try
115 # to localize it.
116 33 push @defaults, $self->_humanize_label($attribute);
117  
118 33 my $key = shift @defaults;
119 33 $options->{default} = \@defaults;
120 33 ▶ $options->{count} = 1 unless exists $options->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"count"} 33 0 33
121  
122 33 ▶ return my $localized = $self->i18n->translate($key, %{$options||+{}});
Condition50%1/2
$options || {}
l!l
330
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
123 }
124  
125 33 ▶sub _humanize_label { shift->_humanize(@_) }
Subroutine _humanize_label: called CC=1 SCAR=0.0
126 432 ▶sub _humanize_attribute { shift->_humanize(@_) }
Subroutine _humanize_attribute: called CC=1 SCAR=0.0
127  
128 441 ▶sub _humanize {
Subroutine _humanize: called CC=1 SCAR=0.0
129 441 my ($self, $text) = @_;
130 441 my $humanized = $text;
131  
132 441 $humanized =~s/_id$//; # remove trailing _id
133 441 $humanized =~s/_/ /g;
134 441 $humanized = autoformat($humanized, +{case=>'title'});
135 441 $humanized =~s/[\n]//g; # Is this a bug in Text::Autoformat???
136  
137 441 return $humanized;
138 }
139  
140 41 1;
141  
142 =head1 NAME
143  
144 Valiant::Translation - Localized, human readable names for models and attributes
145  
146 =head1 DESCRIPTION
147  
148 A role providing the translation glue between your model and L<Valiant::I18N>:
149 it turns attribute names into localized, human readable strings for error
150 messages and form labels. You won't usually consume this role directly; it is
151 composed into your class via L<Valiant::Validates>. Documented here is the
152 public API it adds to your class.
153  
154 =head1 ATTRIBUTES
155  
156 =head2 i18n
157  
158 An instance of the class named by L</i18n_class>, lazily built. You can pass
159 your own at construction if you need custom translation behavior.
160  
161 =head1 METHODS
162  
163 =head2 i18n_class
164  
165 The class used for translation. Defaults to C<Valiant::I18N>. Override this
166 method in your class to use a different translation backend.
167  
168 =head2 i18n_scope
169  
170 The top level namespace under which translation keys are looked up. Defaults
171 to C<valiant>. Override to relocate your translations.
172  
173 =head2 human_attribute_name ($attribute, \%options)
174  
175 Returns a human readable, localized version of an attribute name, used when
176 building full error messages. Looks for translations under
177 C<< {i18n_scope}.attributes.{model}.{attribute} >> across the class hierarchy,
178 falling back to any C<default> tags passed in C<\%options> and finally to a
179 title cased version of the attribute name itself (with any trailing C<_id>
180 removed and underscores turned into spaces).
181  
182 =head2 human_label_name ($attribute, \%options)
183  
184 The same lookup as L</human_attribute_name> but under the C<labels> scope
185 rather than C<attributes>; used by the form generation code to build field
186 labels so labels can be localized separately from error messages.
187  
188 =head1 SEE ALSO
189  
190 L<Valiant>, L<Valiant::I18N>, L<Valiant::Validates>.
191  
192 =head1 AUTHOR
193  
194 See L<Valiant>
195  
196 =head1 COPYRIGHT & LICENSE
197  
198 See L<Valiant>
199  
200 =cut
« Valiant/Filterable.pm ↑ Summary Valiant/Validator/With.pm »