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