Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/LocaleHandler.pm @ 391:2287c72f303a
code cleanup
author | cin |
---|---|
date | Thu, 13 Feb 2014 20:17:22 +0400 |
parents | eff7f75a4408 |
children | 212cc86e470b |
rev | line source |
---|---|
360 | 1 package IMPL::Web::Handler::LocaleHandler; |
2 use strict; | |
3 | |
4 use IMPL::Const qw(:prop); | |
5 use IMPL::declare { | |
6 require => { | |
7 Resources => 'IMPL::Resources' | |
8 }, | |
9 base => [ | |
10 'IMPL::Object' => undef, | |
11 'IMPL::Object::Autofill' => '@_', | |
12 'IMPL::Object::Serializable' => undef | |
13 ], | |
14 props => [ | |
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 | 18 ] |
19 }; | |
20 | |
21 sub Invoke { | |
22 my ($this,$action,$nextHandler) = @_; | |
23 | |
361
eff7f75a4408
added cookie support for the request language detection
cin
parents:
360
diff
changeset
|
24 my $locale; |
360 | 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 | 28 } |
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 | 57 } |
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 | 61 return $nextHandler->($action); |
62 } | |
63 | |
64 1; | |
65 | |
66 __END__ | |
67 | |
68 =pod | |
69 | |
70 =head1 NAME | |
71 | |
72 C<IMPL::Web::Handler::LocaleHandler> - handles locale for the request | |
73 | |
74 =head1 SYNOPSIS | |
75 | |
76 =begin code xml | |
77 | |
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 | 87 </handlers> |
88 | |
89 =end code xml | |
90 | |
91 =cut |