TPAC i18n: Turn UA locales into Evergreen locales
authorDan Scott <dscott@laurentian.ca>
Fri, 17 Aug 2012 19:06:59 +0000 (15:06 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 20 Aug 2012 20:16:59 +0000 (16:16 -0400)
We need to convert the likes of fr_ca into fr-CA to be able to pull the
appropriate localized values from the database via CStoreEditor.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Signed-off-by: Bill Erickson <berick@esilibrary.com>

Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm

index 02cf017..44c6018 100644 (file)
@@ -170,9 +170,8 @@ sub load_context {
         $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) || 
         parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en_us';
 
-
     # set the editor default locale for each page load
-    $OpenILS::Utils::CStoreEditor::default_locale = $ctx->{locale};
+    $OpenILS::Utils::CStoreEditor::default_locale = parse_eg_locale($ctx->{locale});
 
     my $mprefix = $ctx->{media_prefix};
     if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
@@ -183,7 +182,7 @@ sub load_context {
     return $ctx;
 }
 
-# turn Accept-Language into sometihng EG can understand
+# turn Accept-Language into something EG can understand
 # TODO: try all langs, not just the first
 sub parse_accept_lang {
     my $al = shift;
@@ -195,6 +194,18 @@ sub parse_accept_lang {
     return $locale;
 }
 
+# Accept-Language uses locales like 'en', 'fr', 'fr_fr', while Evergreen
+# internally uses 'en-US', 'fr-CA', 'fr-FR' (always with the 2 lowercase,
+# hyphen, 2 uppercase convention)
+sub parse_eg_locale {
+    my $ua_locale = shift || 'en_us';
+
+    $ua_locale =~ m/^(..).?(..)?$/;
+    my $lang_code = lc($1);
+    my $region_code = $2 ? uc($2) : uc($1);
+    return "$lang_code-$region_code";
+}
+
 # Given a URI, finds the configured template and any extra page 
 # arguments (trailing path info).  Any extra data is returned
 # as page arguments, in the form of an array, one item per