removing trailing ?> from function scripts
[squirrelmail.git] / include / languages.php
1 <?php
2
3 /**
4 * SquirrelMail internationalization functions
5 *
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 *
12 * Before 1.5.2 functions were stored in functions/i18n.php. Script is moved
13 * because it executes some code in order to detect functions supported by
14 * existing PHP installation and implements fallback functions when required
15 * functions are not available. Scripts in functions/ directory should not
16 * setup anything when they are loaded.
17 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
18 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
19 * @version $Id$
20 * @package squirrelmail
21 * @subpackage i18n
22 */
23
24
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.
31 * @since 1.5.1
32 * @param string $domain gettext domain name
33 * @param string $dir directory that contains all translations
34 * @return string path to translation directory
35 */
36 function 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.
55 * @since 1.5.1
56 * @param string $name gettext domain name
57 * @return string gettext domain name
58 */
59 function sq_textdomain($domain) {
60 global $gettext_domain;
61 $gettext_domain=textdomain($domain);
62 return $gettext_domain;
63 }
64
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 *
72 * @param int $category locale category name. Use php named constants
73 * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME)
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.
76 * @since 1.5.1 and 1.4.5
77 * @see http://www.php.net/setlocale
78 */
79 function sq_setlocale($category,$locale) {
80 if (is_string($locale)) {
81 // string with only one locale
82 $ret = setlocale($category,$locale);
83 } elseif (! check_php_version(4,3)) {
84 // older php version (second setlocale argument must be string)
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
96 /* safety checks */
97 if (preg_match("/^.*\/.*\/.*\/.*\/.*\/.*$/",$ret)) {
98 /**
99 * Welcome to We-Don't-Follow-Own-Fine-Manual department
100 * OpenBSD 3.8, 3.9-current and maybe later versions
101 * return invalid response to setlocale command.
102 * SM bug report #1427512.
103 */
104 $ret = false;
105 }
106 return $ret;
107 }
108
109 /**
110 * Converts string from given charset to charset, that can be displayed by user translation.
111 *
112 * Function by default returns html encoded strings, if translation uses different encoding.
113 * If Japanese translation is used - function returns string converted to euc-jp
114 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
115 * If $charset is not supported - function returns unconverted string.
116 *
117 * sanitizing of html tags is also done by this function.
118 *
119 * @param string $charset
120 * @param string $string Text to be decoded
121 * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
122 * Argument is available since 1.5.1 and 1.4.5.
123 * @param boolean $save_html disables htmlspecialchars() in order to preserve
124 * html formating. Use with care. Available since 1.5.1
125 * @return string decoded string
126 */
127 function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
128 global $languages, $squirrelmail_language, $default_charset;
129 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
130
131 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
132 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
133 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
134 }
135
136 $charset = strtolower($charset);
137
138 set_my_charset();
139
140 // Variables that allow to use functions without function_exist() calls
141 if (! isset($use_php_recode) || $use_php_recode=="" ) {
142 $use_php_recode=false; }
143 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
144 $use_php_iconv=false; }
145
146 // Don't do conversion if charset is the same.
147 if ( ! $force_decode && $charset == strtolower($default_charset) )
148 return ($save_html ? $string : htmlspecialchars($string));
149
150 // catch iso-8859-8-i thing
151 if ( $charset == "iso-8859-8-i" )
152 $charset = "iso-8859-8";
153
154 /*
155 * Recode converts html special characters automatically if you use
156 * 'charset..html' decoding. There is no documented way to put -d option
157 * into php recode function call.
158 */
159 if ( $use_php_recode ) {
160 if ( $default_charset == "utf-8" ) {
161 // other charsets can be converted to utf-8 without loss.
162 // and output string is smaller
163 $string = recode_string($charset . "..utf-8",$string);
164 return ($save_html ? $string : htmlspecialchars($string));
165 } else {
166 $string = recode_string($charset . "..html",$string);
167 // recode does not convert single quote, htmlspecialchars does.
168 $string = str_replace("'", '&#039;', $string);
169 // undo html specialchars
170 if ($save_html)
171 $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
172 array('&','"','<','>'),$string);
173 return $string;
174 }
175 }
176
177 // iconv functions does not have html target and can be used only with utf-8
178 if ( $use_php_iconv && $default_charset=='utf-8') {
179 $string = iconv($charset,$default_charset,$string);
180 return ($save_html ? $string : htmlspecialchars($string));
181 }
182
183 // If we don't use recode and iconv, we'll do it old way.
184
185 /* All HTML special characters are 7 bit and can be replaced first */
186 if (! $save_html) $string = htmlspecialchars ($string);
187
188 /* controls cpu and memory intensive decoding cycles */
189 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
190 $aggressive_decoding=false; }
191
192 $decode=fixcharset($charset);
193 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
194 if (file_exists($decodefile)) {
195 include_once($decodefile);
196 // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
197 $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
198 } else {
199 $ret = $string;
200 }
201 return( $ret );
202 }
203
204 /**
205 * Converts html string to given charset
206 * @since 1.5.1 and 1.4.4
207 * @param string $string
208 * @param string $charset
209 * @param boolean $htmlencode keep htmlspecialchars encoding
210 * @param string
211 */
212 function charset_encode($string,$charset,$htmlencode=true) {
213 global $default_charset;
214
215 $encode=fixcharset($charset);
216 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
217 if (file_exists($encodefile)) {
218 include_once($encodefile);
219 $ret = call_user_func('charset_encode_'.$encode, $string);
220 } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) {
221 // function replaces all 8bit html entities with question marks.
222 // it is used when other encoding functions are unavailable
223 include_once(SM_PATH . 'functions/encode/us_ascii.php');
224 $ret = charset_encode_us_ascii($string);
225 } else {
226 /**
227 * fix for yahoo users that remove all us-ascii related things
228 */
229 $ret = $string;
230 }
231
232 /**
233 * Undo html special chars, some places (like compose form) have
234 * own sanitizing functions and don't need html symbols.
235 * Undo chars only after encoding in order to prevent conversion of
236 * html entities in plain text emails.
237 */
238 if (! $htmlencode ) {
239 $ret = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$ret);
240 }
241 return( $ret );
242 }
243
244 /**
245 * Combined decoding and encoding functions
246 *
247 * If conversion is done to charset different that utf-8, unsupported symbols
248 * will be replaced with question marks.
249 * @since 1.5.1 and 1.4.4
250 * @param string $in_charset initial charset
251 * @param string $string string that has to be converted
252 * @param string $out_charset final charset
253 * @param boolean $htmlencode keep htmlspecialchars encoding
254 * @return string converted string
255 */
256 function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
257 $string=charset_decode($in_charset,$string,true);
258 $string=sqi18n_convert_entities($string);
259 $string=charset_encode($string,$out_charset,$htmlencode);
260 return $string;
261 }
262
263 /**
264 * Makes charset name suitable for decoding cycles
265 *
266 * @since 1.5.0 and 1.4.4
267 * @param string $charset Name of charset
268 * @return string $charset Adjusted name of charset
269 */
270 function fixcharset($charset) {
271 /* remove minus and characters that might be used in paths from charset
272 * name in order to be able to use it in function names and include calls.
273 */
274 $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
275
276 // OE ks_c_5601_1987 > cp949
277 $charset=str_replace('ks_c_5601_1987','cp949',$charset);
278 // Moz x-euc-tw > euc-tw
279 $charset=str_replace('x_euc','euc',$charset);
280 // Moz x-windows-949 > cp949
281 $charset=str_replace('x_windows_','cp',$charset);
282
283 // windows-125x and cp125x charsets
284 $charset=str_replace('windows_','cp',$charset);
285
286 // ibm > cp
287 $charset=str_replace('ibm','cp',$charset);
288
289 // iso-8859-8-i -> iso-8859-8
290 // use same cycle until I'll find differences
291 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
292
293 return $charset;
294 }
295
296 /**
297 * Set up the language to be output
298 * if $do_search is true, then scan the browser information
299 * for a possible language that we know
300 *
301 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
302 * gettext translation bindings and html header information.
303 *
304 * Function returns error codes, if there is some fatal error.
305 * 0 = no error,
306 * 1 = mbstring support is not present,
307 * 2 = mbstring support is not present, user's translation reverted to en_US.
308 *
309 * @param string $sm_language translation used by user's interface
310 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
311 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
312 * @return int function execution error codes.
313 */
314 function set_up_language($sm_language, $do_search = false, $default = false) {
315
316 static $SetupAlready = 0;
317 global $use_gettext, $languages,
318 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
319 $sm_notAlias, $username, $data_dir;
320
321 if ($SetupAlready) {
322 return;
323 }
324
325 $SetupAlready = TRUE;
326 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
327
328 /**
329 * If function is asked to detect preferred language
330 * OR squirrelmail default language is set to empty string
331 * AND
332 * squirrelmail language ($sm_language) is empty string
333 * (not set in user's prefs and no cookie with language info)
334 * AND
335 * browser provides list of preferred languages
336 * THEN
337 * get preferred language from HTTP_ACCEPT_LANGUAGE header
338 */
339 if (($do_search || empty($squirrelmail_default_language)) &&
340 ! $sm_language &&
341 isset($accept_lang)) {
342 // TODO: use more than one language, if first language is not available
343 // FIXME: function assumes that string contains two or more characters.
344 // FIXME: some languages use 5 chars
345 $sm_language = substr($accept_lang, 0, 2);
346 }
347
348 /**
349 * If language preference is not set OR script asks to use default language
350 * AND
351 * default squirrelmail language is not set to empty string
352 * THEN
353 * use default squirrelmail language value from configuration.
354 */
355 if ((!$sm_language||$default) &&
356 ! empty($squirrelmail_default_language)) {
357 $squirrelmail_language = $squirrelmail_default_language;
358 $sm_language = $squirrelmail_default_language;
359 }
360
361 /** provide failsafe language when detection fails */
362 if (! $sm_language) $sm_language='en_US';
363
364 $sm_notAlias = $sm_language;
365
366 // Catching removed translation
367 // System reverts to English translation if user prefs contain translation
368 // that is not available in $languages array
369 if (!isset($languages[$sm_notAlias])) {
370 $sm_notAlias="en_US";
371 }
372
373 while (isset($languages[$sm_notAlias]['ALIAS'])) {
374 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
375 }
376
377 if ( isset($sm_language) &&
378 $use_gettext &&
379 $sm_language != '' &&
380 isset($languages[$sm_notAlias]['CHARSET']) ) {
381 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
382 sq_textdomain( 'squirrelmail' );
383
384 // set codeset in order to avoid gettext charset conversions
385 if (function_exists('bind_textdomain_codeset')) {
386 // Japanese translation uses different internal charset
387 if ($sm_notAlias == 'ja_JP') {
388 bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
389 } else {
390 bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
391 }
392 }
393
394 // Use LOCALE key, if it is set.
395 if (isset($languages[$sm_notAlias]['LOCALE'])){
396 $longlocale=$languages[$sm_notAlias]['LOCALE'];
397 } else {
398 $longlocale=$sm_notAlias;
399 }
400
401 // try setting locale
402 $retlocale=sq_setlocale(LC_ALL, $longlocale);
403
404 // check if locale is set and assign that locale to $longlocale
405 // in order to use it in putenv calls.
406 if (! is_bool($retlocale)) {
407 $longlocale=$retlocale;
408 } elseif (is_array($longlocale)) {
409 // setting of all locales failed.
410 // we need string instead of array used in LOCALE key.
411 $longlocale=$sm_notAlias;
412 }
413
414 if ( !((bool)ini_get('safe_mode')) &&
415 getenv( 'LC_ALL' ) != $longlocale ) {
416 putenv( "LC_ALL=$longlocale" );
417 putenv( "LANG=$longlocale" );
418 putenv( "LANGUAGE=$longlocale" );
419 putenv( "LC_NUMERIC=C" );
420 if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" );
421 }
422 // Workaround for plugins that use numbers with floating point
423 // It might be removed if plugins use correct decimal delimiters
424 // according to locale settings.
425 setlocale(LC_NUMERIC, 'C');
426 // Workaround for specific Turkish strtolower/strtoupper rules.
427 // Many functions expect English conversion rules.
428 if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C');
429
430 /**
431 * Set text direction/alignment variables
432 * When language environment is setup, scripts can use these globals
433 * without accessing $languages directly and making checks for optional
434 * array key.
435 */
436 global $text_direction, $left_align, $right_align;
437 if (isset($languages[$sm_notAlias]['DIR']) &&
438 $languages[$sm_notAlias]['DIR'] == 'rtl') {
439 /**
440 * Text direction
441 * @global string $text_direction
442 */
443 $text_direction='rtl';
444 /**
445 * Left alignment
446 * @global string $left_align
447 */
448 $left_align='right';
449 /**
450 * Right alignment
451 * @global string $right_align
452 */
453 $right_align='left';
454 } else {
455 $text_direction='ltr';
456 $left_align='left';
457 $right_align='right';
458 }
459
460 $squirrelmail_language = $sm_notAlias;
461 if ($squirrelmail_language == 'ja_JP') {
462 header ('Content-Type: text/html; charset=EUC-JP');
463 if (!function_exists('mb_internal_encoding')) {
464 // Error messages can't be displayed here
465 $error = 1;
466 // Revert to English if possible.
467 if (function_exists('setPref') && $username!='' && $data_dir!="") {
468 setPref($data_dir, $username, 'language', "en_US");
469 $error = 2;
470 }
471 // stop further execution in order not to get php errors on mb_internal_encoding().
472 return $error;
473 }
474 if (function_exists('mb_language')) {
475 mb_language('Japanese');
476 }
477 mb_internal_encoding('EUC-JP');
478 mb_http_output('pass');
479 } elseif ($squirrelmail_language == 'en_US') {
480 header( 'Content-Type: text/html; charset=' . $default_charset );
481 } else {
482 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
483 }
484 /**
485 * mbstring.func_overload fix (#929644).
486 *
487 * php mbstring extension can replace standard string functions with their multibyte
488 * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature
489 * was added in php v.4.2.0
490 *
491 * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced
492 * to use mbstring functions and mbstring internal encoding is set to multibyte charset,
493 * interface can't trust regular string functions. Due to mbstring overloading design
494 * limits php scripts can't control this setting.
495 *
496 * This hack should fix some issues related to 8bit strings in passwords. Correct fix is
497 * to disable mbstring overloading. Japanese translation uses different internal encoding.
498 */
499 if ($squirrelmail_language != 'ja_JP' &&
500 function_exists('mb_internal_encoding') &&
501 check_php_version(4,2,0) &&
502 (int)ini_get('mbstring.func_overload')!=0) {
503 mb_internal_encoding('pass');
504 }
505 }
506 return 0;
507 }
508
509 /**
510 * Sets default_charset variable according to the one that is used by user's translations.
511 *
512 * Function changes global $default_charset variable in order to be sure, that it
513 * contains charset used by user's translation. Sanity of $squirrelmail_language
514 * and $default_charset combination is also tested.
515 *
516 * There can be a $default_charset setting in the
517 * config.php file, but the user may have a different language
518 * selected for a user interface. This function checks the
519 * language selected by the user and tags the outgoing messages
520 * with the appropriate charset corresponding to the language
521 * selection. This is "more right" (tm), than just stamping the
522 * message blindly with the system-wide $default_charset.
523 */
524 function set_my_charset(){
525 global $data_dir, $username, $default_charset, $languages, $squirrelmail_language;
526
527 $my_language = getPref($data_dir, $username, 'language');
528 if (!$my_language) {
529 $my_language = $squirrelmail_language ;
530 }
531 // Catch removed translation
532 if (!isset($languages[$my_language])) {
533 $my_language="en_US";
534 }
535 while (isset($languages[$my_language]['ALIAS'])) {
536 $my_language = $languages[$my_language]['ALIAS'];
537 }
538 $my_charset = $languages[$my_language]['CHARSET'];
539 if ($my_language!='en_US') {
540 $default_charset = $my_charset;
541 }
542 }
543
544 /**
545 * Replaces non-braking spaces inserted by some browsers with regular space
546 *
547 * This function can be used to replace non-braking space symbols
548 * that are inserted in forms by some browsers instead of normal
549 * space symbol.
550 *
551 * @param string $string Text that needs to be cleaned
552 * @param string $charset Charset used in text
553 * @return string Cleaned text
554 */
555 function cleanup_nbsp($string,$charset) {
556
557 // reduce number of case statements
558 if (stristr('iso-8859-',substr($charset,0,9))){
559 $output_charset="iso-8859-x";
560 }
561 if (stristr('windows-125',substr($charset,0,11))){
562 $output_charset="cp125x";
563 }
564 if (stristr('koi8',substr($charset,0,4))){
565 $output_charset="koi8-x";
566 }
567 if (! isset($output_charset)){
568 $output_charset=strtolower($charset);
569 }
570
571 // where is non-braking space symbol
572 switch($output_charset):
573 case "iso-8859-x":
574 case "cp125x":
575 case "iso-2022-jp":
576 $nbsp="\xA0";
577 break;
578 case "koi8-x":
579 $nbsp="\x9A";
580 break;
581 case "utf-8":
582 $nbsp="\xC2\xA0";
583 break;
584 default:
585 // don't change string if charset is unmatched
586 return $string;
587 endswitch;
588
589 // return space instead of non-braking space.
590 return str_replace($nbsp,' ',$string);
591 }
592
593 /**
594 * Function informs if it is safe to convert given charset to the one that is used by user.
595 *
596 * It is safe to use conversion only if user uses utf-8 encoding and when
597 * converted charset is similar to the one that is used by user.
598 *
599 * @param string $input_charset Charset of text that needs to be converted
600 * @return bool is it possible to convert to user's charset
601 */
602 function is_conversion_safe($input_charset) {
603 global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
604
605 if (isset($lossy_encoding) && $lossy_encoding )
606 return true;
607
608 // convert to lower case
609 $input_charset = strtolower($input_charset);
610
611 // Is user's locale Unicode based ?
612 if ( $default_charset == "utf-8" ) {
613 return true;
614 }
615
616 // Charsets that are similar
617 switch ($default_charset) {
618 case "windows-1251":
619 if ( $input_charset == "iso-8859-5" ||
620 $input_charset == "koi8-r" ||
621 $input_charset == "koi8-u" ) {
622 return true;
623 } else {
624 return false;
625 }
626 case "windows-1257":
627 if ( $input_charset == "iso-8859-13" ||
628 $input_charset == "iso-8859-4" ) {
629 return true;
630 } else {
631 return false;
632 }
633 case "iso-8859-4":
634 if ( $input_charset == "iso-8859-13" ||
635 $input_charset == "windows-1257" ) {
636 return true;
637 } else {
638 return false;
639 }
640 case "iso-8859-5":
641 if ( $input_charset == "windows-1251" ||
642 $input_charset == "koi8-r" ||
643 $input_charset == "koi8-u" ) {
644 return true;
645 } else {
646 return false;
647 }
648 case "iso-8859-13":
649 if ( $input_charset == "iso-8859-4" ||
650 $input_charset == "windows-1257" ) {
651 return true;
652 } else {
653 return false;
654 }
655 case "koi8-r":
656 if ( $input_charset == "windows-1251" ||
657 $input_charset == "iso-8859-5" ||
658 $input_charset == "koi8-u" ) {
659 return true;
660 } else {
661 return false;
662 }
663 case "koi8-u":
664 if ( $input_charset == "windows-1251" ||
665 $input_charset == "iso-8859-5" ||
666 $input_charset == "koi8-r" ) {
667 return true;
668 } else {
669 return false;
670 }
671 default:
672 return false;
673 }
674 }
675
676 /**
677 * Converts html character entities to numeric entities
678 *
679 * SquirrelMail encoding functions work only with numeric entities.
680 * This function fixes issues with decoding functions that might convert
681 * some symbols to character entities. Issue is specific to PHP recode
682 * extension decoding. Function is used internally in charset_convert()
683 * function.
684 * @param string $str string that might contain html character entities
685 * @return string string with character entities converted to decimals.
686 * @since 1.5.2
687 */
688 function sqi18n_convert_entities($str) {
689
690 $entities = array(
691 // Latin 1
692 '&nbsp;' => '&#160;',
693 '&iexcl;' => '&#161;',
694 '&cent;' => '&#162;',
695 '&pound;' => '&#163;',
696 '&curren;' => '&#164;',
697 '&yen;' => '&#165;',
698 '&brvbar;' => '&#166;',
699 '&sect;' => '&#167;',
700 '&uml;' => '&#168;',
701 '&copy;' => '&#169;',
702 '&ordf;' => '&#170;',
703 '&laquo;' => '&#171;',
704 '&not;' => '&#172;',
705 '&shy;' => '&#173;',
706 '&reg;' => '&#174;',
707 '&macr;' => '&#175;',
708 '&deg;' => '&#176;',
709 '&plusmn;' => '&#177;',
710 '&sup2;' => '&#178;',
711 '&sup3;' => '&#179;',
712 '&acute;' => '&#180;',
713 '&micro;' => '&#181;',
714 '&para;' => '&#182;',
715 '&middot;' => '&#183;',
716 '&cedil;' => '&#184;',
717 '&sup1;' => '&#185;',
718 '&ordm;' => '&#186;',
719 '&raquo;' => '&#187;',
720 '&frac14;' => '&#188;',
721 '&frac12;' => '&#189;',
722 '&frac34;' => '&#190;',
723 '&iquest;' => '&#191;',
724 '&Agrave;' => '&#192;',
725 '&Aacute;' => '&#193;',
726 '&Acirc;' => '&#194;',
727 '&Atilde;' => '&#195;',
728 '&Auml;' => '&#196;',
729 '&Aring;' => '&#197;',
730 '&AElig;' => '&#198;',
731 '&Ccedil;' => '&#199;',
732 '&Egrave;' => '&#200;',
733 '&Eacute;' => '&#201;',
734 '&Ecirc;' => '&#202;',
735 '&Euml;' => '&#203;',
736 '&Igrave;' => '&#204;',
737 '&Iacute;' => '&#205;',
738 '&Icirc;' => '&#206;',
739 '&Iuml;' => '&#207;',
740 '&ETH;' => '&#208;',
741 '&Ntilde;' => '&#209;',
742 '&Ograve;' => '&#210;',
743 '&Oacute;' => '&#211;',
744 '&Ocirc;' => '&#212;',
745 '&Otilde;' => '&#213;',
746 '&Ouml;' => '&#214;',
747 '&times;' => '&#215;',
748 '&Oslash;' => '&#216;',
749 '&Ugrave;' => '&#217;',
750 '&Uacute;' => '&#218;',
751 '&Ucirc;' => '&#219;',
752 '&Uuml;' => '&#220;',
753 '&Yacute;' => '&#221;',
754 '&THORN;' => '&#222;',
755 '&szlig;' => '&#223;',
756 '&agrave;' => '&#224;',
757 '&aacute;' => '&#225;',
758 '&acirc;' => '&#226;',
759 '&atilde;' => '&#227;',
760 '&auml;' => '&#228;',
761 '&aring;' => '&#229;',
762 '&aelig;' => '&#230;',
763 '&ccedil;' => '&#231;',
764 '&egrave;' => '&#232;',
765 '&eacute;' => '&#233;',
766 '&ecirc;' => '&#234;',
767 '&euml;' => '&#235;',
768 '&igrave;' => '&#236;',
769 '&iacute;' => '&#237;',
770 '&icirc;' => '&#238;',
771 '&iuml;' => '&#239;',
772 '&eth;' => '&#240;',
773 '&ntilde;' => '&#241;',
774 '&ograve;' => '&#242;',
775 '&oacute;' => '&#243;',
776 '&ocirc;' => '&#244;',
777 '&otilde;' => '&#245;',
778 '&ouml;' => '&#246;',
779 '&divide;' => '&#247;',
780 '&oslash;' => '&#248;',
781 '&ugrave;' => '&#249;',
782 '&uacute;' => '&#250;',
783 '&ucirc;' => '&#251;',
784 '&uuml;' => '&#252;',
785 '&yacute;' => '&#253;',
786 '&thorn;' => '&#254;',
787 '&yuml;' => '&#255;',
788 // Latin Extended-A
789 '&OElig;' => '&#338;',
790 '&oelig;' => '&#339;',
791 '&Scaron;' => '&#352;',
792 '&scaron;' => '&#353;',
793 '&Yuml;' => '&#376;',
794 // Spacing Modifier Letters
795 '&circ;' => '&#710;',
796 '&tilde;' => '&#732;',
797 // General Punctuation
798 '&ensp;' => '&#8194;',
799 '&emsp;' => '&#8195;',
800 '&thinsp;' => '&#8201;',
801 '&zwnj;' => '&#8204;',
802 '&zwj;' => '&#8205;',
803 '&lrm;' => '&#8206;',
804 '&rlm;' => '&#8207;',
805 '&ndash;' => '&#8211;',
806 '&mdash;' => '&#8212;',
807 '&lsquo;' => '&#8216;',
808 '&rsquo;' => '&#8217;',
809 '&sbquo;' => '&#8218;',
810 '&ldquo;' => '&#8220;',
811 '&rdquo;' => '&#8221;',
812 '&bdquo;' => '&#8222;',
813 '&dagger;' => '&#8224;',
814 '&Dagger;' => '&#8225;',
815 '&permil;' => '&#8240;',
816 '&lsaquo;' => '&#8249;',
817 '&rsaquo;' => '&#8250;',
818 '&euro;' => '&#8364;',
819 // Latin Extended-B
820 '&fnof;' => '&#402;',
821 // Greek
822 '&Alpha;' => '&#913;',
823 '&Beta;' => '&#914;',
824 '&Gamma;' => '&#915;',
825 '&Delta;' => '&#916;',
826 '&Epsilon;' => '&#917;',
827 '&Zeta;' => '&#918;',
828 '&Eta;' => '&#919;',
829 '&Theta;' => '&#920;',
830 '&Iota;' => '&#921;',
831 '&Kappa;' => '&#922;',
832 '&Lambda;' => '&#923;',
833 '&Mu;' => '&#924;',
834 '&Nu;' => '&#925;',
835 '&Xi;' => '&#926;',
836 '&Omicron;' => '&#927;',
837 '&Pi;' => '&#928;',
838 '&Rho;' => '&#929;',
839 '&Sigma;' => '&#931;',
840 '&Tau;' => '&#932;',
841 '&Upsilon;' => '&#933;',
842 '&Phi;' => '&#934;',
843 '&Chi;' => '&#935;',
844 '&Psi;' => '&#936;',
845 '&Omega;' => '&#937;',
846 '&alpha;' => '&#945;',
847 '&beta;' => '&#946;',
848 '&gamma;' => '&#947;',
849 '&delta;' => '&#948;',
850 '&epsilon;' => '&#949;',
851 '&zeta;' => '&#950;',
852 '&eta;' => '&#951;',
853 '&theta;' => '&#952;',
854 '&iota;' => '&#953;',
855 '&kappa;' => '&#954;',
856 '&lambda;' => '&#955;',
857 '&mu;' => '&#956;',
858 '&nu;' => '&#957;',
859 '&xi;' => '&#958;',
860 '&omicron;' => '&#959;',
861 '&pi;' => '&#960;',
862 '&rho;' => '&#961;',
863 '&sigmaf;' => '&#962;',
864 '&sigma;' => '&#963;',
865 '&tau;' => '&#964;',
866 '&upsilon;' => '&#965;',
867 '&phi;' => '&#966;',
868 '&chi;' => '&#967;',
869 '&psi;' => '&#968;',
870 '&omega;' => '&#969;',
871 '&thetasym;' => '&#977;',
872 '&upsih;' => '&#978;',
873 '&piv;' => '&#982;',
874 // General Punctuation
875 '&bull;' => '&#8226;',
876 '&hellip;' => '&#8230;',
877 '&prime;' => '&#8242;',
878 '&Prime;' => '&#8243;',
879 '&oline;' => '&#8254;',
880 '&frasl;' => '&#8260;',
881 // Letterlike Symbols
882 '&weierp;' => '&#8472;',
883 '&image;' => '&#8465;',
884 '&real;' => '&#8476;',
885 '&trade;' => '&#8482;',
886 '&alefsym;' => '&#8501;',
887 // Arrows
888 '&larr;' => '&#8592;',
889 '&uarr;' => '&#8593;',
890 '&rarr;' => '&#8594;',
891 '&darr;' => '&#8595;',
892 '&harr;' => '&#8596;',
893 '&crarr;' => '&#8629;',
894 '&lArr;' => '&#8656;',
895 '&uArr;' => '&#8657;',
896 '&rArr;' => '&#8658;',
897 '&dArr;' => '&#8659;',
898 '&hArr;' => '&#8660;',
899 // Mathematical Operators
900 '&forall;' => '&#8704;',
901 '&part;' => '&#8706;',
902 '&exist;' => '&#8707;',
903 '&empty;' => '&#8709;',
904 '&nabla;' => '&#8711;',
905 '&isin;' => '&#8712;',
906 '&notin;' => '&#8713;',
907 '&ni;' => '&#8715;',
908 '&prod;' => '&#8719;',
909 '&sum;' => '&#8721;',
910 '&minus;' => '&#8722;',
911 '&lowast;' => '&#8727;',
912 '&radic;' => '&#8730;',
913 '&prop;' => '&#8733;',
914 '&infin;' => '&#8734;',
915 '&ang;' => '&#8736;',
916 '&and;' => '&#8743;',
917 '&or;' => '&#8744;',
918 '&cap;' => '&#8745;',
919 '&cup;' => '&#8746;',
920 '&int;' => '&#8747;',
921 '&there4;' => '&#8756;',
922 '&sim;' => '&#8764;',
923 '&cong;' => '&#8773;',
924 '&asymp;' => '&#8776;',
925 '&ne;' => '&#8800;',
926 '&equiv;' => '&#8801;',
927 '&le;' => '&#8804;',
928 '&ge;' => '&#8805;',
929 '&sub;' => '&#8834;',
930 '&sup;' => '&#8835;',
931 '&nsub;' => '&#8836;',
932 '&sube;' => '&#8838;',
933 '&supe;' => '&#8839;',
934 '&oplus;' => '&#8853;',
935 '&otimes;' => '&#8855;',
936 '&perp;' => '&#8869;',
937 '&sdot;' => '&#8901;',
938 // Miscellaneous Technical
939 '&lceil;' => '&#8968;',
940 '&rceil;' => '&#8969;',
941 '&lfloor;' => '&#8970;',
942 '&rfloor;' => '&#8971;',
943 '&lang;' => '&#9001;',
944 '&rang;' => '&#9002;',
945 // Geometric Shapes
946 '&loz;' => '&#9674;',
947 // Miscellaneous Symbols
948 '&spades;' => '&#9824;',
949 '&clubs;' => '&#9827;',
950 '&hearts;' => '&#9829;',
951 '&diams;' => '&#9830;');
952
953 $str = str_replace(array_keys($entities), array_values($entities), $str);
954
955 return $str;
956 }
957
958 /* ------------------------------ main --------------------------- */
959
960 global $squirrelmail_language, $languages, $use_gettext;
961
962 if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
963 $squirrelmail_language = '';
964 }
965
966 /**
967 * Array specifies the available translations.
968 *
969 * Structure of array:
970 * $languages['language']['variable'] = 'value'
971 *
972 * Possible 'variable' names:
973 * NAME - Translation name in English
974 * CHARSET - Encoding used by translation
975 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
976 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
977 * 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
978 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
979 * XTRA_CODE - translation uses special functions. See doc/i18n.txt
980 *
981 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
982 *
983 * @name $languages
984 * @global array $languages
985 */
986 $languages['en_US']['NAME'] = 'English';
987 $languages['en_US']['CHARSET'] = 'iso-8859-1';
988 $languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
989 $languages['en']['ALIAS'] = 'en_US';
990
991 /**
992 * Automatic translation loading from setup.php files.
993 * Solution for bug. 1240889.
994 * setup.php file can contain $languages array entries and XTRA_CODE functions.
995 */
996 if (is_dir(SM_PATH . 'locale') &&
997 is_readable(SM_PATH . 'locale')) {
998 $localedir = dir(SM_PATH . 'locale');
999 while($lang_dir=$localedir->read()) {
1000 // remove trailing slash, if present
1001 if (substr($lang_dir,-1)=='/') {
1002 $lang_dir = substr($lang_dir,0,-1);
1003 }
1004 if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
1005 is_dir(SM_PATH.'locale/'.$lang_dir) &&
1006 file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
1007 include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
1008 }
1009 }
1010 $localedir->close();
1011 }
1012
1013 /* Detect whether gettext is installed. */
1014 $gettext_flags = 0;
1015 if (function_exists('_')) {
1016 $gettext_flags += 1;
1017 }
1018 if (function_exists('bindtextdomain')) {
1019 $gettext_flags += 2;
1020 }
1021 if (function_exists('textdomain')) {
1022 $gettext_flags += 4;
1023 }
1024 if (function_exists('ngettext')) {
1025 $gettext_flags += 8;
1026 }
1027
1028 /* If gettext is fully loaded, cool */
1029 if ($gettext_flags == 15) {
1030 $use_gettext = true;
1031 }
1032
1033 /* If ngettext support is missing, load it */
1034 elseif ($gettext_flags == 7) {
1035 $use_gettext = true;
1036 // load internal ngettext functions
1037 include_once(SM_PATH . 'class/l10n.class.php');
1038 include_once(SM_PATH . 'functions/ngettext.php');
1039 }
1040
1041 /* If we can fake gettext, try that */
1042 elseif ($gettext_flags == 0) {
1043 $use_gettext = true;
1044 include_once(SM_PATH . 'functions/gettext.php');
1045 } else {
1046 /* Uh-ho. A weird install */
1047 if (! $gettext_flags & 1) {
1048 /**
1049 * Function is used as replacement in broken installs
1050 * @ignore
1051 */
1052 function _($str) {
1053 return $str;
1054 }
1055 }
1056 if (! $gettext_flags & 2) {
1057 /**
1058 * Function is used as replacement in broken installs
1059 * @ignore
1060 */
1061 function bindtextdomain() {
1062 return;
1063 }
1064 }
1065 if (! $gettext_flags & 4) {
1066 /**
1067 * Function is used as replacemet in broken installs
1068 * @ignore
1069 */
1070 function textdomain() {
1071 return;
1072 }
1073 }
1074 if (! $gettext_flags & 8) {
1075 /**
1076 * Function is used as replacemet in broken installs
1077 * @ignore
1078 */
1079 function ngettext($str,$str2,$number) {
1080 if ($number>1) {
1081 return $str2;
1082 } else {
1083 return $str;
1084 }
1085 }
1086 }
1087 if (! function_exists('dgettext')) {
1088 /**
1089 * Replacement for broken setups.
1090 * @ignore
1091 */
1092 function dgettext($domain,$str) {
1093 return $str;
1094 }
1095 }
1096 if (! function_exists('dngettext')) {
1097 /**
1098 * Replacement for broken setups
1099 * @ignore
1100 */
1101 function dngettext($domain,$str1,$strn,$number) {
1102 return ($number==1 ? $str1 : $strn);
1103 }
1104 }
1105 }