annotate lib/IMPL/Web/Handler/LocaleHandler.pm @ 417:3ed0c58e9da3 ref20150831

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