Mercurial > pub > Impl
comparison Lib/IMPL/Web/Handler/LocaleHandler.pm @ 360:39842eedd923
language detection from request
author | sergey |
---|---|
date | Tue, 26 Nov 2013 03:22:44 +0400 |
parents | |
children | eff7f75a4408 |
comparison
equal
deleted
inserted
replaced
359:833e663796c4 | 360:39842eedd923 |
---|---|
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, | |
16 default => PROP_RO | |
17 ] | |
18 }; | |
19 | |
20 sub Invoke { | |
21 my ($this,$action,$nextHandler) = @_; | |
22 | |
23 my @matches; | |
24 | |
25 my $best = [$this->default,0]; | |
26 | |
27 if(my $header = $action->header('Accept-Language')) { | |
28 foreach my $part (split(/\s*,\s*/, $header)) { | |
29 my ($lang,$quality) = ($part =~ /([a-z]+(?:\-[a-z]+)*)(?:\s*;\s*q=(0\.[\d]+|1))?/i ); | |
30 | |
31 $quality ||=1; | |
32 | |
33 foreach my $tag ($this->locales) { | |
34 if ( $tag =~ m/^$lang/i ) { | |
35 push @matches, [$tag,$quality]; | |
36 } | |
37 } | |
38 } | |
39 | |
40 foreach my $match (@matches) { | |
41 if ($match->[1] > $best->[1]) { | |
42 $best = $match; | |
43 } | |
44 } | |
45 | |
46 | |
47 } | |
48 | |
49 if (my $locale = $best->[0]) { | |
50 Resources->SetLocale($locale); | |
51 } | |
52 | |
53 return $nextHandler->($action); | |
54 } | |
55 | |
56 1; | |
57 | |
58 __END__ | |
59 | |
60 =pod | |
61 | |
62 =head1 NAME | |
63 | |
64 C<IMPL::Web::Handler::LocaleHandler> - handles locale for the request | |
65 | |
66 =head1 SYNOPSIS | |
67 | |
68 =begin code xml | |
69 | |
70 <handlers type="ARRAY"> | |
71 <item type="IMPL::Web::Handler::LocationHandler"/> | |
72 </handlers> | |
73 | |
74 =end code xml | |
75 | |
76 =cut |