Revision: 1420
          http://mrbs.svn.sourceforge.net/mrbs/?rev=1420&view=rev
Author:   cimorrison
Date:     2010-08-25 10:44:59 +0000 (Wed, 25 Aug 2010)

Log Message:
-----------
Made datepicker respect $override_locale if possible

Modified Paths:
--------------
    mrbs/trunk/web/Themes/default/header.inc
    mrbs/trunk/web/language.inc

Modified: mrbs/trunk/web/Themes/default/header.inc
===================================================================
--- mrbs/trunk/web/Themes/default/header.inc    2010-08-24 13:56:16 UTC (rev 
1419)
+++ mrbs/trunk/web/Themes/default/header.inc    2010-08-25 10:44:59 UTC (rev 
1420)
@@ -9,7 +9,8 @@
          $search_str, $locale_warning, $provisional_enabled;
   global $tbl_entry, $tbl_room, $tbl_area;
   global $PHP_SELF, $view_week_number, $weekstarts;
-  global $langs, $disable_automatic_language_changing, 
$default_language_tokens;
+  global $default_language_tokens, $disable_automatic_language_changing, 
$override_locale;
+  global $lang_map_windows, $langs;
   
   $page = basename($PHP_SELF, ".php" );
 
@@ -67,15 +68,20 @@
 ?>
 $(function() {
   <?php
-  // We set the regional setting, by
-  // - setting the MRBS default language
-  // - then setting the locales in increasing order of browser preference
-  // - then setting the MRBS default language setting again if automatic 
language
-  //   changing is disabled
-  // This algorithm is designed to ensure that datepicker is set to the most 
preferred
-  // browser setting for which a datepicker localisation exists.   If the MRBS 
default 
-  // language setting doesn't exist, then at least we get one the user prefers 
according
-  // to the browser settings.
+  // We set the regional setting by setting locales in reverse order of 
priority.
+  // If you try and set a datepicker locale that doesn't exist, then nothing is
+  // changed and the regional setting stays as it was before.   The reverse 
order
+  // of priority is:
+  // - the MRBS default language
+  // - locales taken from the browser in increasing order of browser preference
+  // - then, if automatic language changing is disabled, 
+  //      - the MRBS default language setting again
+  //      - the language part of the override_locale
+  //      - the full override_locale
+  // This algorithm is designed to ensure that datepicker is set to the closest
+  // available locale to that specified in the config file.   If automatic 
language
+  // changing is disabled, we fall back to a browser specified locale if the 
locale
+  // in the config file is not available in datepicker.
   
   // Some special cases
   $default_lang = strtolower($default_language_tokens);
@@ -95,19 +101,39 @@
   foreach ($datepicker_langs as $lang => $qual)
   {
     // Get the locale in the format that datepicker likes: language lower case
-    // and country upper case
-    $components = explode('-', $lang);
-    $components[0] = strtolower($components[0]);
-    if (count($components) > 1)
-    {
-      $components[1] = strtoupper($components[1]);
-    }
-    $lang = implode('-', $components);
-    echo "$.datepicker.setDefaults($.datepicker.regional['$lang']);\n";
+    // and country upper case (xx-XX)
+    $datepicker_locale = locale_format($lang, '-');
+    echo 
"$.datepicker.setDefaults($.datepicker.regional['$datepicker_locale']);\n";
   }
   if ($disable_automatic_language_changing)
   {
+    // They don't want us to use the browser language, so we'll set the 
datepicker
+    // locale setting back to the default language (as a fall-back) and then 
we'll
+    // try and set it to the override_locale
     echo "$.datepicker.setDefaults($.datepicker.regional['$default_lang']);\n";
+    if (!empty($override_locale))
+    {
+      $server_os = get_server_os();
+      if ($server_os == 'windows')
+      {
+        // If the server is running on Windows we'll have to try and translate 
the 
+        // Windows style locale back into an xx-YY locale
+        $datepicker_locale = array_search($override_locale, $lang_map_windows);
+      }
+      else
+      {
+        $datepicker_locale = $override_locale;
+      }
+      if (!empty($datepicker_locale))  // in case the array_search() returned 
FALSE
+      {
+        $datepicker_locale = locale_format($datepicker_locale, '-');
+        $datepicker_lang = substr($datepicker_locale, 0, 2);
+        // First we'll try and get the correct language and then we'll try and
+        // overwrite that with the correct country variant
+        echo 
"$.datepicker.setDefaults($.datepicker.regional['$datepicker_lang']);\n";
+        echo 
"$.datepicker.setDefaults($.datepicker.regional['$datepicker_locale']);\n";
+      }
+    }
   }
   ?>
   $.datepicker.setDefaults({

Modified: mrbs/trunk/web/language.inc
===================================================================
--- mrbs/trunk/web/language.inc 2010-08-24 13:56:16 UTC (rev 1419)
+++ mrbs/trunk/web/language.inc 2010-08-25 10:44:59 UTC (rev 1420)
@@ -5,6 +5,11 @@
 // A map is needed to convert from the HTTP language specifier to a
 // locale specifier for Windows
 //
+// The ordering of this array is important as it is also used to map in the
+// reverse direction, ie to convert a Windows style locale into an xx-yy style
+// locale by finding the first occurence of a value and then using the
+// corresponding key.
+//
 // These locale TLAs found at:
 // http://www.microsoft.com/globaldev/reference/winxp/langtla.mspx
 $lang_map_windows = array
@@ -328,6 +333,7 @@
 );
 
 
+
 ////////////////////////////////////////////////////////////////////////
 // Language token handling
 
@@ -524,7 +530,7 @@
     {
       // Convert locale=xx-xX or xx_Xx or xx_XxXx (etc.) to xx_XX[XX]; this is 
highly
       // dependent on the machine's installed locales
-      $locale = 
strtolower(substr($locale,0,2))."_".strtoupper(substr($locale,3));
+      $locale = locale_format($locale, '_');
     }
     if ($unicode_encoding)
     {
@@ -796,4 +802,21 @@
   }
 }
 
+// Format a locale which could be xx-xX or xx_Xx or xx_XxXx (etc.) into a
+// standardised format consiting of a lower case language followed, if 
applicable,
+// by an upper case country, separated by $separator.    Typically the 
separator
+// will be '-' or '_'.
+function locale_format($locale, $separator)
+{
+  if (strlen($locale) == 2)
+  {
+    $locale = strtolower($locale);
+  }
+  else
+  {
+    $locale = strtolower(substr($locale,0,2)) . $separator . 
strtoupper(substr($locale,3));
+  }
+  return $locale;
+}
+
 ?>


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

------------------------------------------------------------------------------
Sell apps to millions through the Intel(R) Atom(Tm) Developer Program
Be part of this innovative community and reach millions of netbook users 
worldwide. Take advantage of special opportunities to increase revenue and 
speed time-to-market. Join now, and jumpstart your future.
http://p.sf.net/sfu/intel-atom-d2d
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to