Patch for language preferencies from browser by Markus Bertheau, edited by me. Please...
authorsoranzo <soranzo>
Fri, 18 Jun 2004 14:47:21 +0000 (14:47 +0000)
committersoranzo <soranzo>
Fri, 18 Jun 2004 14:47:21 +0000 (14:47 +0000)
HISTORY
libraries/errorhandler.inc.php
libraries/lib.inc.php
login.php

diff --git a/HISTORY b/HISTORY
index d02ca8cc063d4a6d8ec97e3fcb9fda5f44bce47a..4a875f63d3d0f6b59964978ced10817113f6cdc1 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -6,6 +6,7 @@ Version 3.5-dev
 
 Features
 * Context-sensitive online help system
+* Use language preferencies from browser (Markus Bertheau, Nicola Soranzo)
 
 Translations
 * Arabic from Zaki
index f8a93feb2b53e8fd48abf5d9c4e293e50c23aeba..0a8b1054a20299ea8f3b2be12d98bc6db7977133 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Overrides default ADODB error handler to provide nicer error handling.
  *
- * $Id: errorhandler.inc.php,v 1.14 2003/12/17 09:11:32 chriskl Exp $
+ * $Id: errorhandler.inc.php,v 1.15 2004/06/18 14:47:21 soranzo Exp $
  */
 
 define('ADODB_ERROR_HANDLER','Error_Handler');
@@ -20,7 +20,7 @@ define('ADODB_ERROR_HANDLER','Error_Handler');
  */
 function Error_Handler($dbms, $fn, $errno, $errmsg, $p1=false, $p2=false)
 {
-       global $lang;
+       global $lang, $conf;
        global $misc, $appName, $appVersion, $appLangFiles;
 
        switch($fn) {
index 0767a22f5e94b26fccc9a3e81b47f99e7fd84d4c..96dedf8b2ee41406b1e280baa0e37eeec09a5337 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * Function library read in upon startup
         *
-        * $Id: lib.inc.php,v 1.80 2004/06/13 07:08:29 chriskl Exp $
+        * $Id: lib.inc.php,v 1.81 2004/06/18 14:47:21 soranzo Exp $
         */
        
        // Set error reporting level to max
@@ -25,7 +25,7 @@
                include('./conf/config.inc.php');
        }
        else {
-               echo "Configuration error: Copy conf/config.inc.php-dist to conf/config.inc.php and edit appropriately.";
+               echo 'Configuration error: Copy conf/config.inc.php-dist to conf/config.inc.php and edit appropriately.';
                exit;
        }
 
                'turkish' => 'T&uuml;rk&ccedil;e'
        );
 
-       // Language settings.  Always include english.php, since it's the master
-       // language file, and then overwrite it with the user-specified language if
-       // one has not been selected yet.
+       // Always include english.php, since it's the master language file
        if (!isset($conf['default_lang'])) $conf['default_lang'] = 'english';
        $lang = array();
-       include_once('./lang/recoded/english.php');
-       // Include default language over the top - we really should try to avoid this
-       // in the case when the user has chosen a language.
-       include_once("./lang/recoded/" . strtolower($conf['default_lang']) . ".php");
-
-       // Check for config file version mismatch
-       if (!isset($conf['version']) || $conf['base_version'] > $conf['version']) {
-               echo $lang['strbadconfig'];
-               exit;
-       }
+       require_once('./lang/recoded/english.php');
 
        // Create Misc class references
-       include_once('./classes/Misc.php');
+       require_once('./classes/Misc.php');
        $misc = new Misc();
 
        // Start session (if not auto-started)
                exit;
        }
 
+       // Import language file
+       include('./lang/recoded/' . strtolower($_SESSION['webdbLanguage']) . '.php');
+
        // If extra login check fails, back to the login screen
        $_allowed = $misc->checkExtraSecurity();
        if (!$_allowed) {
                exit;
        }
 
-       // Import language file
-       include("./lang/recoded/" . strtolower($_SESSION['webdbLanguage']) . ".php");
-
        // Check database support is properly compiled in
        if (!function_exists('pg_connect')) {
                echo $lang['strnotloaded'];
index 597d5204cc42880b24d3c3b9c5be11f7eb32b151..e06627b638bca814b0f765f61fed2671c507dfef 100755 (executable)
--- a/login.php
+++ b/login.php
@@ -3,27 +3,85 @@
        /**
         * Login screen
         *
-        * $Id: login.php,v 1.16 2004/05/26 11:27:00 soranzo Exp $
+        * $Id: login.php,v 1.17 2004/06/18 14:47:21 soranzo Exp $
         */
 
        // This needs to be an include once to prevent lib.inc.php infinite recursive includes.
        // Check to see if the configuration file exists, if not, explain
-       include_once('./libraries/lib.inc.php');
+       require_once('./libraries/lib.inc.php');
 
-       // Unfortunately, since sometimes lib.inc.php has been included, but we still
-       // need the config variables
-       if (file_exists('conf/config.inc.php')) {
-               require('conf/config.inc.php');
+       // Prepare form variables
+       if (!isset($_POST['formServer'])) $_POST['formServer'] = '';
+       if (!isset($_POST['formLanguage'])) {
+               // Parse the user acceptable language in HTTP_ACCEPT_LANGUAGE
+               // ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 )
+               // If there's one available, then overwrite the default language.
+               if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+                       $userLanguage = '';
+                       $userLanguages = array();
+                       $acceptableLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+                       foreach ($acceptableLanguages as $accLang) {
+                               $languageInfos = explode(';', trim($accLang));
+                               $languageRange = strtolower($languageInfos[0]);
+                               if (isset($languageInfos[1]) && substr($languageInfos[1], 0, 2) == 'q=')
+                                       $languageQuality = (float)substr($languageInfos[1], 2, 5);
+                               else
+                                       $languageQuality = 1;
+                               // If the language is already in the array, check that we
+                               // don't overwrite its quality value with a lower one
+                               if ((!array_key_exists($languageRange, $userLanguages))
+                                       || ($userLanguages[$languageRange] < $languageQuality))
+                                       $userLanguages[$languageRange] = $languageQuality;
+                       }
+                       arsort($userLanguages, SORT_NUMERIC);
+
+                       // if it's available 'language-country', but not general 'language' translation
+                       // (eg. 'portuguese-br', but not 'portuguese')
+                       // specify both 'la' => 'language-country' and 'la-co' => 'language-country'.
+                       // See http://www.w3.org/WAI/ER/IG/ert/iso639.htm for language codes
+                       $availableLanguages = array(
+                               'af' => 'afrikaans',
+                               'zh' => 'chinese-tr',
+                               'zh-cn' => 'chinese-sim',
+                               'cs' => 'czech',
+                               'nl' => 'dutch',
+                               'en' => 'english',
+                               'fr' => 'french',
+                               'de' => 'german',
+                               'hu' => 'hungarian',
+                               'it' => 'italian',
+                               'ja' => 'japanese',
+                               'pl' => 'polish',
+                               'pt' => 'portuguese-br',
+                               'pt-br' => 'portuguese-br',
+                               'ru' => 'russian',
+                               'sk' => 'slovak',
+                               'sv' => 'swedish',
+                               'es' => 'spanish',
+                               'sv' => 'swedish',
+                               'tr' => 'turkish'
+                       );
+
+                       reset($userLanguages);
+                       do {
+                               $languageRange = key($userLanguages);
+                               if (array_key_exists($languageRange, $availableLanguages)) {
+                                       $userLanguage = $availableLanguages[$languageRange];
+                               }
+                       } while ($userLanguage == '' && next($userLanguages));
+                       if ($userLanguage != '') $conf['default_lang'] = $userLanguage;
+               }
+               $_POST['formLanguage'] = $conf['default_lang'];
+               // Include default language over english.
+               include_once('./lang/recoded/' . strtolower($conf['default_lang']) . '.php');
        }
-       else {
-               echo "Configuration Error: You must rename/copy config.inc.php-dist to config.inc.php and set your appropriate settings";
+
+       // Check for config file version mismatch
+       if (!isset($conf['version']) || $conf['base_version'] > $conf['version']) {
+               echo $lang['strbadconfig'];
                exit;
        }
 
-       // Prepare form variables
-       if (!isset($_POST['formServer'])) $_POST['formServer'] = '';
-       if (!isset($_POST['formLanguage'])) $_POST['formLanguage'] = $conf['default_lang'];
-
        // Force encoding to UTF-8
        $lang['appcharset'] = 'UTF-8';