Revision: 2240
          http://mrbs.svn.sourceforge.net/mrbs/?rev=2240&view=rev
Author:   cimorrison
Date:     2012-01-11 11:48:20 +0000 (Wed, 11 Jan 2012)
Log Message:
-----------
Fixed minor problem which caused the browser's language preferences to be 
interpreted slightly unexpectedly, even if possibly strictly correctly, when 
two or more languages had the same qualifier.    The languages' preference 
order expected by the user, from the list of languages in the browser options, 
would be reversed.   The problem could be demonstrated for example by setting 
the following order of preference in Firefox: en-gb, en, th, he, de, fr-fr, fr, 
ru, sv, pt, no, en-us, it, ko.   MRBS would put th before en - strictly 
speaking correct as they were both presented with a qualifier of 0.9, but not 
what the user would be expecting.

Modified Paths:
--------------
    mrbs/trunk/web/language.inc

Modified: mrbs/trunk/web/language.inc
===================================================================
--- mrbs/trunk/web/language.inc 2012-01-10 19:32:43 UTC (rev 2239)
+++ mrbs/trunk/web/language.inc 2012-01-11 11:48:20 UTC (rev 2240)
@@ -395,14 +395,14 @@
   {
     if (preg_match('/([a-zA-Z\-]+);q=([0-9\.]+)/', $specifier, $matches))
     {
-      $langs[$matches[1]] = $matches[2];
+      $langs[$matches[1]] = (float) $matches[2];
     }
     else if (preg_match("/([a-zA-Z\-]+)/", $specifier, $matches))
     {
       $langs[$matches[1]] = 1.0;
     }
   }
-  arsort($langs,SORT_NUMERIC);
+  langs_arsort($langs);
 }
 else // Else use the value from config.inc.php.
 {
@@ -1168,4 +1168,41 @@
   return $locale;
 }
 
+
+// Sort an associative array of the form language => qualifier, where qualifier
+// is a float, eg "en" => 0.6 in reverse order, ie in decreasing order of
+// preference.   The key feature of this function is that if two languages 
have the
+// same qualifier it preserves the browser's original preference order.
+//
+// Returns TRUE on success, FALSE on failure
+function langs_arsort(&$langs)
+{
+  // First of all build an array indexed by qualifier.    Each element will
+  // contain an array of languages with that qualifier (in the order presented
+  // by the browser, ie descending order of preference)
+  $qualifiers = array();
+  foreach ($langs as $lang => $qual)
+  {
+    $qual = (string) $qual; // We need a string for the array index
+    $qualifiers[$qual][] = $lang;
+  }
+  // Sort the $qualifiers array so that it is in descending order (but we're
+  // not sorting the language arays, so they'll still be in descending order)
+  if (krsort($qualifiers, SORT_NUMERIC) === FALSE)
+  {
+    return FALSE;
+  }
+  // Then reconstruct $langs
+  $langs = array();
+  foreach ($qualifiers as $qual => $languages)
+  {
+    foreach ($languages as $language)
+    {
+      $langs[$language] = (float) $qual;  // Turn the qualifier back to a float
+    }
+  }
+
+  return TRUE;
+}
+
 ?>

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual 
desktops for less than the cost of PCs and save 60% on VDI infrastructure 
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to