annotate 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
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);
39842eedd923 language detection from request
sergey
parents:
diff changeset
5 use IMPL::declare {
39842eedd923 language detection from request
sergey
parents:
diff changeset
6 require => {
39842eedd923 language detection from request
sergey
parents:
diff changeset
7 Resources => 'IMPL::Resources'
39842eedd923 language detection from request
sergey
parents:
diff changeset
8 },
39842eedd923 language detection from request
sergey
parents:
diff changeset
9 base => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
10 'IMPL::Object' => undef,
39842eedd923 language detection from request
sergey
parents:
diff changeset
11 'IMPL::Object::Autofill' => '@_',
39842eedd923 language detection from request
sergey
parents:
diff changeset
12 'IMPL::Object::Serializable' => undef
39842eedd923 language detection from request
sergey
parents:
diff changeset
13 ],
39842eedd923 language detection from request
sergey
parents:
diff changeset
14 props => [
39842eedd923 language detection from request
sergey
parents:
diff changeset
15 locales => PROP_RO | PROP_LIST,
39842eedd923 language detection from request
sergey
parents:
diff changeset
16 default => PROP_RO
39842eedd923 language detection from request
sergey
parents:
diff changeset
17 ]
39842eedd923 language detection from request
sergey
parents:
diff changeset
18 };
39842eedd923 language detection from request
sergey
parents:
diff changeset
19
39842eedd923 language detection from request
sergey
parents:
diff changeset
20 sub Invoke {
39842eedd923 language detection from request
sergey
parents:
diff changeset
21 my ($this,$action,$nextHandler) = @_;
39842eedd923 language detection from request
sergey
parents:
diff changeset
22
39842eedd923 language detection from request
sergey
parents:
diff changeset
23 my @matches;
39842eedd923 language detection from request
sergey
parents:
diff changeset
24
39842eedd923 language detection from request
sergey
parents:
diff changeset
25 my $best = [$this->default,0];
39842eedd923 language detection from request
sergey
parents:
diff changeset
26
39842eedd923 language detection from request
sergey
parents:
diff changeset
27 if(my $header = $action->header('Accept-Language')) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
28 foreach my $part (split(/\s*,\s*/, $header)) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
29 my ($lang,$quality) = ($part =~ /([a-z]+(?:\-[a-z]+)*)(?:\s*;\s*q=(0\.[\d]+|1))?/i );
39842eedd923 language detection from request
sergey
parents:
diff changeset
30
39842eedd923 language detection from request
sergey
parents:
diff changeset
31 $quality ||=1;
39842eedd923 language detection from request
sergey
parents:
diff changeset
32
39842eedd923 language detection from request
sergey
parents:
diff changeset
33 foreach my $tag ($this->locales) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
34 if ( $tag =~ m/^$lang/i ) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
35 push @matches, [$tag,$quality];
39842eedd923 language detection from request
sergey
parents:
diff changeset
36 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
37 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
38 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
39
39842eedd923 language detection from request
sergey
parents:
diff changeset
40 foreach my $match (@matches) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
41 if ($match->[1] > $best->[1]) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
42 $best = $match;
39842eedd923 language detection from request
sergey
parents:
diff changeset
43 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
44 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
45
39842eedd923 language detection from request
sergey
parents:
diff changeset
46
39842eedd923 language detection from request
sergey
parents:
diff changeset
47 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
48
39842eedd923 language detection from request
sergey
parents:
diff changeset
49 if (my $locale = $best->[0]) {
39842eedd923 language detection from request
sergey
parents:
diff changeset
50 Resources->SetLocale($locale);
39842eedd923 language detection from request
sergey
parents:
diff changeset
51 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
52
39842eedd923 language detection from request
sergey
parents:
diff changeset
53 return $nextHandler->($action);
39842eedd923 language detection from request
sergey
parents:
diff changeset
54 }
39842eedd923 language detection from request
sergey
parents:
diff changeset
55
39842eedd923 language detection from request
sergey
parents:
diff changeset
56 1;
39842eedd923 language detection from request
sergey
parents:
diff changeset
57
39842eedd923 language detection from request
sergey
parents:
diff changeset
58 __END__
39842eedd923 language detection from request
sergey
parents:
diff changeset
59
39842eedd923 language detection from request
sergey
parents:
diff changeset
60 =pod
39842eedd923 language detection from request
sergey
parents:
diff changeset
61
39842eedd923 language detection from request
sergey
parents:
diff changeset
62 =head1 NAME
39842eedd923 language detection from request
sergey
parents:
diff changeset
63
39842eedd923 language detection from request
sergey
parents:
diff changeset
64 C<IMPL::Web::Handler::LocaleHandler> - handles locale for the request
39842eedd923 language detection from request
sergey
parents:
diff changeset
65
39842eedd923 language detection from request
sergey
parents:
diff changeset
66 =head1 SYNOPSIS
39842eedd923 language detection from request
sergey
parents:
diff changeset
67
39842eedd923 language detection from request
sergey
parents:
diff changeset
68 =begin code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
69
39842eedd923 language detection from request
sergey
parents:
diff changeset
70 <handlers type="ARRAY">
39842eedd923 language detection from request
sergey
parents:
diff changeset
71 <item type="IMPL::Web::Handler::LocationHandler"/>
39842eedd923 language detection from request
sergey
parents:
diff changeset
72 </handlers>
39842eedd923 language detection from request
sergey
parents:
diff changeset
73
39842eedd923 language detection from request
sergey
parents:
diff changeset
74 =end code xml
39842eedd923 language detection from request
sergey
parents:
diff changeset
75
39842eedd923 language detection from request
sergey
parents:
diff changeset
76 =cut