[ghelda-devel] [58] Adapt HTTP lang and enc to new locale

[ Thread Index | Date Index | More lists.tuxfamily.org/ghelda-devel Archives ]


Revision: 58
Author:   odyx
Date:     2009-07-21 20:58:37 +0200 (Tue, 21 Jul 2009)

Log Message:
-----------
Adapt HTTP lang and enc to new locale

Correctly parses HTTP Accept-Language and Accept-Encoding to
choose an accurate locale.

Modified Paths:
--------------
    trunk/inc/g_gettext.php
    trunk/init/gettext.php


Modified: trunk/inc/g_gettext.php
===================================================================
--- trunk/inc/g_gettext.php	2009-07-21 18:58:34 UTC (rev 57)
+++ trunk/inc/g_gettext.php	2009-07-21 18:58:37 UTC (rev 58)
@@ -72,4 +72,46 @@
     return array($g_GT_oldLocale,$g_GT_oldDomain);
 }
 
+function g_getCorrectLocaleFromHTTPValues($charsets,$languages) {
+    global $g_GT_supportedLocales;
+    
+    // Priority :
+    // 1) Language (Human)
+    // 2) Charset  (Machine)
+    $i = 0; $found = false;
+    foreach($languages as $lang => $q_l) {
+        // fr-ch => fr_CH; fr => fr
+        preg_match("#^(.*?)(\-(.*?))?$#i",$lang, $ayM);
+        $langs = array($ayM[1]);
+        // Prepare a two-dim array('fr_CH','fr')
+        if( array_key_exists(3,$ayM))
+            array_unshift($langs,$langs[0].'_'.strtoupper($ayM[3]));
+        
+        foreach($langs as $lang) {
+            foreach($charsets as $charset => $q_c) {
+                // Lowercase and take dashes and stars ('*' only) out
+                $charset = strtolower(str_replace(array('-','*'),'',$charset));
+                $langChar = $lang;
+                if( !empty($charset) )
+                    $langChar .= '.'.$charset;
+                // If the recomposed string is supported
+                if( in_array($langChar, $g_GT_supportedLocales) ) {
+                    return $langChar;
+                    $found = true;
+                    break;
+                // Special case for plain english => has to return C
+                } elseif ( $langChar == 'en') {
+                    return 'C';
+                    $found = true;
+                    break;
+                }
+            }
+            if($found) break;
+        }
+        if($found) break;
+    }
+    
+    return $langChar;
+}
+
 ?>

Modified: trunk/init/gettext.php
===================================================================
--- trunk/init/gettext.php	2009-07-21 18:58:34 UTC (rev 57)
+++ trunk/init/gettext.php	2009-07-21 18:58:37 UTC (rev 58)
@@ -67,19 +67,24 @@
 // Define the locale
 if(isset($g_GT_urlLocale)) {
     $locale = $g_GT_urlLocale;
+    gol('Language choice (URL):',$g_GT_urlLocale);
 } elseif (!empty($g_GT_userLocale)) {
     $locale = $g_GT_userLocale;
+    gol('Language choice (User setting):',$g_GT_userLocale);
 } elseif (isset($g_GT_cookieLocale)) {
     $locale = $g_GT_cookieLocale;
+    gol('Language choice (Cookie):',$g_GT_cookieLocale);
 } else {
     $locale = LOCALE_DEFAULT;
     // Get the language given by the "Accept-Language" HTTP header
     if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
         // Create the list of charsets
-        foreach(explode(',',$_SERVER['HTTP_ACCEPT_CHARSET']) as $ccharset) { 
-            preg_match("#^(.*?)([-_].*?)?(\;q\=(.*))?$#i", $ccharset, $ayM);
-            $q = isset($ayM[4]) ? $ayM[4] : '1.0'; 
+        foreach(explode(',',$_SERVER['HTTP_ACCEPT_CHARSET']) as $ccharset) {
+            preg_match("#^(.*?)?(\;q\=(.*))?$#i", $ccharset, $ayM);
+            $q = isset($ayM[3]) ? $ayM[3] : '1.0'; 
             $charset = strtolower(trim($ayM[1]));
+            // Special case for '*' => make it a little lower
+            $q = $charset == '*' ? $q-1e-9 : $q;
             $charsets[$charset] = $q;
         }
         arsort($charsets,SORT_NUMERIC);
@@ -87,32 +92,17 @@
         // Create the list of languages
         $selected_q = 0.0;
         foreach(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']) as $llang) { 
-            preg_match("#^(.*?)([-_].*?)?(\;q\=(.*))?$#i", $llang, $ayM);
-            $q = isset($ayM[4]) ? $ayM[4] : '1.0'; 
+            preg_match("#^(.*?)?(\;q\=(.*))?$#i", $llang, $ayM);
+            $q = isset($ayM[3]) ? $ayM[3] : '1.0'; 
             $lang = strtolower(trim($ayM[1]));
             $languages[$lang] = $q;
         }
         arsort($languages,SORT_NUMERIC);
 
-        $charsetsToUpper['utf'] = "UTF-8";
-
-        // Assume that both arrays are already sorted in descending order (of q) (THEY ARE NOT) TODO
-
-        $i = 0; $found = false;
-        foreach($languages as $lang) {
-            foreach($charsets as $charset) {
-                $charset = array_key_exists($charset[0],$charsetsToUpper) ? $charsetsToUpper[$charset[0]] : $charset[0];
-                $localeName = $lang[0].'.'.$charset;
-                if(array_search($localeName,$g_GT_supportedLocales) !== false) {
-                    $locale = $localeName;
-                    $found = true;
-                    break;
-                }
-            }
-            if($found) break;
-        }
+        // Assume that both arrays are already sorted in descending order of q
+        $g_GT_HTTP_Locale = g_getCorrectLocaleFromHTTPValues($charsets,$languages);
     }
-    
+    gol('Language choice (HTTP Header):',$g_GT_HTTP_Locale);
 }
 // header += "Accept-Language: " + m_request.languages + ";q=0.1\r\n";
 
@@ -130,4 +120,3 @@
 // */
 
 ?>
-


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/