annotate Lib/IMPL/Web/Handler/LocaleHandler.pm @ 374:6608db5dcb81

sync
author cin
date Thu, 09 Jan 2014 19:40:33 +0400
parents eff7f75a4408
children 212cc86e470b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
1 package IMPL::Web::Handler::LocaleHandler;
39842eedd923 language detection from request
sergey
parents:
diff changeset
2 use strict;
39842eedd923 language detection from request
sergey
parents:
diff changeset
3
39842eedd923 language detection from request
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop);
39842eedd923 language detection from request
sergey
parents:
diff changeset
5 use IMPL::declare {
39842eedd923 language detection from request
sergey
parents:
diff changeset
6 require => {
39842eedd923 language detection from request
sergey
parents:
diff changeset
7 Resources => 'IMPL::Resources'
39842eedd923 language detection from request
sergey
parents:
diff changeset
8 },
39842eedd923 language detection from request
sergey
parents:
diff changeset
9 base => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
10 'IMPL::Object' => undef,
39842eedd923 language detection from request
sergey
parents:
diff changeset
11 'IMPL::Object::Autofill' => '@_',
39842eedd923 language detection from request
sergey
parents:
diff changeset
12 'IMPL::Object::Serializable' => undef
39842eedd923 language detection from request
sergey
parents:
diff changeset
13 ],
39842eedd923 language detection from request
sergey
parents:
diff changeset
14 props => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
15 locales => PROP_RO | PROP_LIST,
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
16 default => PROP_RO,
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
17 cookie => PROP_RO
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
18 ]
39842eedd923 language detection from request
sergey
parents:
diff changeset
19 };
39842eedd923 language detection from request
sergey
parents:
diff changeset
20
39842eedd923 language detection from request
sergey
parents:
diff changeset
21 sub Invoke {
39842eedd923 language detection from request
sergey
parents:
diff changeset
22 my ($this,$action,$nextHandler) = @_;
39842eedd923 language detection from request
sergey
parents:
diff changeset
23
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
24 my $locale;
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
25
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
26 if ($this->cookie and my $cookie = $action->cookie($this->cookie)) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
27 ($locale) = grep /^$cookie/i, $this->locales;
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
28 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
29
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
30 unless($locale) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
31 my @matches;
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
32
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
33 my $best = [$this->default,0];
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
34
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
35 if(my $header = $action->header('Accept-Language')) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
36 foreach my $part (split(/\s*,\s*/, $header)) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
37 my ($lang,$quality) = ($part =~ /([a-z]+(?:\-[a-z]+)*)(?:\s*;\s*q=(0\.[\d]+|1))?/i );
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
38
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
39 $quality ||=1;
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
40
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
41 foreach my $tag ($this->locales) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
42 if ( $tag =~ m/^$lang/i ) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
43 push @matches, [$tag,$quality];
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
44 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
45 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
46 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
47
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
48 foreach my $match (@matches) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
49 if ($match->[1] > $best->[1]) {
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
50 $best = $match;
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
51 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
52 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
53
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
54 }
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
55
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
56 $locale = $best->[0];
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
57 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
58
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
59 Resources->SetLocale($locale) if $locale;
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
60
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
61 return $nextHandler->($action);
39842eedd923 language detection from request
sergey
parents:
diff changeset
62 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
63
39842eedd923 language detection from request
sergey
parents:
diff changeset
64 1;
39842eedd923 language detection from request
sergey
parents:
diff changeset
65
39842eedd923 language detection from request
sergey
parents:
diff changeset
66 __END__
39842eedd923 language detection from request
sergey
parents:
diff changeset
67
39842eedd923 language detection from request
sergey
parents:
diff changeset
68 =pod
39842eedd923 language detection from request
sergey
parents:
diff changeset
69
39842eedd923 language detection from request
sergey
parents:
diff changeset
70 =head1 NAME
39842eedd923 language detection from request
sergey
parents:
diff changeset
71
39842eedd923 language detection from request
sergey
parents:
diff changeset
72 C<IMPL::Web::Handler::LocaleHandler> - handles locale for the request
39842eedd923 language detection from request
sergey
parents:
diff changeset
73
39842eedd923 language detection from request
sergey
parents:
diff changeset
74 =head1 SYNOPSIS
39842eedd923 language detection from request
sergey
parents:
diff changeset
75
39842eedd923 language detection from request
sergey
parents:
diff changeset
76 =begin code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
77
39842eedd923 language detection from request
sergey
parents:
diff changeset
78 <handlers type="ARRAY">
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
79 <item type="IMPL::Web::Handler::LocaleHandler">
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
80 <locales type="ARRAY">
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
81 <item>en-US</item>
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
82 <item>ru-RU</item>
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
83 </locales>
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
84 <default>en-US</default>
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
85 <cookie>lang</cookie>
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
86 </item>
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
87 </handlers>
39842eedd923 language detection from request
sergey
parents:
diff changeset
88
39842eedd923 language detection from request
sergey
parents:
diff changeset
89 =end code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
90
39842eedd923 language detection from request
sergey
parents:
diff changeset
91 =cut