| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::TraitFor::Controller::LocaleSelect; |
|
2
|
1
|
|
|
1
|
|
25587
|
use MooseX::MethodAttributes::Role; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use namespace::autoclean; |
|
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
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
before 'auto' => sub { |
|
18
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $config = $self->_build_language_config($c); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|| $config->{default}; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$c->languages( [ $config->{selected} ] ); |
|
30
|
|
|
|
|
|
|
$c->stash( locale => $config ); |
|
31
|
|
|
|
|
|
|
}; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _lang_for_request { |
|
34
|
|
|
|
|
|
|
my ( $self, $c, $allowed ) = @_; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $language = delete $c->req->params->{'locale'}; |
|
37
|
|
|
|
|
|
|
if ( $language && exists $allowed->{ $language } ) { |
|
38
|
|
|
|
|
|
|
return $language; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
return 0; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _lang_for_set { |
|
44
|
|
|
|
|
|
|
my ( $self, $c, $allowed ) = @_; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $language = delete $c->req->params->{'set_locale'}; |
|
47
|
|
|
|
|
|
|
if ( $language && exists $allowed->{ $language } ) { |
|
48
|
|
|
|
|
|
|
$c->res->cookies->{'locale'} = { |
|
49
|
|
|
|
|
|
|
value => $language, |
|
50
|
|
|
|
|
|
|
expires => '+5y' |
|
51
|
|
|
|
|
|
|
}; |
|
52
|
|
|
|
|
|
|
return $language; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
return 0; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _lang_from_cookie { |
|
58
|
|
|
|
|
|
|
my ( $self, $c, $allowed ) = @_; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $language = $c->req->cookie('locale'); |
|
61
|
|
|
|
|
|
|
if ( $language && exists $allowed->{ $language->value } ) { |
|
62
|
|
|
|
|
|
|
return $language->value; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
return 0; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _lang_from_agent { |
|
68
|
|
|
|
|
|
|
my ( $self, $c, $allowed ) = @_; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my @langs = grep { exists $allowed->{$_} } @{ $c->languages }; |
|
71
|
|
|
|
|
|
|
if (@langs) { |
|
72
|
|
|
|
|
|
|
return $langs[0]; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
return 0; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _build_language_config { |
|
78
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Build defaults |
|
81
|
|
|
|
|
|
|
my $config = $c->config->{LocaleSelect} || {}; |
|
82
|
|
|
|
|
|
|
unless ( exists $config->{allowed} && ref $config->{allowed} ) { |
|
83
|
|
|
|
|
|
|
if ( $config->{allowed} ) { |
|
84
|
|
|
|
|
|
|
$config->{allowed} = [ $config->{allowed} ]; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
else { |
|
87
|
|
|
|
|
|
|
$config->{allowed} = ['en']; |
|
88
|
|
|
|
|
|
|
$config->{default} = 'en'; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
$config->{default} = $config->{allowed}[0] |
|
92
|
|
|
|
|
|
|
unless $config->{default}; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub auto : Private { 1 } |
|
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
|
|
|
|
|
|
|
1; # End of Catalyst::TraitFor::Controller::LocaleSelect |