Getting ready for 1.4.0 RC1
[squirrelmail.git] / functions / i18n.php
index 82242dd3a543d5a2b7505d7fb9d378dc1a5ae540..edd76043b215c8f24579631c10ed2a20d7c75dff 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * i18n.php
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This file contains variuos functions that are needed to do
 
 /* Decodes a string to the internal encoding from the given charset */
 function charset_decode ($charset, $string) {
+    global $languages, $squirrelmail_language;
+
+    if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
+        function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
+        $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
+    }
 
     /* All HTML special characters are 7 bit and can be replaced first */
     $string = htmlspecialchars ($string);
@@ -28,8 +34,12 @@ function charset_decode ($charset, $string) {
             $ret = charset_decode_iso_8859_1 ($string);
         } else if ($res[1] == '2') {
             $ret = charset_decode_iso_8859_2 ($string);
+        } else if ($res[1] == '4') {
+            $ret = charset_decode_iso_8859_4 ($string);
         } else if ($res[1] == '7') {
             $ret = charset_decode_iso_8859_7 ($string);
+        } else if ($res[1] == '13') {
+            $ret = charset_decode_iso_8859_13 ($string);
         } else if ($res[1] == '15') {
             $ret = charset_decode_iso_8859_15 ($string);
         } else {
@@ -40,7 +50,9 @@ function charset_decode ($charset, $string) {
     } else if ($charset == 'koi8-r') {
         $ret = charset_decode_koi8r ($string);
     } else if ($charset == 'windows-1251') {
-        $ret = charset_decode_koi8r ($string);
+        $ret = charset_decode_windows_1251 ($string);
+    } else if ($charset == 'windows-1257') {
+        $ret = charset_decode_windows_1257 ($string);
     } else {
         $ret = $string;
     }
@@ -397,6 +409,127 @@ function charset_decode_iso_8859_2 ($string) {
     return $string;
 }
 
+/* 
+ ISO/IEC 8859-4:1998 Latin Alphabet No. 4
+*/
+
+function charset_decode_iso_8859_4 ($string) {
+    global $default_charset, $languages, $sm_notAlias;
+
+    if (strtolower($default_charset) == 'iso-8859-4')
+        return $string;
+    if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'iso-8859-4')
+        return $string;
+
+    /* Only do the slow convert if there are 8-bit characters */
+    if (! ereg("[\200-\377]", $string))
+        return $string;
+
+    // latin capital letter a with ogonek 161 -> 260
+    $string = str_replace ("\241", 'Ą', $string);
+    // latin small letter kra 162 -> 312
+    $string = str_replace ("\242", 'ĸ', $string);
+    // latin capital letter r with cedilla 163 -> 342
+    $string = str_replace ("\243", 'Ŗ', $string);
+    // latin capital letter i with tilde 165 -> 296
+    $string = str_replace ("\245", 'Ĩ', $string);
+    // latin capital letter l with cedilla 166 -> 315
+    $string = str_replace ("\246", 'Ļ', $string);
+    // latin capital letter s with caron 169 -> 352
+    $string = str_replace ("\251", 'Š', $string);
+    // latin capital letter e with macron 170 -> 274
+    $string = str_replace ("\252", 'Ē', $string);
+    // latin capital letter g with cedilla 171 -> 290
+    $string = str_replace ("\253", 'Ģ', $string);
+    // latin capital letter t with stroke 172 -> 358
+    $string = str_replace ("\254", 'Ŧ', $string);
+    // latin capital letter z with caron 174 -> 381
+    $string = str_replace ("\256", 'Ž', $string);
+    // latin small letter a with ogonek 177 -> 261
+    $string = str_replace ("\261", 'ą', $string);
+    // ogonek 178 -> 731
+    $string = str_replace ("\262", '˛', $string);
+    // latin small letter r with cedilla 179 -> 343
+    $string = str_replace ("\263", 'ŗ', $string);
+    // latin small letter i with tilde 181 -> 297
+    $string = str_replace ("\265", 'ĩ', $string);
+    // latin small letter l with cedilla 182 -> 316
+    $string = str_replace ("\266", 'ļ', $string);
+    // caron 183 -> 711 
+    $string = str_replace ("\267", 'ˇ', $string);
+    // latin small letter s with caron 185 -> 353
+    $string = str_replace ("\271", 'š', $string);
+    // latin small letter e with macron 186 -> 275
+    $string = str_replace ("\272", 'ē', $string);
+    // latin small letter g with cedilla 187 -> 291
+    $string = str_replace ("\273", 'ģ', $string);
+    // latin small letter t with stroke 188 -> 359
+    $string = str_replace ("\274", 'ŧ', $string);
+    // latin capital letter eng 189 -> 330
+    $string = str_replace ("\275", 'Ŋ', $string);
+    // latin small letter z with caron 190 -> 382
+    $string = str_replace ("\276", 'ž', $string);
+    // latin small letter eng 191 -> 331
+    $string = str_replace ("\277", 'ŋ', $string);
+    // latin capital letter a with macron 192 -> 256
+    $string = str_replace ("\300", 'Ā', $string);
+    // latin capital letter i with ogonek 199 -> 302
+    $string = str_replace ("\307", 'Į', $string);
+    // latin capital letter c with caron 200 -> 268
+    $string = str_replace ("\310", 'Č', $string);
+    // latin capital letter e with ogonek 202 -> 280
+    $string = str_replace ("\312", 'Ę', $string);
+    // latin capital letter e with dot above 204 -> 278
+    $string = str_replace ("\314", 'Ė', $string);
+    // latin capital letter i with macron 207 -> 298
+    $string = str_replace ("\317", 'Ī', $string);
+    // latin capital letter d with stroke 208 -> 272
+    $string = str_replace ("\320", 'Đ', $string);
+    // latin capital letter n with cedilla 209 -> 325
+    $string = str_replace ("\321", 'Ņ', $string);
+    // latin capital letter o with macron 210 -> 332
+    $string = str_replace ("\322", 'Ō', $string);
+    // latin capital letter k with cedilla 211 -> 310
+    $string = str_replace ("\323", 'Ķ', $string);
+    // latin capital letter u with ogonek 217 -> 370
+    $string = str_replace ("\331", 'Ų', $string);
+    // latin capital letter u with tilde 221 -> 360
+    $string = str_replace ("\335", 'Ũ', $string);
+    // latin capital letter u with macron 222 -> 362
+    $string = str_replace ("\336", 'Ū', $string);
+    // latin small letter a with macron 224 -> 257
+    $string = str_replace ("\340", 'ā', $string);
+    // latin small letter i with ogonek 231 -> 303
+    $string = str_replace ("\347", 'į', $string);
+    // latin small letter c with caron 232 -> 269
+    $string = str_replace ("\350", 'č', $string);
+    // latin small letter e with ogonek 234 -> 281
+    $string = str_replace ("\352", 'ę', $string);
+    // latin small letter e with dot above 236 -> 279
+    $string = str_replace ("\354", 'ė', $string);
+    // latin small letter i with macron 239 -> 299
+    $string = str_replace ("\357", 'ī', $string);
+    // latin small letter d with stroke 240 -> 273
+    $string = str_replace ("\360", 'đ', $string);
+    // latin small letter n with cedilla 241 -> 326
+    $string = str_replace ("\361", 'ņ', $string);
+    // latin small letter o with macron 242 -> 333
+    $string = str_replace ("\362", 'ō', $string);
+    // latin small letter k with cedilla 243 -> 311
+    $string = str_replace ("\363", 'ķ', $string);
+    // latin small letter u with ogonek 249 -> 371
+    $string = str_replace ("\371", 'ų', $string);
+    // latin small letter u with tilde 253 -> 361
+    $string = str_replace ("\375", 'ũ', $string);
+    // latin small letter u with macron 254 -> 363
+    $string = str_replace ("\376", 'ū', $string);
+    // dot above 255 -> 729
+    $string = str_replace ("\377", '˙', $string);
+
+    // rest of charset is the same as ISO-8859-1
+    return (charset_decode_iso_8859_1($string));
+}
+
 /* iso-8859-7 is Greek. */
 function charset_decode_iso_8859_7 ($string) {
     global $default_charset;
@@ -435,7 +568,7 @@ function charset_decode_iso_8859_7 ($string) {
      * ISO-8859-7 characters from 11/04 (0xB4) to 11/06 (0xB6)
      * These are Unicode 900-902
      */
-    $string = preg_replace("/([\264-\266])/","'&#' . (ord(\\1)+720)",$string);
+    $string = preg_replace("/([\264-\266])/e","'&#' . (ord('\\1')+720);",$string);
     
     /* 11/07 (0xB7) Middle dot is the same in iso-8859-1 */
     $string = str_replace("\267", '·', $string);
@@ -444,7 +577,7 @@ function charset_decode_iso_8859_7 ($string) {
      * ISO-8859-7 characters from 11/08 (0xB8) to 11/10 (0xBA)
      * These are Unicode 900-902
      */
-    $string = preg_replace("/([\270-\272])/","'&#' . (ord(\\1)+720)",$string);
+    $string = preg_replace("/([\270-\272])/e","'&#' . (ord('\\1')+720);",$string);
 
     /*
      * 11/11 (0xBB) Right angle quotation mark is the same as in
@@ -453,11 +586,143 @@ function charset_decode_iso_8859_7 ($string) {
     $string = str_replace("\273", '»', $string);
 
     /* And now the rest of the charset */
-    $string = preg_replace("/([\274-\376])/","'&#' . (ord(\\1)+720)",$string);
+    $string = preg_replace("/([\274-\376])/e","'&#'.(ord('\\1')+720);",$string);
 
     return $string;
 }
 
+/*
+ ISO/IEC 8859-13:1998 Latin Alphabet No. 7 (Baltic Rim) 
+*/
+function charset_decode_iso_8859_13 ($string) {
+    global $default_charset, $languages, $sm_notAlias;
+
+    if (strtolower($default_charset) == 'iso-8859-13')
+        return $string;
+    if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'iso-8859-13')
+        return $string;
+
+    /* Only do the slow convert if there are 8-bit characters */
+    if (! ereg("[\200-\377]", $string))
+        return $string;
+
+    // right double quotation mark 161 -> 8221
+    $string = str_replace ("\241", '”', $string);
+    // double low-9 quotation mark 165 -> 8222
+    $string = str_replace ("\245", '„', $string);
+    // latin capital letter o with stroke 168 -> 216
+    $string = str_replace ("\250", 'Ø', $string);
+    // latin capital letter r with cedilla 170 -> 342
+    $string = str_replace ("\252", 'Ŗ', $string);
+    // latin capital letter ae 175 -> 198
+    $string = str_replace ("\257", 'Æ', $string);
+    // left double quotation mark 180 -> 8220
+    $string = str_replace ("\264", '“', $string);
+    // latin small letter o with stroke 184 -> 248
+    $string = str_replace ("\270", 'ø', $string);
+    // latin small letter r with cedilla 186 -> 343
+    $string = str_replace ("\272", 'ŗ', $string);
+    // latin small letter ae 191 -> 230
+    $string = str_replace ("\277", 'æ', $string);
+    // latin capital letter a with ogonek 192 -> 260
+    $string = str_replace ("\300", 'Ą', $string);
+    // latin capital letter i with ogonek 193 -> 302
+    $string = str_replace ("\301", 'Į', $string);
+    // latin capital letter a with macron 194 -> 256
+    $string = str_replace ("\302", 'Ā', $string);
+    // latin capital letter c with acute 195 -> 262
+    $string = str_replace ("\303", 'Ć', $string);
+    // latin capital letter e with ogonek 198 -> 280
+    $string = str_replace ("\306", 'Ę', $string);
+    // latin capital letter e with macron 199 -> 274
+    $string = str_replace ("\307", 'Ē', $string);
+    // latin capital letter c with caron 200 -> 268
+    $string = str_replace ("\310", 'Č', $string);
+    // latin capital letter z with acute 202 -> 377
+    $string = str_replace ("\312", 'Ź', $string);
+    // latin capital letter e with dot above 203 -> 278
+    $string = str_replace ("\313", 'Ė', $string);
+    // latin capital letter g with cedilla 204 -> 290
+    $string = str_replace ("\314", 'Ģ', $string);
+    // latin capital letter k with cedilla 205 -> 310
+    $string = str_replace ("\315", 'Ķ', $string);
+    // latin capital letter i with macron 206 -> 298
+    $string = str_replace ("\316", 'Ī', $string);
+    // latin capital letter l with cedilla 207 -> 315
+    $string = str_replace ("\317", 'Ļ', $string);
+    // latin capital letter s with caron 208 -> 352
+    $string = str_replace ("\320", 'Š', $string);
+    // latin capital letter n with acute 209 -> 323
+    $string = str_replace ("\321", 'Ń', $string);
+    // latin capital letter n with cedilla 210 -> 325
+    $string = str_replace ("\322", 'Ņ', $string);
+    // latin capital letter o with macron 212 -> 332
+    $string = str_replace ("\324", 'Ō', $string);
+    // latin capital letter u with ogonek 216 -> 370
+    $string = str_replace ("\330", 'Ų', $string);
+    // latin capital letter l with stroke 217 -> 321
+    $string = str_replace ("\331", 'Ł', $string);
+    // latin capital letter s with acute 218 -> 346
+    $string = str_replace ("\332", 'Ś', $string);
+    // latin capital letter u with macron 219 -> 362
+    $string = str_replace ("\333", 'Ū', $string);
+    // latin capital letter z with dot above 221 -> 379
+    $string = str_replace ("\335", 'Ż', $string);
+    // latin capital letter z with caron 222 -> 381
+    $string = str_replace ("\336", 'Ž', $string);
+    // latin small letter a with ogonek 224 -> 261
+    $string = str_replace ("\340", 'ą', $string);
+    // latin small letter i with ogonek 225 -> 303
+    $string = str_replace ("\341", 'į', $string);
+    // latin small letter a with macron 226 -> 257
+    $string = str_replace ("\342", 'ā', $string);
+    // latin small letter c with acute 227 -> 263
+    $string = str_replace ("\343", 'ć', $string);
+    // latin small letter e with ogonek 230 -> 281
+    $string = str_replace ("\346", 'ę', $string);
+    // latin small letter e with macron 231 -> 275
+    $string = str_replace ("\347", 'ē', $string);
+    // latin small letter c with caron 232 -> 269
+    $string = str_replace ("\350", 'č', $string);
+    // latin small letter z with acute 234 -> 378
+    $string = str_replace ("\352", 'ź', $string);
+    // latin small letter e with dot above 235 -> 279
+    $string = str_replace ("\353", 'ė', $string);
+    // latin small letter g with cedilla 236 -> 291
+    $string = str_replace ("\354", 'ģ', $string);
+    // latin small letter k with cedilla 237 -> 311
+    $string = str_replace ("\355", 'ķ', $string);
+    // latin small letter i with macron 238 -> 299
+    $string = str_replace ("\356", 'ī', $string);
+    // latin small letter l with cedilla 239 -> 316
+    $string = str_replace ("\357", 'ļ', $string);
+    // latin small letter s with caron 240 -> 253
+    $string = str_replace ("\360", 'š', $string);
+    // latin small letter n with acute 241 -> 324
+    $string = str_replace ("\361", 'ń', $string);
+    // latin small letter n with cedilla 242 -> 326
+    $string = str_replace ("\362", 'ņ', $string);
+    // latin small letter o with macron 244 -> 333
+    $string = str_replace ("\364", 'ō', $string);
+    // latin small letter u with ogonek 248 -> 371
+    $string = str_replace ("\370", 'ų', $string);
+    // latin small letter l with stroke 249 -> 322
+    $string = str_replace ("\371", 'ł', $string);
+    // latin small letter s with acute 250 -> 347
+    $string = str_replace ("\372", 'ś', $string);
+    // latin small letter u with macron 251 -> 363
+    $string = str_replace ("\373", 'ū', $string);    
+    // latin small letter z with dot above 253 -> 380
+    $string = str_replace ("\375", 'ż', $string);
+    // latin small letter z with caron 254 -> 382
+    $string = str_replace ("\376", 'ž', $string);
+    // right single quotation mark 255 -> 8217
+    $string = str_replace ("\377", '’', $string);
+
+    // rest of charset is the same as ISO-8859-1
+    return (charset_decode_iso_8859_1($string));
+}
+
 /*
  * iso-8859-15 is Latin 9 and has very much the same use as Latin 1
  * but has the Euro symbol and some characters needed for French.
@@ -673,6 +938,185 @@ function charset_decode_koi8r ($string) {
     return $string;
 }
 
+/*
+ windows-1257 (BaltRim)
+ */
+function charset_decode_windows_1257 ($string) {
+    global $default_charset, $languages, $sm_notAlias;
+
+    if (strtolower($default_charset) == 'windows-1257')
+        return $string;
+    if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'windows-1257')
+        return $string;
+
+    /* Only do the slow convert if there are 8-bit characters */
+    if (! ereg("[\200-\377]", $string))
+        return $string;
+
+    // Euro sign 128 -> 8364
+    $string = str_replace("\200", '€', $string);
+    // Single low-9 quotation mark 130 -> 8218
+    $string = str_replace("\202", '‚', $string);
+    // Double low-9 quotation mark 132 -> 8222
+    $string = str_replace("\204", '„', $string);
+    // horizontal ellipsis 133 -> 8230
+    $string = str_replace("\205", '…', $string);
+    // dagger 134 -> 8224
+    $string = str_replace("\206", '†', $string);
+    // double dagger 135 -> 8225
+    $string = str_replace("\207", '‡', $string);
+    // per mille sign 137 -> 8240
+    $string = str_replace("\211", '‰', $string);
+    // single left-pointing angle quotation mark 139 -> 8249
+    $string = str_replace("\213", '‹', $string);
+    // diaeresis 141 -> 168
+    $string = str_replace("\215", '¨', $string);
+    // caron 142 -> 711
+    $string = str_replace("\216", 'ˇ', $string);
+    // cedilla 143 -> 184
+    $string = str_replace("\217", '¸', $string);
+    // left single quotation mark 145 -> 8216
+    $string = str_replace("\221", '‘', $string);
+    // right single quotation mark 146 -> 8217
+    $string = str_replace("\222", '’', $string);
+    // left double quotation mark 147 -> 8220
+    $string = str_replace("\223", '“', $string);
+    // right double quotation mark 148 -> 8221
+    $string = str_replace("\224", '”', $string);
+    // bullet 149 -> 8226
+    $string = str_replace("\225", '•', $string);
+    // en dash 150 -> 8211
+    $string = str_replace("\226", '–', $string);
+    // em dash 151 -> 8212
+    $string = str_replace("\227", '—', $string);
+    // trade mark sign 153 -> 8482
+    $string = str_replace("\231", '™', $string);
+    // single right-pointing angle quotation mark 155 -> 8250
+    $string = str_replace("\233", '›', $string);
+    // macron 157 -> 175
+    $string = str_replace("\235", '¯', $string);
+    // ogonek 158 -> 731
+    $string = str_replace("\236", '˛', $string);
+    // latin capital letter o with stroke 168 -> 216
+    $string = str_replace ("\250", 'Ø', $string);
+    // latin capital letter r with cedilla 170 -> 342
+    $string = str_replace ("\252", 'Ŗ', $string);
+    // latin capital letter ae 175 -> 198
+    $string = str_replace ("\257", 'Æ', $string);
+    // latin small letter o with stroke 184 -> 248
+    $string = str_replace ("\270", 'ø', $string);
+    // latin small letter r with cedilla 186 -> 343
+    $string = str_replace ("\272", 'ŗ', $string);
+    // latin small letter ae 191 -> 230
+    $string = str_replace ("\277", 'æ', $string);
+    // latin capital letter a with ogonek 192 -> 260
+    $string = str_replace ("\300", 'Ą', $string);
+    // latin capital letter i with ogonek 193 -> 302
+    $string = str_replace ("\301", 'Į', $string);
+    // latin capital letter a with macron 194 -> 256
+    $string = str_replace ("\302", 'Ā', $string);
+    // latin capital letter c with acute 195 -> 262
+    $string = str_replace ("\303", 'Ć', $string);
+    // latin capital letter e with ogonek 198 -> 280
+    $string = str_replace ("\306", 'Ę', $string);
+    // latin capital letter e with macron 199 -> 274
+    $string = str_replace ("\307", 'Ē', $string);
+    // latin capital letter c with caron 200 -> 268
+    $string = str_replace ("\310", 'Č', $string);
+    // latin capital letter z with acute 202 -> 377
+    $string = str_replace ("\312", 'Ź', $string);
+    // latin capital letter e with dot above 203 -> 278
+    $string = str_replace ("\313", 'Ė', $string);
+    // latin capital letter g with cedilla 204 -> 290
+    $string = str_replace ("\314", 'Ģ', $string);
+    // latin capital letter k with cedilla 205 -> 310
+    $string = str_replace ("\315", 'Ķ', $string);
+    // latin capital letter i with macron 206 -> 298
+    $string = str_replace ("\316", 'Ī', $string);
+    // latin capital letter l with cedilla 207 -> 315
+    $string = str_replace ("\317", 'Ļ', $string);
+    // latin capital letter s with caron 208 -> 352
+    $string = str_replace ("\320", 'Š', $string);
+    // latin capital letter n with acute 209 -> 323
+    $string = str_replace ("\321", 'Ń', $string);
+    // latin capital letter n with cedilla 210 -> 325
+    $string = str_replace ("\322", 'Ņ', $string);
+    // latin capital letter o with macron 212 -> 332
+    $string = str_replace ("\324", 'Ō', $string);
+    // latin capital letter u with ogonek 216 -> 370
+    $string = str_replace ("\330", 'Ų', $string);
+    // latin capital letter l with stroke 217 -> 321
+    $string = str_replace ("\331", 'Ł', $string);
+    // latin capital letter r with acute 218 -> 340
+    $string = str_replace ("\332", 'Ŕ', $string);
+    // latin capital letter u with macron 219 -> 362
+    $string = str_replace ("\333", 'Ū', $string);
+    // latin capital letter z with dot above 221 -> 379
+    $string = str_replace ("\335", 'Ż', $string);
+    // latin capital letter z with caron 222 -> 381
+    $string = str_replace ("\336", 'Ž', $string);
+    // latin small letter a with ogonek 224 -> 261
+    $string = str_replace ("\340", 'ą', $string);
+    // latin small letter i with ogonek 225 -> 303
+    $string = str_replace ("\341", 'į', $string);
+    // latin small letter a with macron 226 -> 257
+    $string = str_replace ("\342", 'ā', $string);
+    // latin small letter c with acute 227 -> 263
+    $string = str_replace ("\343", 'ć', $string);
+    // latin small letter e with ogonek 230 -> 281
+    $string = str_replace ("\346", 'ę', $string);
+    // latin small letter e with macron 231 -> 275
+    $string = str_replace ("\347", 'ē', $string);
+    // latin small letter c with caron 232 -> 269
+    $string = str_replace ("\350", 'č', $string);
+    // latin small letter z with acute 234 -> 378
+    $string = str_replace ("\352", 'ź', $string);
+    // latin small letter e with dot above 235 -> 279
+    $string = str_replace ("\353", 'ė', $string);
+    // latin small letter g with cedilla 236 -> 291
+    $string = str_replace ("\354", 'ģ', $string);
+    // latin small letter k with cedilla 237 -> 311
+    $string = str_replace ("\355", 'ķ', $string);
+    // latin small letter i with macron 238 -> 299
+    $string = str_replace ("\356", 'ī', $string);
+    // latin small letter l with cedilla 239 -> 316
+    $string = str_replace ("\357", 'ļ', $string);
+    // latin small letter s with caron 240 -> 253
+    $string = str_replace ("\360", 'š', $string);
+    // latin small letter n with acute 241 -> 324
+    $string = str_replace ("\361", 'ń', $string);
+    // latin small letter n with cedilla 242 -> 326
+    $string = str_replace ("\362", 'ņ', $string);
+    // latin small letter o with macron 244 -> 333
+    $string = str_replace ("\364", 'ō', $string);
+    // latin small letter u with ogonek 248 -> 371
+    $string = str_replace ("\370", 'ų', $string);
+    // latin small letter l with stroke 249 -> 322
+    $string = str_replace ("\371", 'ł', $string);
+    // latin small letter s with acute 250 -> 347
+    $string = str_replace ("\372", 'ś', $string);
+    // latin small letter u with macron 251 -> 363
+    $string = str_replace ("\373", 'ū', $string);    
+    // latin small letter z with dot above 253 -> 380
+    $string = str_replace ("\375", 'ż', $string);
+    // latin small letter z with caron 254 -> 382
+    $string = str_replace ("\376", 'ž', $string);
+    // dot above 255 -> 729
+    $string = str_replace ("\377", '˙', $string);
+
+    // Rest of charset is like iso-8859-1
+    return (charset_decode_iso_8859_1($string));
+}
+
+/* windows-1251 is Microsoft Cyrillic encoding */
+function charset_decode_windows_1251 ($string) {
+    // Convert to KOI8-R, then return this decoded.
+    $string = convert_cyr_string($string, 'w', 'k');
+    return charset_decode_koi8r($string);
+}
+
+
+
 /*
  * Set up the language to be output
  * if $do_search is true, then scan the browser information
@@ -681,7 +1125,7 @@ function charset_decode_koi8r ($string) {
 function set_up_language($sm_language, $do_search = false) {
 
     static $SetupAlready = 0;
-    global $HTTP_ACCEPT_LANGUAGE, $use_gettext, $languages,
+    global $use_gettext, $languages,
            $squirrelmail_language, $squirrelmail_default_language,
            $sm_notAlias;
 
@@ -690,8 +1134,8 @@ function set_up_language($sm_language, $do_search = false) {
     }
     $SetupAlready = TRUE;
 
-    if ($do_search && ! $sm_language && isset($HTTP_ACCEPT_LANGUAGE)) {
-        $sm_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2);
+    if ($do_search && ! $sm_language && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+        $sm_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
     }
     
     if (!$sm_language && isset($squirrelmail_default_language)) {
@@ -717,9 +1161,18 @@ function set_up_language($sm_language, $do_search = false) {
         }
         setlocale(LC_ALL, $sm_notAlias);
         $squirrelmail_language = $sm_notAlias;
+        if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
+            header ('Content-Type: text/html; charset=EUC-JP');
+            if (!function_exists('mb_internal_encoding')) {
+                echo _("You need to have php4 installed with the multibyte string function enabled (using configure option --with-mbstring).");
+            }
+            mb_internal_encoding('EUC-JP');
+            mb_http_output('pass');
+        } else {
         header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
     }
 }
+}
 
 function set_my_charset(){
 
@@ -820,8 +1273,14 @@ $languages['it_IT']['NAME']    = 'Italian';
 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
 $languages['it']['ALIAS'] = 'it_IT';
 
+$languages['ja_JP']['NAME']    = 'Japanese';
+$languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
+$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
+$languages['ja']['ALIAS'] = 'ja_JP';
+
 $languages['ko_KR']['NAME']    = 'Korean';
 $languages['ko_KR']['CHARSET'] = 'euc-KR';
+$languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
 $languages['ko']['ALIAS'] = 'ko_KR';
 
 $languages['nl_NL']['NAME']    = 'Dutch';
@@ -860,13 +1319,13 @@ $languages['tr_TR']['NAME']    = 'Turkish';
 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
 $languages['tr']['ALIAS'] = 'tr_TR';
 
-$languages['zh_TW']['NAME']    = 'Taiwan';
+$languages['zh_TW']['NAME']    = 'Chinese Trad';
 $languages['zh_TW']['CHARSET'] = 'big5';
 $languages['tw']['ALIAS'] = 'zh_TW';
 
-$languages['zh_TW']['NAME']    = 'Chinese';
-$languages['zh_TW']['CHARSET'] = 'gb2312';
-$languages['tw']['ALIAS'] = 'zh_CN';
+$languages['zh_CN']['NAME']    = 'Chinese Simp';
+$languages['zh_CN']['CHARSET'] = 'gb2312';
+$languages['cn']['ALIAS'] = 'zh_CN';
 
 $languages['sk_SK']['NAME']     = 'Slovak';
 $languages['sk_SK']['CHARSET']  = 'iso-8859-2';
@@ -881,7 +1340,7 @@ $languages['th_TH']['CHARSET'] = 'tis-620';
 $languages['th']['ALIAS'] = 'th_TH';
 
 $languages['lt_LT']['NAME']    = 'Lithuanian';
-$languages['lt_LT']['CHARSET'] = 'iso-8859-13';
+$languages['lt_LT']['CHARSET'] = 'windows-1257';
 $languages['lt']['ALIAS'] = 'lt_LT';
 
 $languages['sl_SI']['NAME']    = 'Slovenian';
@@ -892,6 +1351,10 @@ $languages['bg_BG']['NAME']    = 'Bulgarian';
 $languages['bg_BG']['CHARSET'] = 'windows-1251';
 $languages['bg']['ALIAS'] = 'bg_BG';
 
+$languages['uk_UA']['NAME']    = 'Ukrainian';
+$languages['uk_UA']['CHARSET'] = 'koi8-u';
+$languages['uk']['ALIAS'] = 'uk_UA';
+
 // Right to left languages
 
 $languages['he_HE']['NAME']    = 'Hebrew';
@@ -919,7 +1382,7 @@ if ($gettext_flags == 7) {
 /* If we can fake gettext, try that */
 elseif ($gettext_flags == 0) {
     $use_gettext = true;
-    include_once('../functions/gettext.php');
+    include_once(SM_PATH . 'functions/gettext.php');
 } else {
     /* Uh-ho.  A weird install */
     if (! $gettext_flags & 1) {
@@ -939,4 +1402,112 @@ elseif ($gettext_flags == 0) {
     }
 }
 
+
+/*
+ * Japanese charset extra function
+ *
+ */
+function japanese_charset_xtra() {
+    $ret = func_get_arg(1);  /* default return value */
+    if (function_exists('mb_detect_encoding')) {
+        switch (func_get_arg(0)) { /* action */
+        case 'decode':
+            $detect_encoding = mb_detect_encoding($ret);
+            if ($detect_encoding == 'JIS' ||
+                $detect_encoding == 'EUC-JP' ||
+                $detect_encoding == 'SJIS') {
+                
+                $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+            }
+            break;
+        case 'encode':
+            $detect_encoding = mb_detect_encoding($ret);
+            if ($detect_encoding == 'JIS' ||
+                $detect_encoding == 'EUC-JP' ||
+                $detect_encoding == 'SJIS') {
+                
+                $ret = mb_convert_encoding($ret, 'JIS', 'AUTO');
+            }
+            break;
+        case 'strimwidth':
+            $width = func_get_arg(2);
+            $ret = mb_strimwidth($ret, 0, $width, '...'); 
+            break;
+        case 'encodeheader':
+            $result = '';
+            if (strlen($ret) > 0) {
+                $tmpstr = mb_substr($ret, 0, 1);
+                $prevcsize = strlen($tmpstr);
+                for ($i = 1; $i < mb_strlen($ret); $i++) {
+                    $tmp = mb_substr($ret, $i, 1);
+                    if (strlen($tmp) == $prevcsize) {
+                        $tmpstr .= $tmp;
+                    } else {
+                        if ($prevcsize == 1) {
+                            $result .= $tmpstr;
+                        } else {
+                            $result .= mb_encode_mimeheader($tmpstr);
+                        }
+                        $tmpstr = $tmp;
+                        $prevcsize = strlen($tmp);
+                    }
+                }
+                if (strlen($tmpstr)) {
+                    if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
+                        $result .= $tmpstr;
+                    else
+                        $result .= mb_encode_mimeheader($tmpstr);
+                }
+            }
+            $ret = $result;
+            //$ret = mb_encode_mimeheader($ret);
+            break;
+        case 'decodeheader':
+            $ret = str_replace("\t", "", $ret);
+            if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
+                $ret = mb_decode_mimeheader($ret);
+            $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+            break;
+        case 'downloadfilename':
+            $useragent = func_get_arg(2);
+            if (strstr($useragent, 'Windows') !== false ||
+                strstr($useragent, 'Mac_') !== false) {
+                $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
+            } else {
+                $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+}
+            break;
+        }
+    }
+    return $ret;
+}
+
+
+/*
+ * Korean charset extra function
+ * Hangul(Korean Character) Attached File Name Fix.
+ */
+function korean_charset_xtra() {
+    
+    $ret = func_get_arg(1);  /* default return value */
+    if (func_get_arg(0) == 'downloadfilename') { /* action */
+        $ret = str_replace("\x0D\x0A", '', $ret);  /* Hanmail's CR/LF Clear */
+        for ($i=0;$i<strlen($ret);$i++) {
+            if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") {   /* 0xA1 - 0XFE are Valid */
+                $i++;
+                continue;
+            } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
+                       ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
+                       ($ret[$i] == '.') || ($ret[$i] == '-')) {
+                continue;
+            } else {
+                $ret[$i] = '_';
+            }
+        }
+
+    }
+
+    return $ret;
+}
+
 ?>