| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::TraitFor::Request::PerLanguageDomains; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4378
|
use 5.008005; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
734
|
|
|
4
|
1
|
|
|
1
|
|
1707
|
use Moose::Role; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use I18N::AcceptLanguage; |
|
6
|
|
|
|
|
|
|
use Moose::Autobox; |
|
7
|
|
|
|
|
|
|
use MooseX::Types -declare => [qw/ ValidConfig /]; |
|
8
|
|
|
|
|
|
|
use MooseX::Types::Moose qw/ ArrayRef /; |
|
9
|
|
|
|
|
|
|
use MooseX::Types::Common::String qw/ NonEmptySimpleStr /; |
|
10
|
|
|
|
|
|
|
use MooseX::Types::Structured qw/ Dict /; |
|
11
|
|
|
|
|
|
|
use namespace::autoclean; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
14
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
requires qw/ |
|
17
|
|
|
|
|
|
|
uri |
|
18
|
|
|
|
|
|
|
_context |
|
19
|
|
|
|
|
|
|
headers |
|
20
|
|
|
|
|
|
|
/; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has language => ( |
|
23
|
|
|
|
|
|
|
init_arg => undef, |
|
24
|
|
|
|
|
|
|
is => 'ro', |
|
25
|
|
|
|
|
|
|
lazy => 1, |
|
26
|
|
|
|
|
|
|
builder => '_build_language', |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
subtype ValidConfig, |
|
30
|
|
|
|
|
|
|
as Dict[ |
|
31
|
|
|
|
|
|
|
default_language => NonEmptySimpleStr, |
|
32
|
|
|
|
|
|
|
selectable_language => ArrayRef([NonEmptySimpleStr])|NonEmptySimpleStr, |
|
33
|
|
|
|
|
|
|
]; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has _perlang_config => ( |
|
36
|
|
|
|
|
|
|
init_arg => undef, traits => ['Hash'], |
|
37
|
|
|
|
|
|
|
is => 'ro', |
|
38
|
|
|
|
|
|
|
isa => ValidConfig, |
|
39
|
|
|
|
|
|
|
lazy => 1, builder => '_build_perlang_config', |
|
40
|
|
|
|
|
|
|
handles => { |
|
41
|
|
|
|
|
|
|
map { q[_] . $_ => [ get => $_ ] } |
|
42
|
|
|
|
|
|
|
qw/default_language selectable_language/ |
|
43
|
|
|
|
|
|
|
}, |
|
44
|
|
|
|
|
|
|
); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _build_perlang_config { |
|
47
|
|
|
|
|
|
|
ref(shift->_context)->config->{'TraitFor::Request::PerLanguageDomains'}; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _build_language { |
|
51
|
|
|
|
|
|
|
my $self = shift; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $i18n_accept_language = I18N::AcceptLanguage->new( |
|
54
|
|
|
|
|
|
|
defaultLanguage => $self->_default_language |
|
55
|
|
|
|
|
|
|
); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $from_host = sub { (($self->uri->host =~ m{^(\w{2})\.}) ? $1 : undef) }; |
|
58
|
|
|
|
|
|
|
my $from_session = sub { |
|
59
|
|
|
|
|
|
|
my $ctx = $self->_context; |
|
60
|
|
|
|
|
|
|
if ( my $session_meth = $ctx->can('session') ) { |
|
61
|
|
|
|
|
|
|
$session_meth->($ctx)->{'language'}; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
}; |
|
64
|
|
|
|
|
|
|
my $from_header = sub { $self->headers->header('Accept-language') }; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return $i18n_accept_language->accepts( |
|
67
|
|
|
|
|
|
|
$from_host->() || $from_session->() || $from_header->(), |
|
68
|
|
|
|
|
|
|
[ $self->_selectable_language->flatten ] |
|
69
|
|
|
|
|
|
|
); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=pod |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 NAME |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Catalyst::TraitFor::Request::PerLanguageDomains - Language detection for Catalyst::Requests |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
package MyApp; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
use Moose; |
|
83
|
|
|
|
|
|
|
use namespace::autoclean; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
use Catalyst; |
|
86
|
|
|
|
|
|
|
use CatalystX::RoleApplicator; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
extends 'Catalyst'; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__PACKAGE__->apply_request_class_roles(qw/ |
|
91
|
|
|
|
|
|
|
Catalyst::TraitFor::Request::PerLanguageDomains |
|
92
|
|
|
|
|
|
|
/); |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__PACKAGE__->config( |
|
95
|
|
|
|
|
|
|
'TraitFor::Request::PerLanguageDomains' => { |
|
96
|
|
|
|
|
|
|
default_language => 'de', |
|
97
|
|
|
|
|
|
|
selectable_language => ['de','en'], |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__PACKAGE__->setup; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Config::General style: |
|
104
|
|
|
|
|
|
|
<TraitFor::Request::PerLanguageDomains> |
|
105
|
|
|
|
|
|
|
default_language de |
|
106
|
|
|
|
|
|
|
selectable_language de |
|
107
|
|
|
|
|
|
|
selectable_language en |
|
108
|
|
|
|
|
|
|
</Catalyst::Request> |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Extends L<Catalyst::Request> objects with a C<< $ctx->request->language >> |
|
113
|
|
|
|
|
|
|
method for language detection. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 METHODS |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 language |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $language = $ctx->request->language; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Returns a string that is the two digit code ISO for the request language. |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The following things are checked to find the request language, in order: |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=over |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item * |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
The lang part of the domain (e.g. de from de.example.org) |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
The C<language> key set in the session (if L<Catalyst::Plugin::Session> is loaded) |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
The C<Accept-Language> header of the request. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L<CatalystX::RoleApplicator>, L<I18N::AcceptLanguage>. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 AUTHOR |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Stephan Jauernick <stephan@stejau.de> |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 LICENSE |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This software is copyright (c) 2009 by Stephan Jauernick. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
154
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |