Update
[squirrelmail.git] / include / languages.php
index 97eeb095a30b69ca575e2100e28605b39c3c7af2..149cce1c2344fe963c8b33fe01c02da0e9ace130 100644 (file)
@@ -9,7 +9,12 @@
  * Internally the output character set is used. Other characters are
  * encoded using Unicode entities according to HTML 4.0.
  *
- * @copyright © 1999-2006 The SquirrelMail Project Team
+ * Before 1.5.2 functions were stored in functions/i18n.php. Script is moved
+ * because it executes some code in order to detect functions supported by
+ * existing PHP installation and implements fallback functions when required
+ * functions are not available. Scripts in functions/ directory should not
+ * setup anything when they are loaded.
+ * @copyright © 1999-2007 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
  */
 
 
+/**
+ * Wrapper for textdomain(), bindtextdomain() and
+ * bind_textdomain_codeset() primarily intended for
+ * plugins when changing into their own text domain 
+ * and back again.
+ *
+ * Note that if plugins using this function have 
+ * their translation files located in the SquirrelMail
+ * locale directory, the second argument is optional.
+ *
+ * @param string $domain_name The name of the text domain 
+ *                            (usually the plugin name, or 
+ *                            "squirrelmail") being switched to.                 
+ * @param string $directory   The directory that contains 
+ *                            all translations for the domain
+ *                            (OPTIONAL; default is SquirrelMail
+ *                            locale directory).
+ *
+ * @return string The name of the text domain that was set
+ *                *BEFORE* it is changed herein - NOTE that
+ *                this differs from PHP's textdomain() 
+ *
+ * @since 1.5.2 and 1.4.10 
+ */
+function sq_change_text_domain($domain_name, $directory='') {
+
+    static $domains_already_seen = array();
+    global $gettext_domain;
+    $return_value = $gettext_domain;
+
+    // empty domain defaults to "squirrelmail" 
+    // 
+    if (empty($domain_name)) $domain_name = 'squirrelmail';
+
+    // only need to call bindtextdomain() once 
+    //
+    if (in_array($domain_name, $domains_already_seen)) {
+        sq_textdomain($domain_name);
+        return $return_value;
+    }
+
+    $domains_already_seen[] = $domain_name;
+
+    if (empty($directory)) $directory = SM_PATH . 'locale/';
+
+    sq_bindtextdomain($domain_name, $directory);
+    sq_textdomain($domain_name);
+
+    return $return_value;
+}
+
 /**
  * Gettext bindtextdomain wrapper.
  *
  * Wrapper solves differences between php versions in order to provide
  * ngettext support. Should be used if translation uses ngettext
  * functions.
- * @since 1.5.1
+ *
+ * This also provides a bind_textdomain_codeset call to make sure the
+ * domain's encoding will not be overridden.
+ *
+ * @since 1.4.10 and 1.5.1
  * @param string $domain gettext domain name
- * @param string $dir directory that contains all translations
+ * @param string $dir directory that contains all translations (OPTIONAL;
+ *                    if not specified, defaults to SquirrelMail locale 
+ *                    directory)
  * @return string path to translation directory
  */
-function sq_bindtextdomain($domain,$dir) {
+function sq_bindtextdomain($domain,$dir='') {
     global $l10n, $gettext_flags, $sm_notAlias;
 
+    if (empty($dir)) $dir = SM_PATH . 'locale/';
+
     if ($gettext_flags==7) {
         // gettext extension without ngettext
         if (substr($dir, -1) != '/') $dir .= '/';
@@ -41,6 +105,19 @@ function sq_bindtextdomain($domain,$dir) {
 
     $dir=bindtextdomain($domain,$dir);
 
+    // set codeset in order to avoid gettext charset conversions
+    if (function_exists('bind_textdomain_codeset') 
+     && isset($languages[$sm_notAlias]['CHARSET'])) {
+
+        // Japanese translation uses different internal charset
+        if ($sm_notAlias == 'ja_JP') {
+            bind_textdomain_codeset ($domain_name, 'EUC-JP');
+        } else {
+            bind_textdomain_codeset ($domain_name, $languages[$sm_notAlias]['CHARSET']);
+        }
+
+    }
+
     return $dir;
 }
 
@@ -186,7 +263,7 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals
 
     $decode=fixcharset($charset);
     $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
-    if (file_exists($decodefile)) {
+    if ($decode != 'index' && file_exists($decodefile)) {
         include_once($decodefile);
         // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
         $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
@@ -202,14 +279,14 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals
  * @param string $string
  * @param string $charset
  * @param boolean $htmlencode keep htmlspecialchars encoding
- * @param string
+ * @return string
  */
 function charset_encode($string,$charset,$htmlencode=true) {
     global $default_charset;
 
     $encode=fixcharset($charset);
     $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
-    if (file_exists($encodefile)) {
+    if ($encode != 'index' && file_exists($encodefile)) {
         include_once($encodefile);
         $ret = call_user_func('charset_encode_'.$encode, $string);
     } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) {
@@ -263,10 +340,12 @@ function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
  * @return string $charset Adjusted name of charset
  */
 function fixcharset($charset) {
-    /* remove minus and characters that might be used in paths from charset
+
+    /* Remove minus and characters that might be used in paths from charset
      * name in order to be able to use it in function names and include calls.
+     * Also make sure it's in lower case (ala "UTF" --> "utf")
      */
-    $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
+    $charset=preg_replace("/[-:.\/\\\]/",'_', strtolower($charset));
 
     // OE ks_c_5601_1987 > cp949
     $charset=str_replace('ks_c_5601_1987','cp949',$charset);
@@ -301,15 +380,24 @@ function fixcharset($charset) {
  *  1 = mbstring support is not present,
  *  2 = mbstring support is not present, user's translation reverted to en_US.
  *
- * @param string $sm_language translation used by user's interface
- * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
- * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
+ * @param string $sm_language  Translation used by user's interface
+ * @param bool   $do_search    Use browser's preferred language detection functions.
+ *                             Defaults to false.
+ * @param bool   $default      Set $sm_language to $squirrelmail_default_language if
+ *                             language detection fails or language is not set.
+ *                             Defaults to false.
+ * @param string $content_type The content type being served currently (OPTIONAL;
+ *                             if not specified, defaults to whatever the template
+ *                             set that is in use has defined).
+ *
  * @return int function execution error codes.
+ *
  */
-function set_up_language($sm_language, $do_search = false, $default = false) {
+function set_up_language($sm_language, $do_search=false,
+                         $default=false, $content_type='') {
 
     static $SetupAlready = 0;
-    global $use_gettext, $languages,
+    global $use_gettext, $languages, $oTemplate,
            $squirrelmail_language, $squirrelmail_default_language, $default_charset,
            $sm_notAlias, $username, $data_dir;
 
@@ -320,6 +408,10 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
     $SetupAlready = TRUE;
     sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE',  $accept_lang, SQ_SERVER);
 
+    // grab content type if needed
+    //
+    if (empty($content_type)) $content_type = $oTemplate->get_content_type();
+
     /**
      * If function is asked to detect preferred language
      *  OR squirrelmail default language is set to empty string
@@ -376,16 +468,6 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
         sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
         sq_textdomain( 'squirrelmail' );
 
-        // set codeset in order to avoid gettext charset conversions
-        if (function_exists('bind_textdomain_codeset')) {
-            // Japanese translation uses different internal charset
-            if ($sm_notAlias == 'ja_JP') {
-                bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
-            } else {
-                bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
-            }
-        }
-
         // Use LOCALE key, if it is set.
         if (isset($languages[$sm_notAlias]['LOCALE'])){
             $longlocale=$languages[$sm_notAlias]['LOCALE'];
@@ -454,7 +536,7 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
 
         $squirrelmail_language = $sm_notAlias;
         if ($squirrelmail_language == 'ja_JP') {
-            header ('Content-Type: text/html; charset=EUC-JP');
+            $oTemplate->header ('Content-Type: ' . $content_type . '; charset=EUC-JP');
             if (!function_exists('mb_internal_encoding')) {
                 // Error messages can't be displayed here
                 $error = 1;
@@ -472,9 +554,9 @@ function set_up_language($sm_language, $do_search = false, $default = false) {
             mb_internal_encoding('EUC-JP');
             mb_http_output('pass');
         } elseif ($squirrelmail_language == 'en_US') {
-            header( 'Content-Type: text/html; charset=' . $default_charset );
+            $oTemplate->header( 'Content-Type: ' . $content_type . '; charset=' . $default_charset );
         } else {
-            header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
+            $oTemplate->header( 'Content-Type: ' . $content_type . '; charset=' . $languages[$sm_notAlias]['CHARSET'] );
         }
         /**
          * mbstring.func_overload fix (#929644).
@@ -997,7 +1079,7 @@ if (is_dir(SM_PATH . 'locale') &&
             $lang_dir = substr($lang_dir,0,-1);
         }
         if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
-            is_dir(SM_PATH.'locale/'.$lang_dir) &&
+            $lang_dir != '.svn' && is_dir(SM_PATH.'locale/'.$lang_dir) &&
             file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
             include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
         }
@@ -1098,4 +1180,3 @@ elseif ($gettext_flags == 0) {
         }
     }
 }
-?>
\ No newline at end of file