CatalystX/I18N/Role/GetLocale.pm

filter: Statementstmt 94.1% 80 / 85 Branchbran 70.8% 34 / 48 Conditioncond 62.9% 17 / 27 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 79.7% 154 / 193 CC 43 SCAR 30.3
CC
43
Cov
82%
get_locale_from_browser
34.1
get_locale
6.3
check_locale
6.0
CRAP 20.7
« CatalystX/I18N/Maketext.pm ↑ Summary CatalystX/I18N/Role/PosixLocale.pm »
1 # ============================================================================
2 package CatalystX::I18N::Role::GetLocale;
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  
8 4 ▶use CatalystX::I18N::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::Util qw(first shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 25 ▶sub check_locale {
Subroutine check_locale: called CC=6 SCAR=17.9
Pod documented
12 25 my ($c,$locale) = @_;
13  
14 return
15 25 ▶ unless defined $locale
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale and $locale =~ /^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/ 1 24 25
Condition66%2/3
defined $locale and $locale =~ /^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/
!ll&&!rl&&r
1024
MC/DC50%1/2
defined $locale and $locale =~ /^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/
defined $locale $locale =~ /^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/
Truth table66%2/3
A and B
A= defined $localeB= $locale =~ /^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/
ABresult
0X0
100
111
16 && $locale =~ m/^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/;
17  
18 24 $locale = lc($1);
19 24 ▶ $locale .= '_'.uc($2)
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 8 16 24
20 if defined $2;
21  
22 return
23 if ! exists $c->config->{I18N}{locales}{$locale}
24 24 ▶ || $c->config->{I18N}{locales}{$locale}{inactive} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $c->config->{"I18N"}{"locales"}{$locale} or $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1 15 9 24
Condition100%3/3
not exists $c->config->{"I18N"}{"locales"}{$locale} or $c->config->{"I18N"}{"locales"}{$locale}{"inactive"} == 1
l!l&&r!l&&!r
1419
MC/DC100%2/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 table100%3/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
25  
26 9 return $locale;
27 }
28  
29 4 ▶sub get_locale_from_session {
Subroutine get_locale_from_session: called CC=2 SCAR=7.6
Pod documented
30 4 my ($c) = @_;
31  
32 4 ▶ if ($c->can('session')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("session")) 4 0 4
33 4 return $c->check_locale($c->session->{i18n_locale});
34 }
35  
36 0 return;
37 }
38  
39 2 ▶sub get_locale_from_user {
Subroutine get_locale_from_user: called CC=4 SCAR=17.9
Pod documented
40 2 my ($c) = @_;
41  
42 2 ▶ if ($c->can('user')
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("user") and defined $c->user and $c->user->can("locale")) 0 2 2
Condition33%2/6
$c->can("user") and defined $c->user and $c->user->can("locale")
!ll&&!rl&&r
200
$c->can("user") and defined $c->user
!ll&&!rl&&r
200
MC/DC0%0/3
$c->can("user") and defined $c->user and $c->user->can("locale")
$c->can("user") defined $c->user $c->user->can("locale")
Truth table25%1/4
A and B and C
A= $c->can("user")B= defined $c->userC= $c->user->can("locale")
ABCresult
0XX0
10X0
1100
1111
43 && defined $c->user
44 && $c->user->can('locale')) {
45 0 return $c->check_locale($c->user->locale);
46 }
47  
48 2 return;
49 }
50  
51 11 ▶sub get_locale_from_browser {
Subroutine get_locale_from_browser: called CC=29 SCAR=35.3
Pod documented
52 11 my ($c) = @_;
53  
54 11 my ($languages,$territories) = ([],[]);
55  
56 # Get Accept-Language
57 11 ▶ if ($c->request->can('accept_language')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->can("accept_language")) 11 0 11
58 11 my $locales = $c->request->accept_language;
59 # Check if Accept-Language matches a locale
60 11 foreach my $locale (@$locales) {
61 21 my $checked_locale = $c->check_locale($locale);
62 21 ▶ return $checked_locale
Branch: 1 branch
BranchTrueFalseTotal
if $checked_locale 6 15 21
63 if $checked_locale;
64 15 ▶ if ($locale =~ $CatalystX::I18N::TypeConstraints::LOCALE_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale =~ /$CatalystX::I18N::TypeConstraints::LOCALE_RE/) 15 0 15
65 15 push(@$languages,$1);
66 15 ▶ push(@$territories,$2)
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 14 15
67 if $2;
68 }
69 }
70 }
71  
72 # Get browser language
73 5 ▶ if ( $c->request->user_agent && $c->request->can('browser_language')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->user_agent and $c->request->can("browser_language")) 2 3 5
Condition66%2/3
$c->request->user_agent and $c->request->can("browser_language")
!ll&&!rl&&r
302
MC/DC50%1/2
$c->request->user_agent and $c->request->can("browser_language")
$c->request->user_agent $c->request->can("browser_language")
Truth table66%2/3
A and B
A= $c->request->user_agentB= $c->request->can("browser_language")
ABresult
0X0
100
111
74 2 my $language = $c->request->browser_language;
75 2 ▶ if ($language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language) 2 0 2
76 unshift(@$languages,$language)
77 2 ▶ unless grep { $language eq $_ } @$languages
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$language eq $_;} @$languages 2 0 2
78 }
79 }
80  
81 # Get client country
82 5 ▶ if ($c->request->can('client_country')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->can("client_country")) 5 0 5
83 5 my $territory = $c->request->client_country;
84 5 ▶ unshift(@$territories,uc($territory))
Branch: 1 branch
BranchTrueFalseTotal
if $territory 2 3 5
85 if ($territory);
86 }
87  
88 # Get browser territory
89 5 ▶ if ($c->request->can('browser_territory')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->can("browser_territory")) 5 0 5
90 5 my $territory = $c->request->browser_territory;
91 5 ▶ unshift(@$territories,uc($territory))
Branch: 1 branch
BranchTrueFalseTotal
if $territory 0 5 5
92 if ($territory);
93 }
94  
95 5 my $locale_config = $c->config->{I18N}{locales};
96  
97 # TODO: Make behaviour/preferences customizeable
98  
99 # Try to find best matching combination
100 5 foreach my $territory (@$territories) {
101 2 foreach my $language (@$languages) {
102 3 my $key = $language.'_'.$territory;
103 3 ▶ if (defined $locale_config->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $locale_config->{$key}) 0 3 3
104 0 return $key;
105 }
106 }
107 }
108  
109 # Try to find best matching country
110 5 foreach my $locale (keys %$locale_config) {
111 next
112 22 ▶ if $locale_config->{$locale}{inactive};
Branch: 1 branch
BranchTrueFalseTotal
if $locale_config->{$locale}{"inactive"} 3 19 22
113 19 foreach my $territory (@$territories) {
114 4 ▶ if ($locale =~ m/^[a-z]{2}_${territory}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale =~ /^[a-z]{2}_$territory$/) 2 2 4
115 2 return $locale;
116 }
117 }
118 }
119  
120 # Try to find best matching language
121 3 foreach my $locale (keys %$locale_config) {
122 next
123 8 ▶ if $locale_config->{$locale}{inactive};
Branch: 1 branch
BranchTrueFalseTotal
if $locale_config->{$locale}{"inactive"} 1 7 8
124 7 foreach my $language (@$languages) {
125 2 ▶ if ($locale =~ m/^${language}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale =~ /^$language$/) 2 0 2
126 2 return $locale;
127 }
128 }
129 }
130  
131 # Try to find best matching language
132 1 foreach my $locale (keys %$locale_config) {
133 next
134 6 ▶ if $locale_config->{$locale}{inactive};
Branch: 1 branch
BranchTrueFalseTotal
if $locale_config->{$locale}{"inactive"} 1 5 6
135 5 foreach my $language (@$languages) {
136 0 ▶ if ($locale =~ m/^${language}_[A-Z]{2}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale =~ /^${language}_[A-Z]{2}$/) 0 0 0
137 0 return $locale;
138 }
139 }
140 }
141  
142 1 return;
143 }
144  
145 3 ▶sub get_locale {
Subroutine get_locale: called CC=6 SCAR=18.3
Pod documented
146 3 my ($c) = @_;
147  
148 3 my ($locale,$languages,$territory);
149 3 my $locale_config = $c->config->{I18N}{locales};
150  
151 3 $locale = $c->get_locale_from_session();
152 3 ▶ $locale ||= $c->get_locale_from_user();
Condition66%2/3
$locale ||= $c->get_locale_from_user
l!l&&r!l&&!r
201
MC/DC50%1/2
$locale ||= $c->get_locale_from_user
$locale $c->get_locale_from_user
Truth table66%2/3
A ||= B
A= $localeB= $c->get_locale_from_user
ABresult
1X1
011
000
153 3 ▶ $locale ||= $c->get_locale_from_browser();
Condition66%2/3
$locale ||= $c->get_locale_from_browser
l!l&&r!l&&!r
201
MC/DC50%1/2
$locale ||= $c->get_locale_from_browser
$locale $c->get_locale_from_browser
Truth table66%2/3
A ||= B
A= $localeB= $c->get_locale_from_browser
ABresult
1X1
011
000
154  
155 # Default locale
156 3 ▶ $locale ||= $c->config->{I18N}{default_locale};
Condition66%2/3
$locale ||= $c->config->{"I18N"}{"default_locale"}
l!l&&r!l&&!r
201
MC/DC50%1/2
$locale ||= $c->config->{"I18N"}{"default_locale"}
$locale $c->config->{"I18N"}{"default_locale"}
Truth table66%2/3
A ||= B
A= $localeB= $c->config->{"I18N"}{"default_locale"}
ABresult
1X1
011
000
157  
158 # Random locale
159 3 ▶ $locale ||= first { $locale_config->{$_}{inactive} == 0 } shuffle keys %$locale_config;
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$locale ||= &first(sub { $locale_config->{$_}{"inactive"} == 0; } , &shuffle(keys %$locale_config))
l!l&&r!l&&!r
210
MC/DC0%0/2
$locale ||= &first(sub { $locale_config->{$_}{"inactive"} == 0; } , &shuffle(keys %$locale_config))
$locale &first(sub { $locale_config->{$_}{"inactive"} == 0; } , &shuffle(keys %$locale_config))
Truth table66%2/3
A ||= B
A= $localeB= &first(sub { $locale_config->{$_}{"inactive"} == 0; } , &shuffle(keys %$locale_config))
ABresult
1X1
011
000
160  
161 3 ▶ if ($c->can('locale')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("locale")) 3 0 3
162 3 $c->locale($locale);
163 }
164  
165 3 return $locale;
166 }
167  
168 4 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 4 1;
170  
171 =encoding utf8
172  
173 =head1 NAME
174  
175 CatalystX::I18N::Role::GetLocale - Tries to determine the current users locale
176  
177 =head1 SYNOPSIS
178  
179 package MyApp::Catalyst;
180
181 use CatalystX::RoleApplicator;
182 use Catalyst qw/MyPlugins
183 CatalystX::I18N::Role::Base
184 CatalystX::I18N::Role::GetLocale/;
185
186 __PACKAGE__->apply_request_class_roles(qw/CatalystX::I18N::TraitFor::Request/);
187 __PACKAGE__->setup();
188
189 package MyApp::Catalyst::Controller::Main;
190 use strict;
191 use warnings;
192 use parent qw/Catalyst::Controller/;
193
194 sub auto : Private { # Auto method will always be called first
195 my ($self,$c) = @_;
196 $c->get_locale();
197 }
198  
199 =head1 DESCRIPTION
200  
201 This role provides many methods to retrieve/guess the best locale for the
202 current user.
203  
204 =head1 METHODS
205  
206 =head3 get_locale
207  
208 Tries to determine the users locale in the given order
209  
210 =over
211  
212 =item 1. Session (via C<get_locale_from_session>)
213  
214 =item 2. User (via C<get_locale_from_user>)
215  
216 =item 3. Browser (via C<get_locale_from_browser>)
217  
218 =item 4. Default locale from config (via C<$c-E<gt>config-E<gt>{I18N}{default_locale}>)
219  
220 =item 5. Random locale
221  
222 =back
223  
224 Sets the winning locale (via C<$c-E<gt>locale()>) if the
225 L<CatalystX::I18N::Role::Base> is loaded.
226  
227 =head3 get_locale_from_browser
228  
229 Tries to fetch the locale from the browser (via
230 L<$c-E<gt>request-E<gt>accept_language> and
231 L<$c-E<gt>request-E<gt>browser_language>). L<CatalystX::I18N::TraitFor::Request>
232 must be loaded.
233  
234 =head3 get_locale_from_session
235  
236 Tries to fetch the locale from the current session.
237  
238 =head3 get_locale_from_user
239  
240 Tries to fetch the locale from the user object (via
241 L<$c-E<gt>user-E<gt>locale>).
242  
243 =head3 check_locale
244  
245 Helper method to check for a valid locale
246  
247 =head1 AUTHOR
248  
249 Maroš Kollár
250 CPAN ID: MAROS
251 maros [at] k-1.com
252
253 L<http://www.k-1.com>
« CatalystX/I18N/Maketext.pm ↑ Summary CatalystX/I18N/Role/PosixLocale.pm »