blib/lib/Catalyst/TraitFor/Controller/LocaleSelect.pm

filter: Statementstmt 22.6% 12 / 53 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 30.0% 3 / 10 Podpod 100% 1 / 1 totaltotal 13.5% 16 / 118 CC 17 SCAR 30.1
CC
17
Cov
13%
_build_language_config
42.0
__ANON__
30.0
_lang_for_request
12.0
CRAP 20.2
↑ Summary
1 package Catalyst::TraitFor::Controller::LocaleSelect;
2 2 ▶use MooseX::MethodAttributes::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Catalyst::TraitFor::Controller::LocaleSelect - Provides locale selection mechanism for controllers
8  
9 =head1 VERSION
10  
11 Version 0.03
12  
13 =cut
14  
15 2 our $VERSION = '0.03';
16  
17 0 ▶before 'auto' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
18 0 my ( $self, $c ) = @_;
19  
20 0 my $config = $self->_build_language_config($c);
21  
22 0 my $allowed = { map { $_ => 1 } @{ $config->{allowed} } };
23 $config->{selected} = $self->_lang_for_request($c, $allowed)
24 || $self->_lang_for_set($c, $allowed)
25 || $self->_lang_from_cookie($c, $allowed)
26 || $self->_lang_from_agent($c, $allowed)
27 0 ▶ || $config->{default};
Condition0%0/12
$self->_lang_for_request($c, $allowed) || $self->_lang_for_set($c, $allowed) || $self->_lang_from_cookie($c, $allowed) || $self->_lang_from_agent($c, $allowed) || $config->{'default'}
l!l&&r!l&&!r
000
$self->_lang_for_request($c, $allowed) or $self->_lang_for_set($c, $allowed) or $self->_lang_from_cookie($c, $allowed) or $self->_lang_from_agent($c, $allowed)
l!l&&r!l&&!r
000
$self->_lang_for_request($c, $allowed) or $self->_lang_for_set($c, $allowed) or $self->_lang_from_cookie($c, $allowed)
l!l&&r!l&&!r
000
$self->_lang_for_request($c, $allowed) or $self->_lang_for_set($c, $allowed)
l!l&&r!l&&!r
000
MC/DC0%0/5
$self->_lang_for_request($c, $allowed) || $self->_lang_for_set($c, $allowed) || $self->_lang_from_cookie($c, $allowed) || $self->_lang_from_agent($c, $allowed) || $config->{'default'}
$self->_lang_for_request($c, $allowed) $self->_lang_for_set($c, $allowed) $self->_lang_from_cookie($c, $allowed) $self->_lang_from_agent($c, $allowed) $config->{'default'}
Truth table0%0/6
A or B or C or D || E
A= $self->_lang_for_request($c, $allowed)B= $self->_lang_for_set($c, $allowed)C= $self->_lang_from_cookie($c, $allowed)D= $self->_lang_from_agent($c, $allowed)E= $config->{'default'}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
28  
29 0 $c->languages( [ $config->{selected} ] );
30 0 $c->stash( locale => $config );
31 2 };
32  
33 0 ▶sub _lang_for_request {
Subroutine _lang_for_request: not called CC=3 SCAR=24.8
34 0 my ( $self, $c, $allowed ) = @_;
35  
36 0 my $language = delete $c->req->params->{'locale'};
37 0 ▶ if ( $language && exists $allowed->{ $language } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language and exists $allowed->{$language}) 0 0 0
Condition0%0/3
$language and exists $allowed->{$language}
!ll&&!rl&&r
000
MC/DC0%0/2
$language and exists $allowed->{$language}
$language exists $allowed->{$language}
Truth table0%0/3
A and B
A= $languageB= exists $allowed->{$language}
ABresult
0X0
100
111
38 0 return $language;
39 }
40 0 return 0;
41 }
42  
43 0 ▶sub _lang_for_set {
Subroutine _lang_for_set: not called CC=3 SCAR=24.8
44 0 my ( $self, $c, $allowed ) = @_;
45  
46 0 my $language = delete $c->req->params->{'set_locale'};
47 0 ▶ if ( $language && exists $allowed->{ $language } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language and exists $allowed->{$language}) 0 0 0
Condition0%0/3
$language and exists $allowed->{$language}
!ll&&!rl&&r
000
MC/DC0%0/2
$language and exists $allowed->{$language}
$language exists $allowed->{$language}
Truth table0%0/3
A and B
A= $languageB= exists $allowed->{$language}
ABresult
0X0
100
111
48 0 $c->res->cookies->{'locale'} = {
49 value => $language,
50 expires => '+5y'
51 };
52 0 return $language;
53 }
54 0 return 0;
55 }
56  
57 0 ▶sub _lang_from_cookie {
Subroutine _lang_from_cookie: not called CC=3 SCAR=24.8
58 0 my ( $self, $c, $allowed ) = @_;
59  
60 0 my $language = $c->req->cookie('locale');
61 0 ▶ if ( $language && exists $allowed->{ $language->value } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language and exists $$allowed{$language->value}) 0 0 0
Condition0%0/3
$language and exists $$allowed{$language->value}
!ll&&!rl&&r
000
MC/DC0%0/2
$language and exists $$allowed{$language->value}
$language exists $$allowed{$language->value}
Truth table0%0/3
A and B
A= $languageB= exists $$allowed{$language->value}
ABresult
0X0
100
111
62 0 return $language->value;
63 }
64 0 return 0;
65 }
66  
67 0 ▶sub _lang_from_agent {
Subroutine _lang_from_agent: not called CC=2 SCAR=17.9
68 0 my ( $self, $c, $allowed ) = @_;
69  
70 0 my @langs = grep { exists $allowed->{$_} } @{ $c->languages };
71 0 ▶ if (@langs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@langs) 0 0 0
72 0 return $langs[0];
73 }
74 0 return 0;
75 }
76  
77 0 ▶sub _build_language_config {
Subroutine _build_language_config: not called CC=6 SCAR=37.4
78 0 my ( $self, $c ) = @_;
79  
80 # Build defaults
81 0 ▶ my $config = $c->config->{LocaleSelect} || {};
Condition0%0/2
$c->config->{'LocaleSelect'} || {}
l!l
00
MC/DC0%0/1
$c->config->{'LocaleSelect'} || {}
$c->config->{'LocaleSelect'}
Truth table0%0/2
A
A= $c->config->{'LocaleSelect'}
Aresult
11
00
82 0 ▶ unless ( exists $config->{allowed} && ref $config->{allowed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"allowed"} and ref $config->{"allowed"}) 0 0 0
Condition0%0/3
exists $config->{"allowed"} and ref $config->{"allowed"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $config->{"allowed"} and ref $config->{"allowed"}
exists $config->{"allowed"} ref $config->{"allowed"}
Truth table0%0/3
A and B
A= exists $config->{"allowed"}B= ref $config->{"allowed"}
ABresult
0X0
100
111
83 0 ▶ if ( $config->{allowed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'allowed'}) { } 0 0 0
84 0 $config->{allowed} = [ $config->{allowed} ];
85 }
86 else {
87 0 $config->{allowed} = ['en'];
88 0 $config->{default} = 'en';
89 }
90 }
91 $config->{default} = $config->{allowed}[0]
92 0 ▶ unless $config->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"default"} 0 0 0
93  
94 0 return $config;
95 }
96  
97 =head2 auto
98  
99 This will run before your auto action, so the locale info is available even if you need it on your auto action.
100  
101 =cut
102  
103 0 ▶sub auto : Private { 1 }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine auto: not called CC=1 SCAR=0.2
Pod documented
104  
105 =head1 SYNOPSIS
106  
107 On your app class
108  
109 use Catalyst qw/
110 ...
111 I18N # or Data::Localize
112 ...
113 /;
114  
115 __PACKAGE__->config(
116 LocaleSelect => {
117 allowed => [ qw/ en es / ],
118 default => 'en'
119 }
120 );
121  
122 In your controller ( Apply to root controller to be application wide )
123  
124 package MyApp::Controller::Root;
125 use Moose;
126 use namespace::autoclean;
127  
128 BEGIN { extends 'Catalyst::Controller' }
129 with 'Catalyst::TraitFor::Controller::LocaleSelect';
130
131 # ...
132
133 1;
134  
135 =head1 DESCRIPTION
136  
137 This controller role will provide locale selection capabilities to your controllers.
138 You can apply it to the root controller to have it working application wide.
139  
140 Once in use, the controller have auto locale selection among your configured allowed locales. When no one reported by the user agent are allowed, the default will be in use.
141  
142 This role will give to all actions on the controller two more capabilities:
143
144 =over 4
145  
146 =item *
147  
148 One time locale selection if exists param('locale') and have an allowed value.
149  
150 =item
151  
152 * Cookie based locale lock-in selection using param('set_locale').
153  
154 =back
155  
156 It will also populate the locale key on the stash for later extra use:
157  
158 $c->stash( locale => {
159 allowed => [ 'en', 'es' ],
160 default => 'en',
161 selected => 'es' # the one selected for the request
162 });
163  
164 =head1 LOCALE SELECTION PRIORITY
165  
166 =over 4
167  
168 =item 1.
169  
170 If locale parameter exists, this will be selected.
171  
172 =item 2.
173  
174 If set_locale parameter exists, it will be used an cookie stored for later use.
175  
176 =item 3.
177  
178 If cookie exists...
179  
180 =item 4.
181  
182 If any of the browser supported locales exists...
183  
184 =item 5.
185  
186 The default one.
187  
188 =back
189  
190 =head1 AUTHOR
191  
192 Diego Kuperman, C<< <diego at freekeylabs.com> >>
193  
194 =head1 BUGS
195  
196 Please report any bugs or feature requests to C<bug-catalyst-traitfor-controller-localeselect at rt.cpan.org>, or through
197 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-TraitFor-Controller-LocaleSelect>. I will be notified, and then you'll
198 automatically be notified of progress on your bug as I make changes.
199  
200 =head1 SUPPORT
201  
202 You can find documentation for this module with the perldoc command.
203  
204 perldoc Catalyst::TraitFor::Controller::LocaleSelect
205  
206 You can also look for information at:
207  
208 =over 4
209  
210 =item * RT: CPAN's request tracker
211  
212 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-TraitFor-Controller-LocaleSelect>
213  
214 =item * AnnoCPAN: Annotated CPAN documentation
215  
216 L<http://annocpan.org/dist/Catalyst-TraitFor-Controller-LocaleSelect>
217  
218 =item * CPAN Ratings
219  
220 L<http://cpanratings.perl.org/d/Catalyst-TraitFor-Controller-LocaleSelect>
221  
222 =item * Search CPAN
223  
224 L<http://search.cpan.org/dist/Catalyst-TraitFor-Controller-LocaleSelect/>
225  
226 =back
227  
228 =head1 COPYRIGHT & LICENSE
229  
230 Copyright 2010 Diego Kuperman.
231  
232 This program is free software; you can redistribute it and/or modify it
233 under the terms of either: the GNU General Public License as published
234 by the Free Software Foundation; or the Artistic License.
235  
236 See http://dev.perl.org/licenses/ for more information.
237  
238 =cut
239  
240 2 1; # End of Catalyst::TraitFor::Controller::LocaleSelect
↑ Summary