changeset 361:eff7f75a4408

added cookie support for the request language detection
author cin
date Wed, 27 Nov 2013 17:12:38 +0400
parents 39842eedd923
children 715b9959b207
files Lib/IMPL/Web/Handler/LocaleHandler.pm _test/Resources/person.xsd
diffstat 2 files changed, 44 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Web/Handler/LocaleHandler.pm	Tue Nov 26 03:22:44 2013 +0400
+++ b/Lib/IMPL/Web/Handler/LocaleHandler.pm	Wed Nov 27 17:12:38 2013 +0400
@@ -13,43 +13,51 @@
 	],
 	props => [
 		locales => PROP_RO | PROP_LIST,
-		default => PROP_RO
+		default => PROP_RO,
+		cookie  => PROP_RO
 	]
 };
 
 sub Invoke {
     my ($this,$action,$nextHandler) = @_;
     
-    my @matches;
-    
-    my $best = [$this->default,0];
+    my $locale;
     
-    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 ($this->cookie and my $cookie = $action->cookie($this->cookie)) {
+    	($locale) = grep /^$cookie/i, $this->locales;
     }
     
-    if (my $locale = $best->[0]) {
-    	Resources->SetLocale($locale);
+    unless($locale) {
+	    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;
+	    		}
+	    	}
+	    	
+	    }
+	    
+	    $locale = $best->[0];
     }
     
+    Resources->SetLocale($locale) if $locale;
+    
     return $nextHandler->($action);
 }
 
@@ -68,7 +76,14 @@
 =begin code xml
 
 	<handlers type="ARRAY">
-		<item type="IMPL::Web::Handler::LocationHandler"/>
+		<item type="IMPL::Web::Handler::LocaleHandler">
+			<locales type="ARRAY">
+				<item>en-US</item>
+				<item>ru-RU</item>
+			</locales>
+			<default>en-US</default>
+			<cookie>lang</cookie>
+		</item>
 	</handlers>
 
 =end code xml
--- a/_test/Resources/person.xsd	Tue Nov 26 03:22:44 2013 +0400
+++ b/_test/Resources/person.xsd	Wed Nov 27 17:12:38 2013 +0400
@@ -3,9 +3,10 @@
 	targetNamespace="http://implab.org/person/"
 	xmlns:xs="http://www.w3.org/2001/XMLSchema"
 	xmlns:tns="http://implab.org/person/"
+	xmlns:impl="http://implab.org/schemas/impl-bindings"
 	elementFormDefault="qualified"
 >
-    <xs:complexType name="personInfo">
+    <xs:complexType name="personInfo" impl:bind-type="Person" >
     	<xs:sequence>
     		<xs:element name="firstName" type="xs:string" />
     		<xs:element name="lastName" type="xs:string" />