drop any double spaces since these will be displayed in the title-attribute
[squirrelmail.git] / functions / i18n.php
index 8c53b5900cdb9f7376a4c9bb4cee7cbdeed848dd..554ab4427c189a458c62a67b43618ae68b733f5c 100644 (file)
@@ -104,7 +104,7 @@ function sq_setlocale($category,$locale) {
  *
  * @param string $charset
  * @param string $string Text to be decoded
- * @param boolean $force_decode converts string to html without $charset!=$default_charset check. 
+ * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
  * Argument is available since 1.5.1 and 1.4.5.
  * @return string decoded string
  */
@@ -290,14 +290,42 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
     $SetupAlready = TRUE;
     sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE',  $accept_lang, SQ_SERVER);
 
-    if ($do_search && ! $sm_language && isset($accept_lang)) {
+    /**
+     * If function is asked to detect preferred language
+     *  OR squirrelmail default language is set to empty string
+     *    AND
+     * squirrelmail language ($sm_language) is empty string
+     * (not set in user's prefs and no cookie with language info)
+     *    AND
+     * browser provides list of preferred languages
+     *  THEN
+     * get preferred language from HTTP_ACCEPT_LANGUAGE header
+     */
+    if (($do_search || empty($squirrelmail_default_language)) &&
+        ! $sm_language &&
+        isset($accept_lang)) {
+        // TODO: use more than one language, if first language is not available
+        // FIXME: function assumes that string contains two or more characters.
+        // FIXME: some languages use 5 chars
         $sm_language = substr($accept_lang, 0, 2);
     }
 
-    if ((!$sm_language||$default) && isset($squirrelmail_default_language)) {
+    /**
+     * If language preference is not set OR script asks to use default language
+     *  AND
+     * default squirrelmail language is not set to empty string
+     *  THEN
+     * use default squirrelmail language value from configuration.
+     */
+    if ((!$sm_language||$default) &&
+        ! empty($squirrelmail_default_language)) {
         $squirrelmail_language = $squirrelmail_default_language;
         $sm_language = $squirrelmail_default_language;
     }
+
+    /** provide failsafe language when detection fails */
+    if (! $sm_language) $sm_language='en_US';
+
     $sm_notAlias = $sm_language;
 
     // Catching removed translation
@@ -413,26 +441,26 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
         } else {
             header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
         }
-
         /**
          * mbstring.func_overload fix (#929644).
          *
-         * php mbstring extension can replace standard string functions with their multibyte 
-         * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload.
+         * php mbstring extension can replace standard string functions with their multibyte
+         * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature 
+         * was added in php v.4.2.0
          *
          * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced
          * to use mbstring functions and mbstring internal encoding is set to multibyte charset,
-         * interface can't trust regular string functions. Due to mbstring overloading design 
+         * interface can't trust regular string functions. Due to mbstring overloading design
          * limits php scripts can't control this setting.
          *
          * This hack should fix some issues related to 8bit strings in passwords. Correct fix is
          * to disable mbstring overloading. Japanese translation uses different internal encoding.
          */
-        if ($squirrelmail_language != 'ja_JP' && 
+        if ($squirrelmail_language != 'ja_JP' &&
             function_exists('mb_internal_encoding') &&
             check_php_version(4,2,0) &&
             (int)ini_get('mbstring.func_overload')!=0) {
-            mb_internal_encoding('ASCII');
+            mb_internal_encoding('pass');
         }
     }
     return 0;
@@ -442,8 +470,8 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
  * Sets default_charset variable according to the one that is used by user's translations.
  *
  * Function changes global $default_charset variable in order to be sure, that it
- * contains charset used by user's translation. Sanity of $squirrelmail_default_language
- * and $default_charset combination provided in SquirrelMail config is also tested.
+ * contains charset used by user's translation. Sanity of $squirrelmail_language
+ * and $default_charset combination is also tested.
  *
  * There can be a $default_charset setting in the
  * config.php file, but the user may have a different language
@@ -454,11 +482,11 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
  * message blindly with the system-wide $default_charset.
  */
 function set_my_charset(){
-    global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
+    global $data_dir, $username, $default_charset, $languages, $squirrelmail_language;
 
     $my_language = getPref($data_dir, $username, 'language');
     if (!$my_language) {
-        $my_language = $squirrelmail_default_language ;
+        $my_language = $squirrelmail_language ;
     }
     // Catch removed translation
     if (!isset($languages[$my_language])) {
@@ -871,7 +899,7 @@ endswitch;
 
 global $squirrelmail_language, $languages, $use_gettext;
 
-if (! isset($squirrelmail_language)) {
+if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
     $squirrelmail_language = '';
 }
 
@@ -892,6 +920,7 @@ if (! isset($squirrelmail_language)) {
  *
  * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
  *
+ * @todo TODO: make language loading modular (similar to plugins, with locale/xx_XX/setup.php files)
  * @name $languages
  * @global array $languages
  */