Hmm, client side sorting on large mailboxes is so fast I didn't even noticed
[squirrelmail.git] / functions / i18n.php
CommitLineData
59177427 1<?php
1fd97780 2
35586184 3/**
d3bab52e 4 * SquirrelMail internationalization functions
35586184 5 *
2ba706ef 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file contains variuos functions that are needed to do
10 * internationalization of SquirrelMail.
11 *
12 * Internally the output character set is used. Other characters are
13 * encoded using Unicode entities according to HTML 4.0.
14 *
a8a1c36d 15 * @version $Id$
d6c32258 16 * @package squirrelmail
a8a1c36d 17 * @subpackage i18n
35586184 18 */
19
d6c32258 20/** Everything uses global.php... */
961ca3d8 21require_once(SM_PATH . 'functions/global.php');
22
d6c32258 23/**
51468260 24 * Converts string from given charset to charset, that can be displayed by user translation.
25 *
26 * Function by default returns html encoded strings, if translation uses different encoding.
27 * If Japanese translation is used - function returns string converted to euc-jp
28 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
29 * If $charset is not supported - function returns unconverted string.
d6c32258 30 *
51468260 31 * sanitizing of html tags is also done by this function.
32 *
d6c32258 33 * @param string $charset
34 * @param string $string Text to be decoded
51468260 35 * @return string decoded string
d6c32258 36 */
a2a7852b 37function charset_decode ($charset, $string) {
3ec81e63 38 global $languages, $squirrelmail_language, $default_charset;
edf2c0ba 39 global $use_php_recode, $use_php_iconv, $agresive_decoding;
a2a7852b 40
3714db45 41 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
42 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 43 $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
44 }
b05c8961 45
3ec81e63 46 $charset = strtolower($charset);
47
48 set_my_charset();
49
50 // Variables that allow to use functions without function_exist() calls
edf2c0ba 51 if (! isset($use_php_recode) || $use_php_recode=="" ) {
03db90bc 52 $use_php_recode=false; }
edf2c0ba 53 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
54 $use_php_iconv=false; }
3ec81e63 55
56 // Don't do conversion if charset is the same.
57 if ( $charset == strtolower($default_charset) )
58 return htmlspecialchars($string);
59
60 // catch iso-8859-8-i thing
61 if ( $charset == "iso-8859-8-i" )
62 $charset = "iso-8859-8";
63
64 /*
65 * Recode converts html special characters automatically if you use
66 * 'charset..html' decoding. There is no documented way to put -d option
67 * into php recode function call.
68 */
69 if ( $use_php_recode ) {
70 if ( $default_charset == "utf-8" ) {
03db90bc 71 // other charsets can be converted to utf-8 without loss.
72 // and output string is smaller
73 $string = recode_string($charset . "..utf-8",$string);
74 return htmlspecialchars($string);
3ec81e63 75 } else {
03db90bc 76 $string = recode_string($charset . "..html",$string);
77 // recode does not convert single quote, htmlspecialchars does.
78 $string = str_replace("'", '&#039;', $string);
79 return $string;
3ec81e63 80 }
81 }
82
83 // iconv functions does not have html target and can be used only with utf-8
84 if ( $use_php_iconv && $default_charset=='utf-8') {
85 $string = iconv($charset,$default_charset,$string);
86 return htmlspecialchars($string);
87 }
88
89 // If we don't use recode and iconv, we'll do it old way.
90
a2a7852b 91 /* All HTML special characters are 7 bit and can be replaced first */
cef054e4 92
098ea084 93 $string = htmlspecialchars ($string);
a2a7852b 94
5dd23dac 95 /* controls cpu and memory intensive decoding cycles */
edf2c0ba 96 if (! isset($agresive_decoding) || $agresive_decoding=="" ) {
97 $agresive_decoding=false; }
5dd23dac 98
b142de74 99 $decode=fixcharset($charset);
100 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
101 if (file_exists($decodefile)) {
03db90bc 102 include_once($decodefile);
103 $ret = call_user_func('charset_decode_'.$decode, $string);
a2a7852b 104 } else {
03db90bc 105 $ret = $string;
a2a7852b 106 }
107 return( $ret );
108}
03db90bc 109
d3bab52e 110/**
111 * Converts html string to given charset
112 * @param string $string
113 * @param string $charset
114 * @param string
115 */
116function charset_encode($string,$charset) {
117 global $default_charset;
118
119 $encode=fixcharset($charset);
120 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
121 if (file_exists($encodefile)) {
122 include_once($encodefile);
123 $ret = call_user_func('charset_encode_'.$encode, $string);
124 } else {
125 $ret = $string;
126 }
127 return( $ret );
128}
129
130/**
131 * Combined decoding and encoding functions
132 *
133 * If conversion is done to charset different that utf-8, unsupported symbols
134 * will be replaced with question marks.
135 * @param string $in_charset initial charset
136 * @param string $string string that has to be converted
137 * @param string $out_charset final charset
138 * @return string converted string
139 */
140function charset_convert($in_charset,$string,$out_charset) {
141 $string=charset_decode($in_charset,$string);
142 $string=charset_encode($string,$out_charset);
143 return $string;
144}
145
b142de74 146/**
147 * Makes charset name suitable for decoding cycles
148 *
149 * @param string $charset Name of charset
150 * @return string $charset Adjusted name of charset
151 */
152function fixcharset($charset) {
153 // minus removed from function names
154 $charset=str_replace('-','_',$charset);
155
156 // windows-125x and cp125x charsets
157 $charset=str_replace('windows_','cp',$charset);
a2a7852b 158
b142de74 159 // ibm > cp
160 $charset=str_replace('ibm','cp',$charset);
161
162 // iso-8859-8-i -> iso-8859-8
163 // use same cycle until I'll find differences
164 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
165
166 return $charset;
167}
a2a7852b 168
51468260 169/**
a2a7852b 170 * Set up the language to be output
171 * if $do_search is true, then scan the browser information
172 * for a possible language that we know
51468260 173 *
174 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
175 * gettext translation bindings and html header information.
176 *
5679405c 177 * Function returns error codes, if there is some fatal error.
51468260 178 * 0 = no error,
179 * 1 = mbstring support is not present,
180 * 2 = mbstring support is not present, user's translation reverted to en_US.
181 *
182 * @param string $sm_language translation used by user's interface
183 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
184 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
185 * @return int function execution error codes.
a2a7852b 186 */
67a8c90a 187function set_up_language($sm_language, $do_search = false, $default = false) {
a2a7852b 188
189 static $SetupAlready = 0;
9eb0fbd4 190 global $use_gettext, $languages,
a2a7852b 191 $squirrelmail_language, $squirrelmail_default_language,
51468260 192 $sm_notAlias, $username, $data_dir;
a2a7852b 193
194 if ($SetupAlready) {
195 return;
196 }
a65846a7 197
5c920668 198 $SetupAlready = TRUE;
961ca3d8 199 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
a2a7852b 200
961ca3d8 201 if ($do_search && ! $sm_language && isset($accept_lang)) {
202 $sm_language = substr($accept_lang, 0, 2);
a2a7852b 203 }
66d7950f 204
67a8c90a 205 if ((!$sm_language||$default) && isset($squirrelmail_default_language)) {
a2a7852b 206 $squirrelmail_language = $squirrelmail_default_language;
66d7950f 207 $sm_language = $squirrelmail_default_language;
a2a7852b 208 }
209 $sm_notAlias = $sm_language;
3ec81e63 210
211 // Catching removed translation
212 // System reverts to English translation if user prefs contain translation
2ba706ef 213 // that is not available in $languages array
3ec81e63 214 if (!isset($languages[$sm_notAlias])) {
215 $sm_notAlias="en_US";
216 }
217
a2a7852b 218 while (isset($languages[$sm_notAlias]['ALIAS'])) {
219 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
220 }
221
88cb1b4d 222 if ( isset($sm_language) &&
5c920668 223 $use_gettext &&
224 $sm_language != '' &&
225 isset($languages[$sm_notAlias]['CHARSET']) ) {
a65846a7 226 bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
88cb1b4d 227 textdomain( 'squirrelmail' );
03db90bc 228 if (function_exists('bind_textdomain_codeset')) {
229 if ($sm_notAlias == 'ja_JP') {
230 bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
a5970d71 231 } else {
03db90bc 232 bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
233 }
234 }
235 if (isset($languages[$sm_notAlias]['LOCALE'])){
236 $longlocale=$languages[$sm_notAlias]['LOCALE'];
237 } else {
238 $longlocale=$sm_notAlias;
239 }
88cb1b4d 240 if ( !ini_get('safe_mode') &&
f2374580 241 getenv( 'LC_ALL' ) != $longlocale ) {
242 putenv( "LC_ALL=$longlocale" );
243 putenv( "LANG=$longlocale" );
244 putenv( "LANGUAGE=$longlocale" );
a2a7852b 245 }
03db90bc 246 setlocale(LC_ALL, $longlocale);
247
248 // Set text direction/alignment variables
249 if (isset($languages[$sm_notAlias]['DIR']) &&
250 $languages[$sm_notAlias]['DIR'] == 'rtl') {
251 /**
252 * Text direction
253 * @global string $text_direction
254 */
255 $text_direction='rtl';
256 /**
257 * Left alignment
258 * @global string $left_align
259 */
260 $left_align='right';
261 /**
262 * Right alignment
263 * @global string $right_align
264 */
265 $right_align='left';
266 } else {
267 $text_direction='ltr';
268 $left_align='left';
269 $right_align='right';
270 }
271
272 $squirrelmail_language = $sm_notAlias;
a5970d71 273 if ($squirrelmail_language == 'ja_JP') {
b05c8961 274 header ('Content-Type: text/html; charset=EUC-JP');
275 if (!function_exists('mb_internal_encoding')) {
03db90bc 276 // Error messages can't be displayed here
277 $error = 1;
278 // Revert to English if possible.
279 if (function_exists('setPref') && $username!='' && $data_dir!="") {
280 setPref($data_dir, $username, 'language', "en_US");
281 $error = 2;
282 }
283 // stop further execution in order not to get php errors on mb_internal_encoding().
284 return $error;
e842b215 285 }
286 if (function_exists('mb_language')) {
287 mb_language('Japanese');
b05c8961 288 }
289 mb_internal_encoding('EUC-JP');
290 mb_http_output('pass');
291 } else {
5c920668 292 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
a2a7852b 293 }
294}
51468260 295 return 0;
b05c8961 296}
a2a7852b 297
51468260 298/**
299 * Sets default_charset variable according to the one that is used by user's translations.
300 *
301 * Function changes global $default_charset variable in order to be sure, that it
302 * contains charset used by user's translation. Sanity of $squirrelmail_default_language
303 * and $default_charset combination provided in SquirrelMail config is also tested.
304 *
305 * There can be a $default_charset setting in the
306 * config.php file, but the user may have a different language
307 * selected for a user interface. This function checks the
308 * language selected by the user and tags the outgoing messages
309 * with the appropriate charset corresponding to the language
310 * selection. This is "more right" (tm), than just stamping the
311 * message blindly with the system-wide $default_charset.
312 */
a2a7852b 313function set_my_charset(){
94965562 314 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
88cb1b4d 315
a2a7852b 316 $my_language = getPref($data_dir, $username, 'language');
5c920668 317 if (!$my_language) {
94965562 318 $my_language = $squirrelmail_default_language ;
5c920668 319 }
3ec81e63 320 // Catch removed translation
321 if (!isset($languages[$my_language])) {
322 $my_language="en_US";
323 }
a2a7852b 324 while (isset($languages[$my_language]['ALIAS'])) {
f7e8861e 325 $my_language = $languages[$my_language]['ALIAS'];
a2a7852b 326 }
5c920668 327 $my_charset = $languages[$my_language]['CHARSET'];
a2a7852b 328 if ($my_charset) {
329 $default_charset = $my_charset;
330 }
331}
332
a2a7852b 333/* ------------------------------ main --------------------------- */
334
5c920668 335global $squirrelmail_language, $languages, $use_gettext;
336
a2a7852b 337if (! isset($squirrelmail_language)) {
338 $squirrelmail_language = '';
339}
340
51468260 341/**
342 * Array specifies the available translations.
343 *
344 * Structure of array:
345 * $languages['language']['variable'] = 'value'
346 *
347 * Possible 'variable' names:
348 * NAME - Translation name in English
349 * CHARSET - Encoding used by translation
350 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
351 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
352 * LOCALE - Full locale name (in xx_XX.charset format)
353 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
354 * XTRA_CODE - translation uses special functions. 'value' provides name of that extra function
355 *
356 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
357 *
358 * @name $languages
a8a1c36d 359 * @global array $languages
51468260 360 */
a8fa8e33 361$languages['bg_BG']['NAME'] = 'Bulgarian';
362$languages['bg_BG']['ALTNAME'] = '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;';
363$languages['bg_BG']['CHARSET'] = 'windows-1251';
c30be3cf 364$languages['bg_BG']['LOCALE'] = 'bg_BG.CP1251';
a8fa8e33 365$languages['bg']['ALIAS'] = 'bg_BG';
366
367$languages['ca_ES']['NAME'] = 'Catalan';
368$languages['ca_ES']['CHARSET'] = 'iso-8859-1';
a00d341d 369$languages['ca_ES']['LOCALE'] = 'ca_ES.ISO8859-1';
a8fa8e33 370$languages['ca']['ALIAS'] = 'ca_ES';
371
372$languages['cs_CZ']['NAME'] = 'Czech';
373$languages['cs_CZ']['ALTNAME'] = '&#268;e&scaron;tina';
374$languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
a00d341d 375$languages['cs_CZ']['LOCALE'] = 'cs_CZ.ISO8859-2';
a8fa8e33 376$languages['cs']['ALIAS'] = 'cs_CZ';
377
378$languages['cy_GB']['NAME'] = 'Welsh';
379$languages['cy_GB']['ALTNAME'] = 'Cymraeg';
380$languages['cy_GB']['CHARSET'] = 'iso-8859-1';
a00d341d 381$languages['cy_GB']['LOCALE'] = 'cy_GB.ISO8859-1';
a8fa8e33 382$languages['cy']['ALIAS'] = 'cy_GB';
383
384// Danish locale is da_DK.
385$languages['da_DK']['NAME'] = 'Danish';
386$languages['da_DK']['ALTNAME'] = 'Dansk';
387$languages['da_DK']['CHARSET'] = 'iso-8859-1';
a00d341d 388$languages['da_DK']['LOCALE'] = 'da_DK.ISO8859-1';
a8fa8e33 389$languages['da']['ALIAS'] = 'da_DK';
390
391$languages['de_DE']['NAME'] = 'German';
392$languages['de_DE']['ALTNAME'] = 'Deutsch';
393$languages['de_DE']['CHARSET'] = 'iso-8859-1';
a00d341d 394$languages['de_DE']['LOCALE'] = 'de_DE.ISO8859-1';
a8fa8e33 395$languages['de']['ALIAS'] = 'de_DE';
396
397$languages['el_GR']['NAME'] = 'Greek';
398$languages['el_GR']['ALTNAME'] = '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;';
399$languages['el_GR']['CHARSET'] = 'iso-8859-7';
a00d341d 400$languages['el_GR']['LOCALE'] = 'el_GR.ISO8859-7';
a8fa8e33 401$languages['el']['ALIAS'] = 'el_GR';
a2a7852b 402
3bb3d83b 403$languages['en_GB']['NAME'] = 'British';
404$languages['en_GB']['CHARSET'] = 'iso-8859-15';
a00d341d 405$languages['en_GB']['LOCALE'] = 'en_GB.ISO8859-15';
3bb3d83b 406
5c920668 407$languages['en_US']['NAME'] = 'English';
408$languages['en_US']['CHARSET'] = 'iso-8859-1';
a00d341d 409$languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
5c920668 410$languages['en']['ALIAS'] = 'en_US';
a2a7852b 411
a8fa8e33 412$languages['es_ES']['NAME'] = 'Spanish';
413$languages['es_ES']['ALTNAME'] = 'Espa&ntilde;ol';
414$languages['es_ES']['CHARSET'] = 'iso-8859-1';
a00d341d 415$languages['es_ES']['LOCALE'] = 'es_ES.ISO8859-1';
a8fa8e33 416$languages['es']['ALIAS'] = 'es_ES';
417
418$languages['et_EE']['NAME'] = 'Estonian';
419$languages['et_EE']['CHARSET'] = 'iso-8859-15';
a00d341d 420$languages['et_EE']['LOCALE'] = 'et_EE.ISO8859-15';
a8fa8e33 421$languages['et']['ALIAS'] = 'et_EE';
422
2d268514 423$languages['eu_ES']['NAME'] = 'Basque';
424$languages['eu_ES']['CHARSET'] = 'iso-8859-1';
425$languages['eu_ES']['LOCALE'] = 'eu_ES.ISO8859-1';
426$languages['eu']['ALIAS'] = 'eu_ES';
427
a8fa8e33 428$languages['fo_FO']['NAME'] = 'Faroese';
429$languages['fo_FO']['CHARSET'] = 'iso-8859-1';
a00d341d 430$languages['fo_FO']['LOCALE'] = 'fo_FO.ISO8859-1';
a8fa8e33 431$languages['fo']['ALIAS'] = 'fo_FO';
432
433$languages['fi_FI']['NAME'] = 'Finnish';
434$languages['fi_FI']['ALTNAME'] = 'Suomi';
435$languages['fi_FI']['CHARSET'] = 'iso-8859-1';
a00d341d 436$languages['fi_FI']['LOCALE'] = 'fi_FI.ISO8859-1';
a8fa8e33 437$languages['fi']['ALIAS'] = 'fi_FI';
438
439$languages['fr_FR']['NAME'] = 'French';
440$languages['fr_FR']['ALTNAME'] = 'Fran&#231;ais';
441$languages['fr_FR']['CHARSET'] = 'iso-8859-1';
a00d341d 442$languages['fr_FR']['LOCALE'] = 'fr_FR.ISO8859-1';
a8fa8e33 443$languages['fr']['ALIAS'] = 'fr_FR';
444
445$languages['hr_HR']['NAME'] = 'Croatian';
446$languages['hr_HR']['CHARSET'] = 'iso-8859-2';
a00d341d 447$languages['hr_HR']['LOCALE'] = 'hr_HR.ISO8859-2';
a8fa8e33 448$languages['hr']['ALIAS'] = 'hr_HR';
449
450$languages['hu_HU']['NAME'] = 'Hungarian';
451$languages['hu_HU']['ALTNAME'] = 'Magyar';
452$languages['hu_HU']['CHARSET'] = 'iso-8859-2';
a00d341d 453$languages['hu_HU']['LOCALE'] = 'hu_HU.ISO8859-2';
a8fa8e33 454$languages['hu']['ALIAS'] = 'hu_HU';
455
456$languages['id_ID']['NAME'] = 'Indonesian';
457$languages['id_ID']['ALTNAME'] = 'Bahasa Indonesia';
458$languages['id_ID']['CHARSET'] = 'iso-8859-1';
a00d341d 459$languages['id_ID']['LOCALE'] = 'id_ID.ISO8859-1';
a8fa8e33 460$languages['id']['ALIAS'] = 'id_ID';
461
462$languages['is_IS']['NAME'] = 'Icelandic';
463$languages['is_IS']['ALTNAME'] = '&Iacute;slenska';
464$languages['is_IS']['CHARSET'] = 'iso-8859-1';
a00d341d 465$languages['is_IS']['LOCALE'] = 'is_IS.ISO8859-1';
a8fa8e33 466$languages['is']['ALIAS'] = 'is_IS';
467
468$languages['it_IT']['NAME'] = 'Italian';
469$languages['it_IT']['CHARSET'] = 'iso-8859-1';
a00d341d 470$languages['it_IT']['LOCALE'] = 'it_IT.ISO8859-1';
a8fa8e33 471$languages['it']['ALIAS'] = 'it_IT';
472
473$languages['ja_JP']['NAME'] = 'Japanese';
474$languages['ja_JP']['ALTNAME'] = '&#26085;&#26412;&#35486;';
475$languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
51468260 476$languages['ja_JP']['LOCALE'] = 'ja_JP.EUC-JP';
a8fa8e33 477$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
478$languages['ja']['ALIAS'] = 'ja_JP';
479
480$languages['ko_KR']['NAME'] = 'Korean';
481$languages['ko_KR']['CHARSET'] = 'euc-KR';
c30be3cf 482$languages['ko_KR']['LOCALE'] = 'ko_KR.EUC-KR';
1c9787d6 483// Function does not provide all needed options
484// $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
a8fa8e33 485$languages['ko']['ALIAS'] = 'ko_KR';
486
487$languages['lt_LT']['NAME'] = 'Lithuanian';
488$languages['lt_LT']['ALTNAME'] = 'Lietuvi&#371;';
489$languages['lt_LT']['CHARSET'] = 'utf-8';
490$languages['lt_LT']['LOCALE'] = 'lt_LT.UTF-8';
491$languages['lt']['ALIAS'] = 'lt_LT';
492
493$languages['nl_NL']['NAME'] = 'Dutch';
494$languages['nl_NL']['ALTNAME'] = 'Nederlands';
495$languages['nl_NL']['CHARSET'] = 'iso-8859-1';
a00d341d 496$languages['nl_NL']['LOCALE'] = 'nl_NL.ISO8859-1';
a8fa8e33 497$languages['nl']['ALIAS'] = 'nl_NL';
498
499$languages['ms_MY']['NAME'] = 'Malay';
500$languages['ms_MY']['ALTNAME'] = 'Bahasa Melayu';
501$languages['ms_MY']['CHARSET'] = 'iso-8859-1';
a00d341d 502$languages['ms_MY']['LOCALE'] = 'ms_MY.ISO8859-1';
a8fa8e33 503$languages['my']['ALIAS'] = 'ms_MY';
504
850db3c8 505$languages['nb_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
506$languages['nb_NO']['ALTNAME'] = 'Norsk (Bokm&aring;l)';
507$languages['nb_NO']['CHARSET'] = 'iso-8859-1';
a00d341d 508$languages['nb_NO']['LOCALE'] = 'nb_NO.ISO8859-1';
850db3c8 509$languages['nb']['ALIAS'] = 'nb_NO';
a8fa8e33 510
511$languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
512$languages['nn_NO']['ALTNAME'] = 'Norsk (Nynorsk)';
513$languages['nn_NO']['CHARSET'] = 'iso-8859-1';
a00d341d 514$languages['nn_NO']['LOCALE'] = 'nn_NO.ISO8859-1';
a8fa8e33 515
516$languages['pl_PL']['NAME'] = 'Polish';
517$languages['pl_PL']['ALTNAME'] = 'Polski';
518$languages['pl_PL']['CHARSET'] = 'iso-8859-2';
a00d341d 519$languages['pl_PL']['LOCALE'] = 'pl_PL.ISO8859-2';
a8fa8e33 520$languages['pl']['ALIAS'] = 'pl_PL';
521
522$languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
523$languages['pt_PT']['CHARSET'] = 'iso-8859-1';
a00d341d 524$languages['pt_PT']['LOCALE'] = 'pt_PT.ISO8859-1';
a8fa8e33 525$languages['pt']['ALIAS'] = 'pt_PT';
526
527$languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
528$languages['pt_BR']['ALTNAME'] = 'Portugu&ecirc;s do Brasil';
529$languages['pt_BR']['CHARSET'] = 'iso-8859-1';
a00d341d 530$languages['pt_BR']['LOCALE'] = 'pt_BR.ISO8859-1';
a8fa8e33 531
532$languages['ro_RO']['NAME'] = 'Romanian';
533$languages['ro_RO']['ALTNAME'] = 'Rom&acirc;n&#259;';
534$languages['ro_RO']['CHARSET'] = 'iso-8859-2';
a00d341d 535$languages['ro_RO']['LOCALE'] = 'ro_RO.ISO8859-2';
a8fa8e33 536$languages['ro']['ALIAS'] = 'ro_RO';
537
538$languages['ru_RU']['NAME'] = 'Russian';
539$languages['ru_RU']['ALTNAME'] = '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;';
540$languages['ru_RU']['CHARSET'] = 'utf-8';
c30be3cf 541$languages['ru_RU']['LOCALE'] = 'ru_RU.UTF-8';
a8fa8e33 542$languages['ru']['ALIAS'] = 'ru_RU';
543
c30be3cf 544$languages['sk_SK']['NAME'] = 'Slovak';
545$languages['sk_SK']['CHARSET'] = 'iso-8859-2';
a00d341d 546$languages['sk_SK']['LOCALE'] = 'sk_SK.ISO8859-2';
c30be3cf 547$languages['sk']['ALIAS'] = 'sk_SK';
a8fa8e33 548
549$languages['sl_SI']['NAME'] = 'Slovenian';
550$languages['sl_SI']['ALTNAME'] = 'Sloven&scaron;&#269;ina';
551$languages['sl_SI']['CHARSET'] = 'iso-8859-2';
a00d341d 552$languages['sl_SI']['LOCALE'] = 'sl_SI.ISO8859-2';
a8fa8e33 553$languages['sl']['ALIAS'] = 'sl_SI';
554
555$languages['sr_YU']['NAME'] = 'Serbian';
556$languages['sr_YU']['ALTNAME'] = 'Srpski';
557$languages['sr_YU']['CHARSET'] = 'iso-8859-2';
a00d341d 558$languages['sr_YU']['LOCALE'] = 'sr_YU.ISO8859-2';
a8fa8e33 559$languages['sr']['ALIAS'] = 'sr_YU';
560
561$languages['sv_SE']['NAME'] = 'Swedish';
562$languages['sv_SE']['ALTNAME'] = 'Svenska';
563$languages['sv_SE']['CHARSET'] = 'iso-8859-1';
a00d341d 564$languages['sv_SE']['LOCALE'] = 'sv_SE.ISO8859-1';
a8fa8e33 565$languages['sv']['ALIAS'] = 'sv_SE';
566
567$languages['th_TH']['NAME'] = 'Thai';
568$languages['th_TH']['CHARSET'] = 'tis-620';
c30be3cf 569$languages['th_TH']['LOCALE'] = 'th_TH.TIS-620';
a8fa8e33 570$languages['th']['ALIAS'] = 'th_TH';
571
551a09c7 572$languages['tl_PH']['NAME'] = 'Tagalog';
573$languages['tl_PH']['CHARSET'] = 'iso-8859-1';
a00d341d 574$languages['tl_PH']['LOCALE'] = 'tl_PH.ISO8859-1';
551a09c7 575$languages['tl']['ALIAS'] = 'tl_PH';
576
a8fa8e33 577$languages['tr_TR']['NAME'] = 'Turkish';
578$languages['tr_TR']['CHARSET'] = 'iso-8859-9';
a00d341d 579$languages['tr_TR']['LOCALE'] = 'tr_TR.ISO8859-9';
a8fa8e33 580$languages['tr']['ALIAS'] = 'tr_TR';
581
582$languages['zh_TW']['NAME'] = 'Chinese Trad';
583$languages['zh_TW']['CHARSET'] = 'big5';
c30be3cf 584$languages['zh_TW']['LOCALE'] = 'zh_TW.BIG5';
a8fa8e33 585$languages['tw']['ALIAS'] = 'zh_TW';
586
587$languages['zh_CN']['NAME'] = 'Chinese Simp';
588$languages['zh_CN']['CHARSET'] = 'gb2312';
c30be3cf 589$languages['zh_CN']['LOCALE'] = 'zh_CN.GB2312';
a8fa8e33 590$languages['cn']['ALIAS'] = 'zh_CN';
060c9483 591
a8fa8e33 592$languages['uk_UA']['NAME'] = 'Ukrainian';
593$languages['uk_UA']['CHARSET'] = 'koi8-u';
c30be3cf 594$languages['uk_UA']['LOCALE'] = 'uk_UA.KOI8-U';
a8fa8e33 595$languages['uk']['ALIAS'] = 'uk_UA';
c30be3cf 596
597$languages['ru_UA']['NAME'] = 'Russian (Ukrainian)';
598$languages['ru_UA']['CHARSET'] = 'koi8-r';
599$languages['ru_UA']['LOCALE'] = 'ru_UA.KOI8-R';
600
87c6b544 601/*
850db3c8 602$languages['vi_VN']['NAME'] = 'Vietnamese';
603$languages['vi_VN']['CHARSET'] = 'utf-8';
604$languages['vi']['ALIAS'] = 'vi_VN';
87c6b544 605*/
a8fa8e33 606
d3b57948 607// Right to left languages
a8fa8e33 608$languages['ar']['NAME'] = 'Arabic';
609$languages['ar']['CHARSET'] = 'windows-1256';
610$languages['ar']['DIR'] = 'rtl';
611
4417eead 612$languages['fa_IR']['NAME'] = 'Farsi';
613$languages['fa_IR']['CHARSET'] = 'utf-8';
614$languages['fa_IR']['DIR'] = 'rtl';
615$languages['fa_IR']['LOCALE'] = 'fa_IR.UTF-8';
616$languages['fa']['ALIAS'] = 'fa_IR';
617
a8fa8e33 618$languages['he_IL']['NAME'] = 'Hebrew';
619$languages['he_IL']['CHARSET'] = 'windows-1255';
c30be3cf 620$languages['he_IL']['LOCALE'] = 'he_IL.CP1255';
a8fa8e33 621$languages['he_IL']['DIR'] = 'rtl';
622$languages['he']['ALIAS'] = 'he_IL';
d3b57948 623
5c920668 624/* Detect whether gettext is installed. */
a2a7852b 625$gettext_flags = 0;
626if (function_exists('_')) {
627 $gettext_flags += 1;
628}
629if (function_exists('bindtextdomain')) {
630 $gettext_flags += 2;
631}
632if (function_exists('textdomain')) {
633 $gettext_flags += 4;
634}
635
5c920668 636/* If gettext is fully loaded, cool */
a2a7852b 637if ($gettext_flags == 7) {
638 $use_gettext = true;
639}
5c920668 640/* If we can fake gettext, try that */
a2a7852b 641elseif ($gettext_flags == 0) {
642 $use_gettext = true;
e7ab8c9d 643 include_once(SM_PATH . 'functions/gettext.php');
a2a7852b 644} else {
5c920668 645 /* Uh-ho. A weird install */
a2a7852b 646 if (! $gettext_flags & 1) {
03db90bc 647 /**
648 * Function is used as replacement in broken installs
649 * @ignore
650 */
a2a7852b 651 function _($str) {
652 return $str;
653 }
654 }
655 if (! $gettext_flags & 2) {
03db90bc 656 /**
657 * Function is used as replacement in broken installs
658 * @ignore
659 */
a2a7852b 660 function bindtextdomain() {
661 return;
662 }
663 }
664 if (! $gettext_flags & 4) {
03db90bc 665 /**
666 * Function is used as replacemet in broken installs
667 * @ignore
668 */
a2a7852b 669 function textdomain() {
670 return;
671 }
672 }
673}
674
1d33e35e 675
51468260 676/**
1d33e35e 677 * Japanese charset extra function
678 *
51468260 679 * Action performed by function is defined by first argument.
680 * Default return value is defined by second argument.
681 * Use of third argument depends on action.
682 *
a8a1c36d 683 * @param string $action action performed by this function.
51468260 684 * possible values:
03db90bc 685 * decode - convert returned string to euc-jp. third argument unused
686 * encode - convert returned string to jis. third argument unused
687 * strimwidth - third argument=$width. trims string to $width symbols.
688 * encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
689 * decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
690 * downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
691 * wordwrap - third argument=$wrap. wraps text at $wrap symbols
692 * utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
693 * utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
a8a1c36d 694 * @param string $ret default return value
1d33e35e 695 */
696function japanese_charset_xtra() {
697 $ret = func_get_arg(1); /* default return value */
698 if (function_exists('mb_detect_encoding')) {
699 switch (func_get_arg(0)) { /* action */
700 case 'decode':
e842b215 701 $detect_encoding = @mb_detect_encoding($ret);
1d33e35e 702 if ($detect_encoding == 'JIS' ||
703 $detect_encoding == 'EUC-JP' ||
e842b215 704 $detect_encoding == 'SJIS' ||
705 $detect_encoding == 'UTF-8') {
1d33e35e 706
e842b215 707 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
1d33e35e 708 }
709 break;
710 case 'encode':
e842b215 711 $detect_encoding = @mb_detect_encoding($ret);
1d33e35e 712 if ($detect_encoding == 'JIS' ||
713 $detect_encoding == 'EUC-JP' ||
e842b215 714 $detect_encoding == 'SJIS' ||
715 $detect_encoding == 'UTF-8') {
1d33e35e 716
e842b215 717 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
1d33e35e 718 }
719 break;
720 case 'strimwidth':
721 $width = func_get_arg(2);
722 $ret = mb_strimwidth($ret, 0, $width, '...');
723 break;
724 case 'encodeheader':
8ba05cbe 725 $result = '';
726 if (strlen($ret) > 0) {
727 $tmpstr = mb_substr($ret, 0, 1);
728 $prevcsize = strlen($tmpstr);
729 for ($i = 1; $i < mb_strlen($ret); $i++) {
730 $tmp = mb_substr($ret, $i, 1);
731 if (strlen($tmp) == $prevcsize) {
732 $tmpstr .= $tmp;
733 } else {
734 if ($prevcsize == 1) {
735 $result .= $tmpstr;
736 } else {
e842b215 737 $result .= str_replace(' ', '',
738 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
8ba05cbe 739 }
740 $tmpstr = $tmp;
741 $prevcsize = strlen($tmp);
742 }
743 }
744 if (strlen($tmpstr)) {
745 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
746 $result .= $tmpstr;
747 else
e842b215 748 $result .= str_replace(' ', '',
749 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
8ba05cbe 750 }
751 }
752 $ret = $result;
1d33e35e 753 break;
754 case 'decodeheader':
755 $ret = str_replace("\t", "", $ret);
756 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
e842b215 757 $ret = @mb_decode_mimeheader($ret);
758 $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1d33e35e 759 break;
760 case 'downloadfilename':
761 $useragent = func_get_arg(2);
762 if (strstr($useragent, 'Windows') !== false ||
763 strstr($useragent, 'Mac_') !== false) {
764 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
765 } else {
766 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
767}
768 break;
e842b215 769 case 'wordwrap':
770 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
771 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
772 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
773 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
774 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
775 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
776 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
777 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
778 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
779 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
780 $wrap = func_get_arg(2);
781
782 if (strlen($ret) >= $wrap &&
783 substr($ret, 0, 1) != '>' &&
784 strpos($ret, 'http://') === FALSE &&
785 strpos($ret, 'https://') === FALSE &&
786 strpos($ret, 'ftp://') === FALSE) {
787
788 $ret = mb_convert_kana($ret, "KV");
789
790 $line_new = '';
791 $ptr = 0;
792
793 while ($ptr < strlen($ret) - 1) {
794 $l = mb_strcut($ret, $ptr, $wrap);
795 $ptr += strlen($l);
796 $tmp = $l;
797
798 $l = mb_strcut($ret, $ptr, 2);
799 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
800 $tmp .= $l;
801 $ptr += strlen($l);
802 $l = mb_strcut($ret, $ptr, 1);
803 }
804 $line_new .= $tmp;
805 if ($ptr < strlen($ret) - 1)
806 $line_new .= "\n";
807 }
808 $ret = $line_new;
809 }
810 break;
811 case 'utf7-imap_encode':
812 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
813 break;
814 case 'utf7-imap_decode':
815 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
816 break;
1d33e35e 817 }
818 }
819 return $ret;
820}
821
822
51468260 823/**
824 * Korean charset extra functions
825 *
826 * Action performed by function is defined by first argument.
827 * Default return value is defined by second argument.
828 *
829 * @param string action performed by this function.
830 * possible values:
03db90bc 831 * downloadfilename - Hangul(Korean Character) Attached File Name Fix.
51468260 832 * @param string default return value
1d33e35e 833 */
834function korean_charset_xtra() {
835
836 $ret = func_get_arg(1); /* default return value */
837 if (func_get_arg(0) == 'downloadfilename') { /* action */
838 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
839 for ($i=0;$i<strlen($ret);$i++) {
840 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
841 $i++;
842 continue;
843 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
844 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
845 ($ret[$i] == '.') || ($ret[$i] == '-')) {
846 continue;
847 } else {
848 $ret[$i] = '_';
849 }
850 }
851
852 }
1d33e35e 853 return $ret;
854}
855
db08d0c3 856/**
857 * Replaces non-braking spaces inserted by some browsers with regular space
858 *
9af9c0a2 859 * This function can be used to replace non-braking space symbols
860 * that are inserted in forms by some browsers instead of normal
861 * space symbol.
db08d0c3 862 *
863 * @param string $string Text that needs to be cleaned
864 * @param string $charset Charset used in text
865 * @return string Cleaned text
9af9c0a2 866 */
867function cleanup_nbsp($string,$charset) {
868
869 // reduce number of case statements
870 if (stristr('iso-8859-',substr($charset,0,9))){
871 $output_charset="iso-8859-x";
872 }
873 if (stristr('windows-125',substr($charset,0,11))){
874 $output_charset="cp125x";
875 }
876 if (stristr('koi8',substr($charset,0,4))){
877 $output_charset="koi8-x";
878 }
879 if (! isset($output_charset)){
880 $output_charset=strtolower($charset);
881 }
882
883// where is non-braking space symbol
884switch($output_charset):
885 case "iso-8859-x":
97b9c02f 886 case "cp125x":
887 case "iso-2022-jp":
9af9c0a2 888 $nbsp="\xA0";
889 break;
9af9c0a2 890 case "koi8-x":
891 $nbsp="\x9A";
892 break;
893 case "utf-8":
894 $nbsp="\xC2\xA0";
895 break;
9af9c0a2 896 default:
897 // don't change string if charset is unmatched
898 return $string;
899endswitch;
900
901// return space instead of non-braking space.
902 return str_replace($nbsp,' ',$string);
903}
4e519821 904
db08d0c3 905/**
906 * Function informs if it is safe to convert given charset to the one that is used by user.
907 *
908 * It is safe to use conversion only if user uses utf-8 encoding and when
909 * converted charset is similar to the one that is used by user.
910 *
911 * @param string $input_charset Charset of text that needs to be converted
912 * @return bool is it possible to convert to user's charset
913 */
4e519821 914function is_conversion_safe($input_charset) {
915 global $languages, $sm_notAlias, $default_charset;
916
917 // convert to lower case
918 $input_charset = strtolower($input_charset);
919
920 // Is user's locale Unicode based ?
921 if ( $default_charset == "utf-8" ) {
922 return true;
923 }
924
925 // Charsets that are similar
926switch ($default_charset):
927case "windows-1251":
928 if ( $input_charset == "iso-8859-5" ||
03db90bc 929 $input_charset == "koi8-r" ||
930 $input_charset == "koi8-u" ) {
4e519821 931 return true;
932 } else {
933 return false;
934 }
935case "windows-1257":
936 if ( $input_charset == "iso-8859-13" ||
03db90bc 937 $input_charset == "iso-8859-4" ) {
4e519821 938 return true;
939 } else {
940 return false;
941 }
942case "iso-8859-4":
943 if ( $input_charset == "iso-8859-13" ||
03db90bc 944 $input_charset == "windows-1257" ) {
4e519821 945 return true;
946 } else {
947 return false;
948 }
949case "iso-8859-5":
950 if ( $input_charset == "windows-1251" ||
03db90bc 951 $input_charset == "koi8-r" ||
952 $input_charset == "koi8-u" ) {
4e519821 953 return true;
954 } else {
955 return false;
956 }
957case "iso-8859-13":
958 if ( $input_charset == "iso-8859-4" ||
959 $input_charset == "windows-1257" ) {
960 return true;
961 } else {
962 return false;
963 }
964case "koi8-r":
965 if ( $input_charset == "windows-1251" ||
03db90bc 966 $input_charset == "iso-8859-5" ||
967 $input_charset == "koi8-u" ) {
4e519821 968 return true;
969 } else {
970 return false;
971 }
972case "koi8-u":
973 if ( $input_charset == "windows-1251" ||
03db90bc 974 $input_charset == "iso-8859-5" ||
975 $input_charset == "koi8-r" ) {
4e519821 976 return true;
977 } else {
978 return false;
979 }
980default:
981 return false;
982endswitch;
983}
51468260 984?>