copyright update
[squirrelmail.git] / functions / i18n.php
CommitLineData
59177427 1<?php
4b4abf93 2
35586184 3/**
d3bab52e 4 * SquirrelMail internationalization functions
35586184 5 *
35586184 6 * This file contains variuos functions that are needed to do
7 * internationalization of SquirrelMail.
8 *
9 * Internally the output character set is used. Other characters are
10 * encoded using Unicode entities according to HTML 4.0.
11 *
47ccfad4 12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
a8a1c36d 14 * @version $Id$
d6c32258 15 * @package squirrelmail
a8a1c36d 16 * @subpackage i18n
35586184 17 */
18
c6cd3136 19/** @ignore */
20if (! defined('SM_PATH')) define('SM_PATH','../');
21
d6c32258 22/** Everything uses global.php... */
961ca3d8 23require_once(SM_PATH . 'functions/global.php');
24
3b84e1b1 25/**
26 * Gettext bindtextdomain wrapper.
27 *
28 * Wrapper solves differences between php versions in order to provide
29 * ngettext support. Should be used if translation uses ngettext
30 * functions.
81be3f39 31 * @since 1.5.1
3b84e1b1 32 * @param string $domain gettext domain name
33 * @param string $dir directory that contains all translations
34 * @return string path to translation directory
35 */
36function sq_bindtextdomain($domain,$dir) {
37 global $l10n, $gettext_flags, $sm_notAlias;
38
39 if ($gettext_flags==7) {
40 // gettext extension without ngettext
41 if (substr($dir, -1) != '/') $dir .= '/';
42 $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo';
43 $input = new FileReader($mofile);
44 $l10n[$domain] = new gettext_reader($input);
45 }
46
47 $dir=bindtextdomain($domain,$dir);
48
49 return $dir;
50}
51
52/**
53 * Gettext textdomain wrapper.
54 * Makes sure that gettext_domain global is modified.
81be3f39 55 * @since 1.5.1
3b84e1b1 56 * @param string $name gettext domain name
57 * @return string gettext domain name
58 */
59function sq_textdomain($domain) {
60 global $gettext_domain;
61 $gettext_domain=textdomain($domain);
62 return $gettext_domain;
63}
64
1989cc04 65/**
66 * php setlocale function wrapper
67 *
68 * From php 4.3.0 it is possible to use arrays in order to set locale.
69 * php gettext extension works only when locale is set. This wrapper
70 * function allows to use more than one locale name.
71 *
598294a7 72 * @param int $category locale category name. Use php named constants
73 * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME)
1989cc04 74 * @param mixed $locale option contains array with possible locales or string with one locale
75 * @return string name of set locale or false, if all locales fail.
480feeac 76 * @since 1.5.1 and 1.4.5
1989cc04 77 * @see http://www.php.net/setlocale
78 */
79function sq_setlocale($category,$locale) {
80 // string with only one locale
81 if (is_string($locale))
82 return setlocale($category,$locale);
83
84 if (! check_php_version(4,3)) {
85 $ret=false;
86 $index=0;
87 while ( ! $ret && $index<count($locale)) {
88 $ret=setlocale($category,$locale[$index]);
89 $index++;
90 }
91 } else {
92 // php 4.3.0 or better, use entire array
93 $ret=setlocale($category,$locale);
94 }
95 return $ret;
96}
97
d6c32258 98/**
51468260 99 * Converts string from given charset to charset, that can be displayed by user translation.
100 *
101 * Function by default returns html encoded strings, if translation uses different encoding.
102 * If Japanese translation is used - function returns string converted to euc-jp
103 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
104 * If $charset is not supported - function returns unconverted string.
62f7daa5 105 *
51468260 106 * sanitizing of html tags is also done by this function.
107 *
d6c32258 108 * @param string $charset
109 * @param string $string Text to be decoded
f8a1ed5a 110 * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
480feeac 111 * Argument is available since 1.5.1 and 1.4.5.
b6c52e61 112 * @param boolean $save_html disables htmlspecialchars() in order to preserve
113 * html formating. Use with care. Available since 1.5.1
51468260 114 * @return string decoded string
d6c32258 115 */
b6c52e61 116function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
3ec81e63 117 global $languages, $squirrelmail_language, $default_charset;
f03f6ee7 118 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
a2a7852b 119
3714db45 120 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1bb86586 121 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
122 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
6fbd125b 123 }
b05c8961 124
3ec81e63 125 $charset = strtolower($charset);
126
127 set_my_charset();
128
129 // Variables that allow to use functions without function_exist() calls
edf2c0ba 130 if (! isset($use_php_recode) || $use_php_recode=="" ) {
3b84e1b1 131 $use_php_recode=false; }
edf2c0ba 132 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
3b84e1b1 133 $use_php_iconv=false; }
3ec81e63 134
135 // Don't do conversion if charset is the same.
5341ce66 136 if ( ! $force_decode && $charset == strtolower($default_charset) )
4a46b538 137 return ($save_html ? $string : htmlspecialchars($string));
3ec81e63 138
139 // catch iso-8859-8-i thing
140 if ( $charset == "iso-8859-8-i" )
3b84e1b1 141 $charset = "iso-8859-8";
3ec81e63 142
143 /*
62f7daa5 144 * Recode converts html special characters automatically if you use
145 * 'charset..html' decoding. There is no documented way to put -d option
3ec81e63 146 * into php recode function call.
147 */
148 if ( $use_php_recode ) {
3b84e1b1 149 if ( $default_charset == "utf-8" ) {
150 // other charsets can be converted to utf-8 without loss.
151 // and output string is smaller
152 $string = recode_string($charset . "..utf-8",$string);
4a46b538 153 return ($save_html ? $string : htmlspecialchars($string));
3b84e1b1 154 } else {
155 $string = recode_string($charset . "..html",$string);
156 // recode does not convert single quote, htmlspecialchars does.
157 $string = str_replace("'", '&#039;', $string);
b6c52e61 158 // undo html specialchars
159 if ($save_html)
160 $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
161 array('&','"','<','>'),$string);
3b84e1b1 162 return $string;
163 }
3ec81e63 164 }
165
166 // iconv functions does not have html target and can be used only with utf-8
167 if ( $use_php_iconv && $default_charset=='utf-8') {
3b84e1b1 168 $string = iconv($charset,$default_charset,$string);
4a46b538 169 return ($save_html ? $string : htmlspecialchars($string));
3ec81e63 170 }
171
172 // If we don't use recode and iconv, we'll do it old way.
173
a2a7852b 174 /* All HTML special characters are 7 bit and can be replaced first */
b6c52e61 175 if (! $save_html) $string = htmlspecialchars ($string);
a2a7852b 176
5dd23dac 177 /* controls cpu and memory intensive decoding cycles */
f03f6ee7 178 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
3b84e1b1 179 $aggressive_decoding=false; }
5dd23dac 180
b142de74 181 $decode=fixcharset($charset);
182 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
183 if (file_exists($decodefile)) {
3b84e1b1 184 include_once($decodefile);
b6c52e61 185 // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
186 $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
a2a7852b 187 } else {
3b84e1b1 188 $ret = $string;
a2a7852b 189 }
190 return( $ret );
191}
03db90bc 192
d3bab52e 193/**
194 * Converts html string to given charset
480feeac 195 * @since 1.5.1 and 1.4.4
d3bab52e 196 * @param string $string
197 * @param string $charset
78be8403 198 * @param boolean $htmlencode keep htmlspecialchars encoding
62f7daa5 199 * @param string
d3bab52e 200 */
78be8403 201function charset_encode($string,$charset,$htmlencode=true) {
3b84e1b1 202 global $default_charset;
203
3b84e1b1 204 $encode=fixcharset($charset);
205 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
206 if (file_exists($encodefile)) {
207 include_once($encodefile);
208 $ret = call_user_func('charset_encode_'.$encode, $string);
9280dd9d 209 } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) {
210 // function replaces all 8bit html entities with question marks.
211 // it is used when other encoding functions are unavailable
3b84e1b1 212 include_once(SM_PATH . 'functions/encode/us_ascii.php');
213 $ret = charset_encode_us_ascii($string);
9280dd9d 214 } else {
215 /**
216 * fix for yahoo users that remove all us-ascii related things
217 */
218 $ret = $string;
3b84e1b1 219 }
039fee57 220
221 /**
222 * Undo html special chars, some places (like compose form) have
223 * own sanitizing functions and don't need html symbols.
224 * Undo chars only after encoding in order to prevent conversion of
225 * html entities in plain text emails.
226 */
227 if (! $htmlencode ) {
228 $ret = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$ret);
229 }
3b84e1b1 230 return( $ret );
d3bab52e 231}
232
233/**
234 * Combined decoding and encoding functions
235 *
236 * If conversion is done to charset different that utf-8, unsupported symbols
237 * will be replaced with question marks.
480feeac 238 * @since 1.5.1 and 1.4.4
d3bab52e 239 * @param string $in_charset initial charset
240 * @param string $string string that has to be converted
241 * @param string $out_charset final charset
78be8403 242 * @param boolean $htmlencode keep htmlspecialchars encoding
d3bab52e 243 * @return string converted string
244 */
78be8403 245function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
5341ce66 246 $string=charset_decode($in_charset,$string,true);
33991968 247 $string=charset_encode($string,$out_charset,$htmlencode);
248 return $string;
d3bab52e 249}
250
b142de74 251/**
252 * Makes charset name suitable for decoding cycles
253 *
480feeac 254 * @since 1.5.0 and 1.4.4
b142de74 255 * @param string $charset Name of charset
256 * @return string $charset Adjusted name of charset
257 */
258function fixcharset($charset) {
91e0dccc 259 /* remove minus and characters that might be used in paths from charset
dbebf058 260 * name in order to be able to use it in function names and include calls.
261 */
262 $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
62f7daa5 263
c5c521a9 264 // OE ks_c_5601_1987 > cp949
265 $charset=str_replace('ks_c_5601_1987','cp949',$charset);
266 // Moz x-euc-tw > euc-tw
267 $charset=str_replace('x_euc','euc',$charset);
268 // Moz x-windows-949 > cp949
269 $charset=str_replace('x_windows_','cp',$charset);
270
b142de74 271 // windows-125x and cp125x charsets
272 $charset=str_replace('windows_','cp',$charset);
a2a7852b 273
b142de74 274 // ibm > cp
275 $charset=str_replace('ibm','cp',$charset);
276
277 // iso-8859-8-i -> iso-8859-8
278 // use same cycle until I'll find differences
279 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
280
281 return $charset;
282}
a2a7852b 283
51468260 284/**
a2a7852b 285 * Set up the language to be output
286 * if $do_search is true, then scan the browser information
287 * for a possible language that we know
51468260 288 *
62f7daa5 289 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
51468260 290 * gettext translation bindings and html header information.
291 *
5679405c 292 * Function returns error codes, if there is some fatal error.
62f7daa5 293 * 0 = no error,
294 * 1 = mbstring support is not present,
51468260 295 * 2 = mbstring support is not present, user's translation reverted to en_US.
296 *
297 * @param string $sm_language translation used by user's interface
298 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
299 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
62f7daa5 300 * @return int function execution error codes.
a2a7852b 301 */
67a8c90a 302function set_up_language($sm_language, $do_search = false, $default = false) {
a2a7852b 303
304 static $SetupAlready = 0;
9eb0fbd4 305 global $use_gettext, $languages,
fe48c808 306 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
51468260 307 $sm_notAlias, $username, $data_dir;
a2a7852b 308
309 if ($SetupAlready) {
310 return;
311 }
a65846a7 312
5c920668 313 $SetupAlready = TRUE;
961ca3d8 314 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
a2a7852b 315
0d7a48b0 316 /**
317 * If function is asked to detect preferred language
318 * OR squirrelmail default language is set to empty string
319 * AND
f8a1ed5a 320 * squirrelmail language ($sm_language) is empty string
0d7a48b0 321 * (not set in user's prefs and no cookie with language info)
322 * AND
323 * browser provides list of preferred languages
324 * THEN
325 * get preferred language from HTTP_ACCEPT_LANGUAGE header
326 */
f8a1ed5a 327 if (($do_search || empty($squirrelmail_default_language)) &&
328 ! $sm_language &&
0d7a48b0 329 isset($accept_lang)) {
330 // TODO: use more than one language, if first language is not available
331 // FIXME: function assumes that string contains two or more characters.
332 // FIXME: some languages use 5 chars
961ca3d8 333 $sm_language = substr($accept_lang, 0, 2);
a2a7852b 334 }
62f7daa5 335
0d7a48b0 336 /**
337 * If language preference is not set OR script asks to use default language
338 * AND
339 * default squirrelmail language is not set to empty string
340 * THEN
341 * use default squirrelmail language value from configuration.
342 */
f8a1ed5a 343 if ((!$sm_language||$default) &&
0d7a48b0 344 ! empty($squirrelmail_default_language)) {
a2a7852b 345 $squirrelmail_language = $squirrelmail_default_language;
66d7950f 346 $sm_language = $squirrelmail_default_language;
a2a7852b 347 }
7be4d717 348
349 /** provide failsafe language when detection fails */
350 if (! $sm_language) $sm_language='en_US';
351
a2a7852b 352 $sm_notAlias = $sm_language;
62f7daa5 353
3ec81e63 354 // Catching removed translation
355 // System reverts to English translation if user prefs contain translation
2ba706ef 356 // that is not available in $languages array
3ec81e63 357 if (!isset($languages[$sm_notAlias])) {
3b84e1b1 358 $sm_notAlias="en_US";
3ec81e63 359 }
360
a2a7852b 361 while (isset($languages[$sm_notAlias]['ALIAS'])) {
362 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
363 }
364
88cb1b4d 365 if ( isset($sm_language) &&
5c920668 366 $use_gettext &&
367 $sm_language != '' &&
368 isset($languages[$sm_notAlias]['CHARSET']) ) {
3b84e1b1 369 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
370 sq_textdomain( 'squirrelmail' );
1989cc04 371
372 // set codeset in order to avoid gettext charset conversions
03db90bc 373 if (function_exists('bind_textdomain_codeset')) {
1989cc04 374 // Japanese translation uses different internal charset
3b84e1b1 375 if ($sm_notAlias == 'ja_JP') {
598294a7 376 bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
a5970d71 377 } else {
598294a7 378 bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
03db90bc 379 }
380 }
1989cc04 381
382 // Use LOCALE key, if it is set.
03db90bc 383 if (isset($languages[$sm_notAlias]['LOCALE'])){
3b84e1b1 384 $longlocale=$languages[$sm_notAlias]['LOCALE'];
03db90bc 385 } else {
3b84e1b1 386 $longlocale=$sm_notAlias;
03db90bc 387 }
1989cc04 388
389 // try setting locale
390 $retlocale=sq_setlocale(LC_ALL, $longlocale);
391
392 // check if locale is set and assign that locale to $longlocale
393 // in order to use it in putenv calls.
394 if (! is_bool($retlocale)) {
395 $longlocale=$retlocale;
396 } elseif (is_array($longlocale)) {
397 // setting of all locales failed.
398 // we need string instead of array used in LOCALE key.
399 $longlocale=$sm_notAlias;
400 }
401
5a4689fb 402 if ( !((bool)ini_get('safe_mode')) &&
f2374580 403 getenv( 'LC_ALL' ) != $longlocale ) {
404 putenv( "LC_ALL=$longlocale" );
405 putenv( "LANG=$longlocale" );
406 putenv( "LANGUAGE=$longlocale" );
54b168cf 407 putenv( "LC_NUMERIC=C" );
475df436 408 if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" );
a2a7852b 409 }
54b168cf 410 // Workaround for plugins that use numbers with floating point
91e0dccc 411 // It might be removed if plugins use correct decimal delimiters
412 // according to locale settings.
54b168cf 413 setlocale(LC_NUMERIC, 'C');
414 // Workaround for specific Turkish strtolower/strtoupper rules.
415 // Many functions expect English conversion rules.
475df436 416 if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C');
03db90bc 417
418 // Set text direction/alignment variables
8d8da447 419 // These don't appear to be used... are they safe to remove?
62f7daa5 420 if (isset($languages[$sm_notAlias]['DIR']) &&
03db90bc 421 $languages[$sm_notAlias]['DIR'] == 'rtl') {
3b84e1b1 422 /**
423 * Text direction
424 * @global string $text_direction
425 */
426 $text_direction='rtl';
427 /**
428 * Left alignment
429 * @global string $left_align
430 */
431 $left_align='right';
432 /**
433 * Right alignment
434 * @global string $right_align
435 */
436 $right_align='left';
03db90bc 437 } else {
3b84e1b1 438 $text_direction='ltr';
439 $left_align='left';
440 $right_align='right';
03db90bc 441 }
442
443 $squirrelmail_language = $sm_notAlias;
a5970d71 444 if ($squirrelmail_language == 'ja_JP') {
b05c8961 445 header ('Content-Type: text/html; charset=EUC-JP');
446 if (!function_exists('mb_internal_encoding')) {
3b84e1b1 447 // Error messages can't be displayed here
448 $error = 1;
449 // Revert to English if possible.
450 if (function_exists('setPref') && $username!='' && $data_dir!="") {
451 setPref($data_dir, $username, 'language', "en_US");
452 $error = 2;
453 }
454 // stop further execution in order not to get php errors on mb_internal_encoding().
455 return $error;
e842b215 456 }
457 if (function_exists('mb_language')) {
458 mb_language('Japanese');
b05c8961 459 }
460 mb_internal_encoding('EUC-JP');
461 mb_http_output('pass');
fe48c808 462 } elseif ($squirrelmail_language == 'en_US') {
463 header( 'Content-Type: text/html; charset=' . $default_charset );
b05c8961 464 } else {
fe48c808 465 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
466 }
f5c507dc 467 /**
468 * mbstring.func_overload fix (#929644).
469 *
f8a1ed5a 470 * php mbstring extension can replace standard string functions with their multibyte
4c12c858 471 * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature
472 * was added in php v.4.2.0
f5c507dc 473 *
474 * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced
475 * to use mbstring functions and mbstring internal encoding is set to multibyte charset,
f8a1ed5a 476 * interface can't trust regular string functions. Due to mbstring overloading design
f5c507dc 477 * limits php scripts can't control this setting.
478 *
479 * This hack should fix some issues related to 8bit strings in passwords. Correct fix is
480 * to disable mbstring overloading. Japanese translation uses different internal encoding.
481 */
f8a1ed5a 482 if ($squirrelmail_language != 'ja_JP' &&
f5c507dc 483 function_exists('mb_internal_encoding') &&
484 check_php_version(4,2,0) &&
485 (int)ini_get('mbstring.func_overload')!=0) {
fa4cfb07 486 mb_internal_encoding('pass');
f5c507dc 487 }
a2a7852b 488 }
51468260 489 return 0;
b05c8961 490}
a2a7852b 491
51468260 492/**
493 * Sets default_charset variable according to the one that is used by user's translations.
494 *
495 * Function changes global $default_charset variable in order to be sure, that it
0d7a48b0 496 * contains charset used by user's translation. Sanity of $squirrelmail_language
497 * and $default_charset combination is also tested.
51468260 498 *
499 * There can be a $default_charset setting in the
500 * config.php file, but the user may have a different language
501 * selected for a user interface. This function checks the
502 * language selected by the user and tags the outgoing messages
503 * with the appropriate charset corresponding to the language
504 * selection. This is "more right" (tm), than just stamping the
505 * message blindly with the system-wide $default_charset.
506 */
a2a7852b 507function set_my_charset(){
0d7a48b0 508 global $data_dir, $username, $default_charset, $languages, $squirrelmail_language;
88cb1b4d 509
a2a7852b 510 $my_language = getPref($data_dir, $username, 'language');
5c920668 511 if (!$my_language) {
0d7a48b0 512 $my_language = $squirrelmail_language ;
5c920668 513 }
3ec81e63 514 // Catch removed translation
515 if (!isset($languages[$my_language])) {
3b84e1b1 516 $my_language="en_US";
3ec81e63 517 }
a2a7852b 518 while (isset($languages[$my_language]['ALIAS'])) {
f7e8861e 519 $my_language = $languages[$my_language]['ALIAS'];
a2a7852b 520 }
5c920668 521 $my_charset = $languages[$my_language]['CHARSET'];
fe48c808 522 if ($my_language!='en_US') {
a2a7852b 523 $default_charset = $my_charset;
524 }
525}
526
e0e1b169 527/**
528 * Replaces non-braking spaces inserted by some browsers with regular space
529 *
530 * This function can be used to replace non-braking space symbols
531 * that are inserted in forms by some browsers instead of normal
532 * space symbol.
533 *
534 * @param string $string Text that needs to be cleaned
535 * @param string $charset Charset used in text
536 * @return string Cleaned text
537 */
538function cleanup_nbsp($string,$charset) {
a8fa8e33 539
e0e1b169 540 // reduce number of case statements
541 if (stristr('iso-8859-',substr($charset,0,9))){
542 $output_charset="iso-8859-x";
543 }
544 if (stristr('windows-125',substr($charset,0,11))){
545 $output_charset="cp125x";
546 }
547 if (stristr('koi8',substr($charset,0,4))){
548 $output_charset="koi8-x";
549 }
550 if (! isset($output_charset)){
551 $output_charset=strtolower($charset);
552 }
a8fa8e33 553
e0e1b169 554// where is non-braking space symbol
555switch($output_charset):
556 case "iso-8859-x":
557 case "cp125x":
558 case "iso-2022-jp":
559 $nbsp="\xA0";
560 break;
561 case "koi8-x":
562 $nbsp="\x9A";
563 break;
564 case "utf-8":
565 $nbsp="\xC2\xA0";
566 break;
567 default:
568 // don't change string if charset is unmatched
569 return $string;
570endswitch;
a8fa8e33 571
e0e1b169 572// return space instead of non-braking space.
573 return str_replace($nbsp,' ',$string);
574}
a8fa8e33 575
e0e1b169 576/**
577 * Function informs if it is safe to convert given charset to the one that is used by user.
578 *
579 * It is safe to use conversion only if user uses utf-8 encoding and when
580 * converted charset is similar to the one that is used by user.
581 *
582 * @param string $input_charset Charset of text that needs to be converted
583 * @return bool is it possible to convert to user's charset
584 */
585function is_conversion_safe($input_charset) {
586 global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
a8fa8e33 587
e0e1b169 588 if (isset($lossy_encoding) && $lossy_encoding )
91e0dccc 589 return true;
a8fa8e33 590
e0e1b169 591 // convert to lower case
592 $input_charset = strtolower($input_charset);
a8fa8e33 593
e0e1b169 594 // Is user's locale Unicode based ?
595 if ( $default_charset == "utf-8" ) {
596 return true;
597 }
a8fa8e33 598
e0e1b169 599 // Charsets that are similar
600switch ($default_charset):
601case "windows-1251":
602 if ( $input_charset == "iso-8859-5" ||
603 $input_charset == "koi8-r" ||
604 $input_charset == "koi8-u" ) {
605 return true;
606 } else {
607 return false;
608 }
609case "windows-1257":
610 if ( $input_charset == "iso-8859-13" ||
611 $input_charset == "iso-8859-4" ) {
612 return true;
613 } else {
614 return false;
615 }
616case "iso-8859-4":
617 if ( $input_charset == "iso-8859-13" ||
618 $input_charset == "windows-1257" ) {
619 return true;
620 } else {
621 return false;
622 }
623case "iso-8859-5":
624 if ( $input_charset == "windows-1251" ||
625 $input_charset == "koi8-r" ||
626 $input_charset == "koi8-u" ) {
627 return true;
628 } else {
629 return false;
630 }
631case "iso-8859-13":
632 if ( $input_charset == "iso-8859-4" ||
633 $input_charset == "windows-1257" ) {
634 return true;
635 } else {
636 return false;
637 }
638case "koi8-r":
639 if ( $input_charset == "windows-1251" ||
640 $input_charset == "iso-8859-5" ||
641 $input_charset == "koi8-u" ) {
642 return true;
643 } else {
644 return false;
645 }
646case "koi8-u":
647 if ( $input_charset == "windows-1251" ||
648 $input_charset == "iso-8859-5" ||
649 $input_charset == "koi8-r" ) {
650 return true;
651 } else {
652 return false;
653 }
654default:
655 return false;
656endswitch;
657}
551a09c7 658
a8fa8e33 659
e0e1b169 660/* ------------------------------ main --------------------------- */
a8fa8e33 661
e0e1b169 662global $squirrelmail_language, $languages, $use_gettext;
060c9483 663
0d7a48b0 664if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
e0e1b169 665 $squirrelmail_language = '';
666}
c30be3cf 667
e0e1b169 668/**
669 * Array specifies the available translations.
670 *
671 * Structure of array:
672 * $languages['language']['variable'] = 'value'
673 *
674 * Possible 'variable' names:
675 * NAME - Translation name in English
676 * CHARSET - Encoding used by translation
677 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
678 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
c6cd3136 679 * LOCALE - Full locale name (in xx_XX.charset format). It can use array with more than one locale name since 1.4.5 and 1.5.1
e0e1b169 680 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
681 * XTRA_CODE - translation uses special functions. See doc/i18n.txt
682 *
683 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
684 *
685 * @name $languages
686 * @global array $languages
687 */
e0e1b169 688$languages['en_US']['NAME'] = 'English';
689$languages['en_US']['CHARSET'] = 'iso-8859-1';
690$languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
691$languages['en']['ALIAS'] = 'en_US';
a2a7852b 692
89b3545c 693/**
694 * Automatic translation loading from setup.php files.
695 * Solution for bug. 1240889.
696 * setup.php file can contain $languages array entries and XTRA_CODE functions.
89b3545c 697 */
6c8a387f 698if (is_dir(SM_PATH . 'locale') &&
699 is_readable(SM_PATH . 'locale')) {
700 $localedir = dir(SM_PATH . 'locale');
701 while($lang_dir=$localedir->read()) {
702 // remove trailing slash, if present
703 if (substr($lang_dir,-1)=='/') {
704 $lang_dir = substr($lang_dir,0,-1);
705 }
706 if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
707 is_dir(SM_PATH.'locale/'.$lang_dir) &&
708 file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
709 include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
710 }
89b3545c 711 }
6c8a387f 712 $localedir->close();
89b3545c 713}
89b3545c 714
e0e1b169 715/* Detect whether gettext is installed. */
716$gettext_flags = 0;
717if (function_exists('_')) {
718 $gettext_flags += 1;
719}
720if (function_exists('bindtextdomain')) {
721 $gettext_flags += 2;
722}
723if (function_exists('textdomain')) {
724 $gettext_flags += 4;
725}
726if (function_exists('ngettext')) {
727 $gettext_flags += 8;
728}
729
730/* If gettext is fully loaded, cool */
731if ($gettext_flags == 15) {
732 $use_gettext = true;
733}
734
735/* If ngettext support is missing, load it */
736elseif ($gettext_flags == 7) {
737 $use_gettext = true;
738 // load internal ngettext functions
739 include_once(SM_PATH . 'class/l10n.class.php');
740 include_once(SM_PATH . 'functions/ngettext.php');
741}
742
743/* If we can fake gettext, try that */
744elseif ($gettext_flags == 0) {
745 $use_gettext = true;
746 include_once(SM_PATH . 'functions/gettext.php');
747} else {
748 /* Uh-ho. A weird install */
749 if (! $gettext_flags & 1) {
750 /**
751 * Function is used as replacement in broken installs
752 * @ignore
753 */
754 function _($str) {
755 return $str;
756 }
757 }
758 if (! $gettext_flags & 2) {
759 /**
760 * Function is used as replacement in broken installs
761 * @ignore
762 */
763 function bindtextdomain() {
764 return;
765 }
766 }
767 if (! $gettext_flags & 4) {
768 /**
769 * Function is used as replacemet in broken installs
770 * @ignore
771 */
772 function textdomain() {
773 return;
774 }
775 }
776 if (! $gettext_flags & 8) {
777 /**
778 * Function is used as replacemet in broken installs
779 * @ignore
780 */
781 function ngettext($str,$str2,$number) {
782 if ($number>1) {
783 return $str2;
784 } else {
785 return $str;
786 }
787 }
788 }
a9a10f57 789 if (! function_exists('dgettext')) {
790 /**
791 * Replacement for broken setups.
792 * @ignore
793 */
794 function dgettext($domain,$str) {
795 return $str;
796 }
797 }
798 if (! function_exists('dngettext')) {
799 /**
800 * Replacement for broken setups
801 * @ignore
802 */
803 function dngettext($domain,$str1,$strn,$number) {
804 return ($number==1 ? $str1 : $strn);
805 }
806 }
4e519821 807}
51468260 808?>