Revision: 2561
          https://sourceforge.net/p/mrbs/code/2561/
Author:   cimorrison
Date:     2012-11-06 19:06:15 +0000 (Tue, 06 Nov 2012)
Log Message:
-----------
Eliminated need to use $langs as a global

Modified Paths:
--------------
    mrbs/trunk/web/Themes/default/header.inc
    mrbs/trunk/web/help.php
    mrbs/trunk/web/js/datepicker.js.php
    mrbs/trunk/web/language.inc

Modified: mrbs/trunk/web/Themes/default/header.inc
===================================================================
--- mrbs/trunk/web/Themes/default/header.inc    2012-11-05 17:25:23 UTC (rev 
2560)
+++ mrbs/trunk/web/Themes/default/header.inc    2012-11-06 19:06:15 UTC (rev 
2561)
@@ -13,7 +13,6 @@
   global $view_week_number, $weekstarts, $times_along_top, $periods, 
$enable_periods;
   global $auth, $max_level;
   global $default_language_tokens, $disable_automatic_language_changing, 
$override_locale;
-  global $lang_map_windows, $langs, $server_os;
   global $select_options;
   global $ajax_refresh_rate;
   global $main_table_cell_border_width, $main_cell_height;

Modified: mrbs/trunk/web/help.php
===================================================================
--- mrbs/trunk/web/help.php     2012-11-05 17:25:23 UTC (rev 2560)
+++ mrbs/trunk/web/help.php     2012-11-06 19:06:15 UTC (rev 2561)
@@ -23,7 +23,7 @@
 
 echo "<p>\n" . get_vocab("browserlang") .":\n";
 
-echo implode(", ", array_keys($langs));
+echo implode(", ", array_keys(get_language_qualifiers()));
 
 echo "\n</p>\n";
 

Modified: mrbs/trunk/web/js/datepicker.js.php
===================================================================
--- mrbs/trunk/web/js/datepicker.js.php 2012-11-05 17:25:23 UTC (rev 2560)
+++ mrbs/trunk/web/js/datepicker.js.php 2012-11-06 19:06:15 UTC (rev 2561)
@@ -12,8 +12,6 @@
   echo "'use strict';\n";
 }
 
-global $lang_aliases;
-
 // Set the default values for datepicker, including the default regional 
setting
 ?>
 $(function() {
@@ -39,14 +37,11 @@
   $default_lang = locale_format($default_language_tokens, '-');
   
   echo "$.datepicker.setDefaults($.datepicker.regional['$default_lang']);\n";
-  $datepicker_langs = $langs;
+  $datepicker_langs = get_language_qualifiers();
+  $datepicker_langs = alias_qualifiers($datepicker_langs);
   asort($datepicker_langs, SORT_NUMERIC);
   foreach ($datepicker_langs as $lang => $qual)
   {
-    if (isset($lang_aliases[strtolower($lang)]))
-    {
-      $lang = $lang_aliases[strtolower($lang)];
-    }
     // Get the locale in the format that datepicker likes: language lower case
     // and country upper case (xx-XX)
     $datepicker_locale = locale_format($lang, '-');

Modified: mrbs/trunk/web/language.inc
===================================================================
--- mrbs/trunk/web/language.inc 2012-11-05 17:25:23 UTC (rev 2560)
+++ mrbs/trunk/web/language.inc 2012-11-06 19:06:15 UTC (rev 2561)
@@ -421,36 +421,14 @@
 // environment variable.
 $cli_mode = is_cli();
 
-// If we're running from the CLI, use the config setting
-if ($cli_mode && !empty($cli_language))
-{
-  $langs[$cli_language] = 1.0;
-}
-// Otherwise we enumerate the user's language preferences...
-elseif (isset($HTTP_ACCEPT_LANGUAGE)) // Attempt to use $HTTP_ACCEPT_LANGUAGE 
only when defined.
-{
-  $lang_specifiers = explode(',',$HTTP_ACCEPT_LANGUAGE);
-  foreach ($lang_specifiers as $specifier)
-  {
-    if (preg_match('/([a-zA-Z\-]+);q=([0-9\.]+)/', $specifier, $matches))
-    {
-      $langs[$matches[1]] = (float) $matches[2];
-    }
-    else if (preg_match("/([a-zA-Z\-]+)/", $specifier, $matches))
-    {
-      $langs[$matches[1]] = 1.0;
-    }
-  }
-  langs_arsort($langs);
-}
-else // Else use the value from config.inc.php.
-{
-  $langs[$default_language_tokens] = 1.0;
-}
+// Get the acceptable languages, convert to aliases where applicable and sort
+// them in reverse order so that the most acceptable one is at the start
+$langs = get_language_qualifiers();
+$langs = alias_qualifiers($langs);
+langs_arsort($langs);
 
 // The following attempts to import a language based on what the client
 // is using.
-
 if (!$disable_automatic_language_changing || $cli_mode)
 {
   $doneit = 0;
@@ -467,10 +445,6 @@
       $locale = $lang;
     }
 
-    if (isset($lang_aliases[strtolower($lang)]))
-    {
-      $lang = $locale = $lang_aliases[strtolower($lang)];
-    }
     if (set_vocab($lang))
     {
       $locale = $lang;
@@ -638,7 +612,7 @@
 }
 
 
-// Sets the locale according to the MRBS config seetings and the user's
+// Sets the locale according to the MRBS config settings and the user's
 // browser preferences
 function set_mrbs_locale()
 {
@@ -671,6 +645,69 @@
 }
 
 
+// Returns an array of language qualifiers with the language key replaced
+// by its alias, if one exists
+function alias_qualifiers($qualifiers)
+{
+  global $lang_aliases;
+  
+  $result = array();
+  foreach ($qualifiers as $lang => $qualifier)
+  {
+    if (isset($lang_aliases[strtolower($lang)]))
+    {
+      $result[$lang_aliases[strtolower($lang)]] = $qualifier;
+    }
+    else
+    {
+      $result[$lang] = $qualifier;
+    }
+  }
+  return $result;
+}
+
+
+// Returns an unsorted associative array of acceptable language qualifiers, 
indexed
+// by language.   The language will have been replaced its alias if one exists.
+function get_language_qualifiers()
+{
+  global $cli_mode, $cli_language, $default_language_tokens, 
$HTTP_ACCEPT_LANGUAGE;
+  
+  static $qualifiers = array();
+  
+  if (count($qualifiers) == 0)
+  {
+    // If we're running from the CLI, use the config setting
+    if ($cli_mode && !empty($cli_language))
+    {
+      $qualifiers[$cli_language] = 1.0;
+    }
+    // Otherwise we enumerate the user's language preferences...
+    elseif (isset($HTTP_ACCEPT_LANGUAGE)) // Attempt to use 
$HTTP_ACCEPT_LANGUAGE only when defined.
+    {
+      $lang_specifiers = explode(',',$HTTP_ACCEPT_LANGUAGE);
+      foreach ($lang_specifiers as $specifier)
+      {
+        if (preg_match('/([a-zA-Z\-]+);q=([0-9\.]+)/', $specifier, $matches))
+        {
+          $qualifiers[$matches[1]] = (float) $matches[2];
+        }
+        else if (preg_match("/([a-zA-Z\-]+)/", $specifier, $matches))
+        {
+          $qualifiers[$matches[1]] = 1.0;
+        }
+      }
+    }
+    else // Else use the value from config.inc.php.
+    {
+      $qualifiers[$default_language_tokens] = 1.0;
+    }
+  }
+  
+  return $qualifiers;
+}
+
+
 // Returns the pathname of the language file to use for the dataTables
 // jQuery plugin.    If no suitable language file exists then returns FALSE
 function get_datatable_lang()
------------------------------------------------------------------------------
LogMeIn Central: Instant, anywhere, Remote PC access and management.
Stay in control, update software, and manage PCs from one command center
Diagnose problems and improve visibility into emerging IT issues
Automate, monitor and manage. Do more in less time with Central
http://p.sf.net/sfu/logmein12331_d2d
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to