annotate Lib/IMPL/Web/Handler/LocaleHandler.pm @ 396:6f2a494579cb

sync
author sergey
date Mon, 24 Feb 2014 01:55:46 +0400
parents 212cc86e470b
children
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);
395
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
5 use DateTime;
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
6 use IMPL::declare {
39842eedd923 language detection from request
sergey
parents:
diff changeset
7 require => {
39842eedd923 language detection from request
sergey
parents:
diff changeset
8 Resources => 'IMPL::Resources'
39842eedd923 language detection from request
sergey
parents:
diff changeset
9 },
39842eedd923 language detection from request
sergey
parents:
diff changeset
10 base => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
11 'IMPL::Object' => undef,
39842eedd923 language detection from request
sergey
parents:
diff changeset
12 'IMPL::Object::Autofill' => '@_',
39842eedd923 language detection from request
sergey
parents:
diff changeset
13 'IMPL::Object::Serializable' => undef
39842eedd923 language detection from request
sergey
parents:
diff changeset
14 ],
39842eedd923 language detection from request
sergey
parents:
diff changeset
15 props => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
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
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
39842eedd923 language detection from request
sergey
parents:
diff changeset
22 sub Invoke {
39842eedd923 language detection from request
sergey
parents:
diff changeset
23 my ($this,$action,$nextHandler) = @_;
39842eedd923 language detection from request
sergey
parents:
diff changeset
24
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
25 my $locale;
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
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
39842eedd923 language detection from request
sergey
parents:
diff changeset
29 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
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
39842eedd923 language detection from request
sergey
parents:
diff changeset
58 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
59
395
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
60 if($locale) {
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
61 Resources->SetLocale($locale);
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
62 #$locale =~ tr/-/_/;
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
63 DateTime->DefaultLocale($locale);
212cc86e470b Code cleanup
sergey
parents: 361
diff changeset
64 }
361
eff7f75a4408 added cookie support for the request language detection
cin
parents: 360
diff changeset
65
360
39842eedd923 language detection from request
sergey
parents:
diff changeset
66 return $nextHandler->($action);
39842eedd923 language detection from request
sergey
parents:
diff changeset
67 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
68
39842eedd923 language detection from request
sergey
parents:
diff changeset
69 1;
39842eedd923 language detection from request
sergey
parents:
diff changeset
70
39842eedd923 language detection from request
sergey
parents:
diff changeset
71 __END__
39842eedd923 language detection from request
sergey
parents:
diff changeset
72
39842eedd923 language detection from request
sergey
parents:
diff changeset
73 =pod
39842eedd923 language detection from request
sergey
parents:
diff changeset
74
39842eedd923 language detection from request
sergey
parents:
diff changeset
75 =head1 NAME
39842eedd923 language detection from request
sergey
parents:
diff changeset
76
39842eedd923 language detection from request
sergey
parents:
diff changeset
77 C<IMPL::Web::Handler::LocaleHandler> - handles locale for the request
39842eedd923 language detection from request
sergey
parents:
diff changeset
78
39842eedd923 language detection from request
sergey
parents:
diff changeset
79 =head1 SYNOPSIS
39842eedd923 language detection from request
sergey
parents:
diff changeset
80
39842eedd923 language detection from request
sergey
parents:
diff changeset
81 =begin code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
82
39842eedd923 language detection from request
sergey
parents:
diff changeset
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
39842eedd923 language detection from request
sergey
parents:
diff changeset
92 </handlers>
39842eedd923 language detection from request
sergey
parents:
diff changeset
93
39842eedd923 language detection from request
sergey
parents:
diff changeset
94 =end code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
95
39842eedd923 language detection from request
sergey
parents:
diff changeset
96 =cut