LP#1629078 IDL2js correctly parse Accept-Language header user/berick/lp1629078-idl2js-locale-parse
authorBill Erickson <berickxx@gmail.com>
Mon, 3 Oct 2016 15:59:18 +0000 (11:59 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 3 Oct 2016 16:09:29 +0000 (12:09 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/IDL2js.pm

index a7486e4..cfb269d 100644 (file)
@@ -73,7 +73,8 @@ sub handler {
 
     # if no valid locale is present in the query 
     # string, pull it from the headers
-    $locale = $r->headers_in->get('Accept-Language') unless $locale;
+    $locale = parse_accept_lang($r->headers_in->get('Accept-Language')) 
+        unless $locale;
 
     if (!$locale or $locale !~ /^[a-z]{2}-[A-Z]{2}$/) {
         $r->log->debug("Invalid IDL2js locale '$locale'; using en-US");
@@ -91,6 +92,19 @@ sub handler {
     return Apache2::Const::OK;
 }
 
+# turn Accept-Language into something EG can understand
+# TODO: try all langs, not just the first
+sub parse_accept_lang {
+    my $al = shift;
+    return undef unless $al;
+    my ($locale) = split(/,/, $al);
+    ($locale) = split(/;/, $locale);
+    return undef unless $locale;
+    $locale =~ s/_/-/og;
+    return $locale;
+}
+
+
 # loads the IDL for the provided locale.
 # when possible, use a cached version of the IDL.
 sub load_IDL {