CatalystX/I18N/Role/Base.pm

filter: Statementstmt 88.4% 84 / 95 Branchbran 67.5% 27 / 40 Conditioncond 60.8% 14 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 76.8% 146 / 190 CC 32 SCAR 24.9
CC
32
Cov
79%
__ANON__
21.9
set_locale
10.3
i18n_geocode
6.0
CRAP 12.1
« CatalystX/I18N/DataLocalize.pm ↑ Summary CatalystX/I18N/TraitFor/Request.pm »
1 # ============================================================================
2 package CatalystX::I18N::Role::Base;
3 # ============================================================================
4  
5 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 requires qw(config response log);
8  
9 4 ▶use CatalystX::I18N::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Clone qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 has 'locale' => (
14 is => 'rw',
15 isa => 'CatalystX::I18N::Type::Locale',
16 lazy_build => 1,
17 builder => '_build_default_locale',
18 predicate => 'has_locale',
19 20 ▶ trigger => sub { shift->set_locale(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 4 );
21  
22 12 ▶sub _build_default_locale {
Subroutine _build_default_locale: called CC=1 SCAR=0.0
23 12 my ($c) = @_;
24  
25 12 my $locale = $c->config->{I18N}{default_locale};
26 12 $c->set_locale($locale);
27 12 return $locale;
28 }
29  
30 36 ▶sub i18n_config {
Subroutine i18n_config: called CC=2 SCAR=7.0
Pod documented
31 36 my ($c) = @_;
32  
33 return {}
34 36 ▶ unless defined $c->config->{I18N}{locales}{$c->locale};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->config->{"I18N"}{"locales"}{$c->locale} 0 36 36
35  
36 36 my $config = Clone::clone($c->config->{I18N}{locales}{$c->locale});
37 36 $config->{locale} = $c->locale;
38  
39 36 return $config;
40 }
41  
42 0 ▶sub i18n_geocode {
Subroutine i18n_geocode: not called CC=2 SCAR=17.9
Pod documented
43 0 my ($c) = @_;
44  
45 0 my $territory = $c->territory;
46  
47 return
48 0 ▶ unless $territory;
Branch: 1 branch
BranchTrueFalseTotal
unless $territory 0 0 0
49  
50 0 Class::Load::load_class('Locale::Geocode');
51  
52 0 my $lc = Locale::Geocode->new();
53 0 return $lc->lookup($territory);
54 }
55  
56 4 ▶sub language {
Subroutine language: called CC=2 SCAR=7.1
Pod documented
57 4 my ($self) = @_;
58  
59 return
60 4 ▶ unless $self->locale =~ $CatalystX::I18N::TypeConstraints::LOCALE_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->locale =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/ 0 4 4
61  
62 4 return lc($1);
63 }
64  
65 2 ▶sub territory {
Subroutine territory: called CC=3 SCAR=11.4
Pod documented
66 2 my ($self) = @_;
67  
68 return
69 2 ▶ unless $self->locale =~ $CatalystX::I18N::TypeConstraints::LOCALE_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->locale =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/ 0 2 2
70  
71 return
72 2 ▶ unless $2;
Branch: 1 branch
BranchTrueFalseTotal
unless $2 0 2 2
73  
74 2 return lc($2);
75 }
76  
77 34 ▶sub set_locale {
Subroutine set_locale: called CC=10 SCAR=23.3
Pod documented
78 34 my ($c,$value) = @_;
79  
80 return
81 34 ▶ unless $value =~ $CatalystX::I18N::TypeConstraints::LOCALE_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/ 0 34 34
82  
83 34 my $meta_attribute = $c->meta->get_attribute('locale');
84  
85 34 my $language = $1;
86 34 my $territory = $2;
87 34 my $locale = lc($language);
88 34 ▶ $locale .= '_'.uc($territory)
Branch: 1 branch
BranchTrueFalseTotal
if defined $territory and $territory ne "" 30 4 34
Condition66%2/3
defined $territory and $territory ne ""
!ll&&!rl&&r
4030
MC/DC50%1/2
defined $territory and $territory ne ""
defined $territory $territory ne ""
Truth table66%2/3
A and B
A= defined $territoryB= $territory ne ""
ABresult
0X0
100
111
89 if defined $territory && $territory ne '';
90  
91 # Check for valid locale
92 34 ▶ if (! exists $c->config->{I18N}{locales}{$locale}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $c->config->{"I18N"}{"locales"}{$locale} or $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1) 1 33 34
Condition66%2/3
not exists $c->config->{"I18N"}{"locales"}{$locale} or $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1
l!l&&r!l&&!r
1033
MC/DC50%1/2
not exists $c->config->{"I18N"}{"locales"}{$locale} or $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1
not exists $c->config->{"I18N"}{"locales"}{$locale} $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1
Truth table66%2/3
not(A) or B
A= not exists $c->config->{"I18N"}{"locales"}{$locale}B= $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1
ABresult
1X1
011
000
93 || $c->config->{I18N}{locales}{$locale}{inactive} == 1) {
94 1 $meta_attribute->clear_value($c);
95 1 return;
96 }
97  
98 # Set content language header
99 33 ▶ $c->response->content_language($language)
Branch: 1 branch
BranchTrueFalseTotal
if $c->response->can("content_language") 33 0 33
100 if $c->response->can('content_language');
101  
102 # Save locale in session
103 33 ▶ if ($c->can('session')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("session")) 33 0 33
104 33 $c->session->{i18n_locale} = $locale
105 }
106  
107 # Set locale
108 33 ▶ $meta_attribute->set_raw_value($c,$locale)
Branch: 1 branch
BranchTrueFalseTotal
if not $meta_attribute->has_value($c) or $meta_attribute->get_raw_value($c) ne $locale 13 20 33
Condition100%3/3
not $meta_attribute->has_value($c) or $meta_attribute->get_raw_value($c) ne $locale
l!l&&r!l&&!r
12120
MC/DC100%2/2
not $meta_attribute->has_value($c) or $meta_attribute->get_raw_value($c) ne $locale
not $meta_attribute->has_value($c) $meta_attribute->get_raw_value($c) ne $locale
Truth table100%3/3
not(A) or B
A= not $meta_attribute->has_value($c)B= $meta_attribute->get_raw_value($c) ne $locale
ABresult
1X1
011
000
109 if ! $meta_attribute->has_value($c)
110 || $meta_attribute->get_raw_value($c) ne $locale;
111  
112 33 return $locale;
113 }
114  
115  
116 3 ▶after setup_finalize => sub {
Subroutine __ANON__: called CC=18 SCAR=30.9
117 3 my ($app) = @_;
118  
119 3 ▶ $app->config->{I18N} ||= {};
Condition50%1/2
$app->config->{"I18N"} ||= {}
l!l
30
MC/DC0%0/1
$app->config->{"I18N"} ||= {}
$app->config->{"I18N"}
Truth table50%1/2
A
A= $app->config->{"I18N"}
Aresult
11
00
120 3 my $config = $app->config->{I18N};
121 3 ▶ my $locales = $config->{locales} ||= {};
Condition50%1/2
$config->{"locales"} ||= {}
l!l
30
MC/DC0%0/1
$config->{"locales"} ||= {}
$config->{"locales"}
Truth table50%1/2
A
A= $config->{"locales"}
Aresult
11
00
122  
123 3 my $locale_type_constraint = $app
124 ->meta
125 ->get_attribute('locale')
126 ->type_constraint;
127  
128 3 my $default_locale = $config->{default_locale};
129 3 ▶ if (defined $default_locale
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_locale and not $locale_type_constraint->check($default_locale)) 0 3 3
Condition33%1/3
defined $default_locale and not $locale_type_constraint->check($default_locale)
!ll&&!rl&&r
030
MC/DC0%0/2
defined $default_locale and not $locale_type_constraint->check($default_locale)
defined $default_locale not $locale_type_constraint->check($default_locale)
Truth table33%1/3
A and not(B)
A= defined $default_localeB= not $locale_type_constraint->check($default_locale)
ABresult
0X0
100
111
130 && ! $locale_type_constraint->check($default_locale)) {
131 0 Catalyst::Exception->throw(sprintf("Default locale '%s' does not match %s",$default_locale,$CatalystX::I18N::TypeConstraints::LOCALE_RE));
132 }
133  
134 # Default locale fallback
135 3 ▶ $default_locale ||= 'en';
Condition50%1/2
$default_locale ||= "en"
l!l
30
MC/DC0%0/1
$default_locale ||= "en"
$default_locale
Truth table50%1/2
A
A= $default_locale
Aresult
11
00
136  
137 # Enable default locale
138 3 ▶ $locales->{$default_locale} ||= {};
Condition50%1/2
$locales->{$default_locale} ||= {}
l!l
30
MC/DC0%0/1
$locales->{$default_locale} ||= {}
$locales->{$default_locale}
Truth table50%1/2
A
A= $locales->{$default_locale}
Aresult
11
00
139  
140 # Build inheritance tree
141 3 my (%tree,$changed);
142 3 $changed = 1;
143 3 ▶ while ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 9 3 12
144 9 $changed = 0;
145 9 foreach my $locale (keys %$locales) {
146 next
147 54 ▶ if exists $tree{$locale};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tree{$locale} 31 23 54
148 23 my $locale_config = $locales->{$locale};
149 23 ▶ my $locale_inactive = $locale_type_constraint->check($locale) ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$locale_type_constraint->check($locale) ? : 23 0 23
150 $locale_config->{inactive} = 0
151 23 ▶ unless defined $locale_config->{inactive};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale_config->{"inactive"} 15 8 23
152 23 ▶ if ($locale_config->{inactive} == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($locale_config->{"inactive"} == 0 and $locale_config->{"inactive"} != $locale_inactive) 0 23 23
Condition66%2/3
$locale_config->{"inactive"} == 0 and $locale_config->{"inactive"} != $locale_inactive
!ll&&!rl&&r
3200
MC/DC0%0/2
$locale_config->{"inactive"} == 0 and $locale_config->{"inactive"} != $locale_inactive
$locale_config->{"inactive"} == 0 $locale_config->{"inactive"} != $locale_inactive
Truth table66%2/3
A and B
A= $locale_config->{"inactive"} == 0B= $locale_config->{"inactive"} != $locale_inactive
ABresult
0X0
100
111
153 && $locale_config->{inactive} != $locale_inactive) {
154 0 $app->log->warn(sprintf("Locale '%s' has been set inactive because it does not match %s",$locale,$CatalystX::I18N::TypeConstraints::LOCALE_RE));
155 0 $locale_config->{inactive} = 1;
156 }
157  
158 23 ▶ unless (exists $locale_config->{inherits}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $locale_config->{'inherits'}) { } 17 6 23
if (do { exists $tree{$locale_config->{"inherits"}} }) 12 5 17
159 6 $locale_config->{_inherits} = [];
160 6 $tree{$locale} = $locale_config;
161 6 $changed = 1;
162 0 } elsif (exists $tree{$locale_config->{inherits}}) {
163 12 my $inactive = $locale_config->{inactive};
164 12 my @inheritance = (@{$tree{$locale_config->{inherits}}->{_inherits}},$locale_config->{inherits});
165 12 $tree{$locale} = $locales->{$locale} = $locale_config = Catalyst::Utils::merge_hashes($tree{$locale_config->{inherits}}, $locale_config);
166 12 $locale_config->{_inherits} = \@inheritance;
167 12 $locale_config->{inactive} = $inactive;
168 12 $changed = 1;
169 }
170 }
171 }
172 # Check inheritance tree for circular references
173 3 foreach my $locale (keys %$locales) {
174 18 my $locale_config = $locales->{$locale};
175 18 ▶ unless (exists $locale_config->{_inherits}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $locale_config->{"_inherits"}) 0 18 18
176 0 Catalyst::Exception->throw(sprintf("Circular I18N inheritance detected between '%s' and '%s'",$locale,$locale_config->{inherits}))
177 }
178 }
179 4 };
180  
181 4 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 4 1;
183  
184 =encoding utf8
185  
186 =head1 NAME
187  
188 CatalystX::I18N::Role::Base - Basic catalyst I18N support
189  
190 =head1 SYNOPSIS
191  
192 package MyApp::Catalyst;
193
194 use Catalyst qw/MyPlugins
195 CatalystX::I18N::Role::Base/;
196
197 package MyApp::Catalyst::Controller::Main;
198 use strict;
199 use warnings;
200 use parent qw/Catalyst::Controller/;
201
202 sub action : Local {
203 my ($self,$c) = @_;
204
205 $c->locale('de_AT');
206 }
207  
208 =head1 DESCRIPTION
209  
210 This role is required by all other roles and provides basic I18N support for
211 Catalyst.
212  
213 =head1 METHODS
214  
215 =head3 locale
216  
217 $c->locale('de_AT');
218 OR
219 my $locale = $c->locale();
220  
221 Get/set the current locale. Changing this value has some side-effects:
222  
223 =over
224  
225 =item * Stores the locale in the current session (if any)
226  
227 =item * Sets the 'Content-Language' response header (if L<CatalystX::I18N::TraitFor::Response> has been loaded)
228  
229 =back
230  
231 =head3 set_locale
232  
233 Same as C<$c-E<gt>locale($locale);>.
234  
235 =head3 language
236  
237 Returns the language part of the current locale
238  
239 =head3 territory
240  
241 Returns the territory part of the current locale
242  
243 =head3 i18n_config
244  
245 Returns the (cloned) I18N config hash for the current locale.
246  
247 =head3 i18n_geocode
248  
249 my $lgt = $c->i18n_geocode
250 say $lgt->name;
251  
252 Returns a L<Locale::Geocode::Territory> object for the current territory.
253  
254 =head1 SEE ALSO
255  
256 L<POSIX>, L<Locale::Geocode>
257  
258 =head1 AUTHOR
259  
260 Maroš Kollár
261 CPAN ID: MAROS
262 maros [at] k-1.com
263
264 L<http://www.k-1.com>
« CatalystX/I18N/DataLocalize.pm ↑ Summary CatalystX/I18N/TraitFor/Request.pm »