# HG changeset patch # User sergey # Date 1385421764 -14400 # Node ID 39842eedd923304f6fdb24a65e7d28acd93b8900 # Parent 833e663796c4ca98e5a3daa1fde42ec22b56140d language detection from request diff -r 833e663796c4 -r 39842eedd923 Lib/IMPL/Resources.pm --- a/Lib/IMPL/Resources.pm Mon Nov 25 02:19:31 2013 +0400 +++ b/Lib/IMPL/Resources.pm Tue Nov 26 03:22:44 2013 +0400 @@ -10,13 +10,16 @@ sub SetLocale { my ($self,$locale) = @_; + $locale =~ tr/\-/_/; + $CurrentLocale = $locale; } sub InvokeInLocale { my ($this,$locale,$code) = @_; - local $CurrentLocale = $locale; + local $CurrentLocale; + $this->SetLocale($locale); eval { &$code() if $code; diff -r 833e663796c4 -r 39842eedd923 Lib/IMPL/Web/Handler/LocaleHandler.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/IMPL/Web/Handler/LocaleHandler.pm Tue Nov 26 03:22:44 2013 +0400 @@ -0,0 +1,76 @@ +package IMPL::Web::Handler::LocaleHandler; +use strict; + +use IMPL::Const qw(:prop); +use IMPL::declare { + require => { + Resources => 'IMPL::Resources' + }, + base => [ + 'IMPL::Object' => undef, + 'IMPL::Object::Autofill' => '@_', + 'IMPL::Object::Serializable' => undef + ], + props => [ + locales => PROP_RO | PROP_LIST, + default => PROP_RO + ] +}; + +sub Invoke { + my ($this,$action,$nextHandler) = @_; + + my @matches; + + my $best = [$this->default,0]; + + if(my $header = $action->header('Accept-Language')) { + foreach my $part (split(/\s*,\s*/, $header)) { + my ($lang,$quality) = ($part =~ /([a-z]+(?:\-[a-z]+)*)(?:\s*;\s*q=(0\.[\d]+|1))?/i ); + + $quality ||=1; + + foreach my $tag ($this->locales) { + if ( $tag =~ m/^$lang/i ) { + push @matches, [$tag,$quality]; + } + } + } + + foreach my $match (@matches) { + if ($match->[1] > $best->[1]) { + $best = $match; + } + } + + + } + + if (my $locale = $best->[0]) { + Resources->SetLocale($locale); + } + + return $nextHandler->($action); +} + +1; + +__END__ + +=pod + +=head1 NAME + +C - handles locale for the request + +=head1 SYNOPSIS + +=begin code xml + + + + + +=end code xml + +=cut \ No newline at end of file