New icon theming. Also added basic template for left_main.php
[squirrelmail.git] / functions / i18n.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 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @version $Id$
15 * @package squirrelmail
16 * @subpackage i18n
17 */
18
19 /** @ignore */
20 if (! defined('SM_PATH')) define('SM_PATH','../');
21
22 /** Everything uses global.php... */
23 require_once(SM_PATH . 'functions/global.php');
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 // 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
98 /**
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.
105 *
106 * sanitizing of html tags is also done by this function.
107 *
108 * @param string $charset
109 * @param string $string Text to be decoded
110 * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
111 * Argument is available since 1.5.1 and 1.4.5.
112 * @param boolean $save_html disables htmlspecialchars() in order to preserve
113 * html formating. Use with care. Available since 1.5.1
114 * @return string decoded string
115 */
116 function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
117 global $languages, $squirrelmail_language, $default_charset;
118 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
119
120 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
121 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
122 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
123 }
124
125 $charset = strtolower($charset);
126
127 set_my_charset();
128
129 // Variables that allow to use functions without function_exist() calls
130 if (! isset($use_php_recode) || $use_php_recode=="" ) {
131 $use_php_recode=false; }
132 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
133 $use_php_iconv=false; }
134
135 // Don't do conversion if charset is the same.
136 if ( ! $force_decode && $charset == strtolower($default_charset) )
137 return ($save_html ? $string : htmlspecialchars($string));
138
139 // catch iso-8859-8-i thing
140 if ( $charset == "iso-8859-8-i" )
141 $charset = "iso-8859-8";
142
143 /*
144 * Recode converts html special characters automatically if you use
145 * 'charset..html' decoding. There is no documented way to put -d option
146 * into php recode function call.
147 */
148 if ( $use_php_recode ) {
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);
153 return ($save_html ? $string : htmlspecialchars($string));
154 } else {
155 $string = recode_string($charset . "..html",$string);
156 // recode does not convert single quote, htmlspecialchars does.
157 $string = str_replace("'", '&#039;', $string);
158 // undo html specialchars
159 if ($save_html)
160 $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
161 array('&','"','<','>'),$string);
162 return $string;
163 }
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') {
168 $string = iconv($charset,$default_charset,$string);
169 return ($save_html ? $string : htmlspecialchars($string));
170 }
171
172 // If we don't use recode and iconv, we'll do it old way.
173
174 /* All HTML special characters are 7 bit and can be replaced first */
175 if (! $save_html) $string = htmlspecialchars ($string);
176
177 /* controls cpu and memory intensive decoding cycles */
178 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
179 $aggressive_decoding=false; }
180
181 $decode=fixcharset($charset);
182 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
183 if (file_exists($decodefile)) {
184 include_once($decodefile);
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);
187 } else {
188 $ret = $string;
189 }
190 return( $ret );
191 }
192
193 /**
194 * Converts html string to given charset
195 * @since 1.5.1 and 1.4.4
196 * @param string $string
197 * @param string $charset
198 * @param boolean $htmlencode keep htmlspecialchars encoding
199 * @param string
200 */
201 function charset_encode($string,$charset,$htmlencode=true) {
202 global $default_charset;
203
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);
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
212 include_once(SM_PATH . 'functions/encode/us_ascii.php');
213 $ret = charset_encode_us_ascii($string);
214 } else {
215 /**
216 * fix for yahoo users that remove all us-ascii related things
217 */
218 $ret = $string;
219 }
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 }
230 return( $ret );
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.
238 * @since 1.5.1 and 1.4.4
239 * @param string $in_charset initial charset
240 * @param string $string string that has to be converted
241 * @param string $out_charset final charset
242 * @param boolean $htmlencode keep htmlspecialchars encoding
243 * @return string converted string
244 */
245 function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
246 $string=charset_decode($in_charset,$string,true);
247 $string=charset_encode($string,$out_charset,$htmlencode);
248 return $string;
249 }
250
251 /**
252 * Makes charset name suitable for decoding cycles
253 *
254 * @since 1.5.0 and 1.4.4
255 * @param string $charset Name of charset
256 * @return string $charset Adjusted name of charset
257 */
258 function fixcharset($charset) {
259 /* remove minus and characters that might be used in paths from charset
260 * name in order to be able to use it in function names and include calls.
261 */
262 $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
263
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
271 // windows-125x and cp125x charsets
272 $charset=str_replace('windows_','cp',$charset);
273
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 }
283
284 /**
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
288 *
289 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
290 * gettext translation bindings and html header information.
291 *
292 * Function returns error codes, if there is some fatal error.
293 * 0 = no error,
294 * 1 = mbstring support is not present,
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.
300 * @return int function execution error codes.
301 */
302 function set_up_language($sm_language, $do_search = false, $default = false) {
303
304 static $SetupAlready = 0;
305 global $use_gettext, $languages,
306 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
307 $sm_notAlias, $username, $data_dir;
308
309 if ($SetupAlready) {
310 return;
311 }
312
313 $SetupAlready = TRUE;
314 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
315
316 /**
317 * If function is asked to detect preferred language
318 * OR squirrelmail default language is set to empty string
319 * AND
320 * squirrelmail language ($sm_language) is empty string
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 */
327 if (($do_search || empty($squirrelmail_default_language)) &&
328 ! $sm_language &&
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
333 $sm_language = substr($accept_lang, 0, 2);
334 }
335
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 */
343 if ((!$sm_language||$default) &&
344 ! empty($squirrelmail_default_language)) {
345 $squirrelmail_language = $squirrelmail_default_language;
346 $sm_language = $squirrelmail_default_language;
347 }
348
349 /** provide failsafe language when detection fails */
350 if (! $sm_language) $sm_language='en_US';
351
352 $sm_notAlias = $sm_language;
353
354 // Catching removed translation
355 // System reverts to English translation if user prefs contain translation
356 // that is not available in $languages array
357 if (!isset($languages[$sm_notAlias])) {
358 $sm_notAlias="en_US";
359 }
360
361 while (isset($languages[$sm_notAlias]['ALIAS'])) {
362 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
363 }
364
365 if ( isset($sm_language) &&
366 $use_gettext &&
367 $sm_language != '' &&
368 isset($languages[$sm_notAlias]['CHARSET']) ) {
369 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
370 sq_textdomain( 'squirrelmail' );
371
372 // set codeset in order to avoid gettext charset conversions
373 if (function_exists('bind_textdomain_codeset')) {
374 // Japanese translation uses different internal charset
375 if ($sm_notAlias == 'ja_JP') {
376 bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
377 } else {
378 bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
379 }
380 }
381
382 // Use LOCALE key, if it is set.
383 if (isset($languages[$sm_notAlias]['LOCALE'])){
384 $longlocale=$languages[$sm_notAlias]['LOCALE'];
385 } else {
386 $longlocale=$sm_notAlias;
387 }
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
402 if ( !((bool)ini_get('safe_mode')) &&
403 getenv( 'LC_ALL' ) != $longlocale ) {
404 putenv( "LC_ALL=$longlocale" );
405 putenv( "LANG=$longlocale" );
406 putenv( "LANGUAGE=$longlocale" );
407 putenv( "LC_NUMERIC=C" );
408 if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" );
409 }
410 // Workaround for plugins that use numbers with floating point
411 // It might be removed if plugins use correct decimal delimiters
412 // according to locale settings.
413 setlocale(LC_NUMERIC, 'C');
414 // Workaround for specific Turkish strtolower/strtoupper rules.
415 // Many functions expect English conversion rules.
416 if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C');
417
418 /**
419 * Set text direction/alignment variables
420 * When language environment is setup, scripts can use these globals
421 * without accessing $languages directly and making checks for optional
422 * array key.
423 */
424 global $text_direction, $left_align, $right_align;
425 if (isset($languages[$sm_notAlias]['DIR']) &&
426 $languages[$sm_notAlias]['DIR'] == 'rtl') {
427 /**
428 * Text direction
429 * @global string $text_direction
430 */
431 $text_direction='rtl';
432 /**
433 * Left alignment
434 * @global string $left_align
435 */
436 $left_align='right';
437 /**
438 * Right alignment
439 * @global string $right_align
440 */
441 $right_align='left';
442 } else {
443 $text_direction='ltr';
444 $left_align='left';
445 $right_align='right';
446 }
447
448 $squirrelmail_language = $sm_notAlias;
449 if ($squirrelmail_language == 'ja_JP') {
450 header ('Content-Type: text/html; charset=EUC-JP');
451 if (!function_exists('mb_internal_encoding')) {
452 // Error messages can't be displayed here
453 $error = 1;
454 // Revert to English if possible.
455 if (function_exists('setPref') && $username!='' && $data_dir!="") {
456 setPref($data_dir, $username, 'language', "en_US");
457 $error = 2;
458 }
459 // stop further execution in order not to get php errors on mb_internal_encoding().
460 return $error;
461 }
462 if (function_exists('mb_language')) {
463 mb_language('Japanese');
464 }
465 mb_internal_encoding('EUC-JP');
466 mb_http_output('pass');
467 } elseif ($squirrelmail_language == 'en_US') {
468 header( 'Content-Type: text/html; charset=' . $default_charset );
469 } else {
470 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
471 }
472 /**
473 * mbstring.func_overload fix (#929644).
474 *
475 * php mbstring extension can replace standard string functions with their multibyte
476 * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature
477 * was added in php v.4.2.0
478 *
479 * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced
480 * to use mbstring functions and mbstring internal encoding is set to multibyte charset,
481 * interface can't trust regular string functions. Due to mbstring overloading design
482 * limits php scripts can't control this setting.
483 *
484 * This hack should fix some issues related to 8bit strings in passwords. Correct fix is
485 * to disable mbstring overloading. Japanese translation uses different internal encoding.
486 */
487 if ($squirrelmail_language != 'ja_JP' &&
488 function_exists('mb_internal_encoding') &&
489 check_php_version(4,2,0) &&
490 (int)ini_get('mbstring.func_overload')!=0) {
491 mb_internal_encoding('pass');
492 }
493 }
494 return 0;
495 }
496
497 /**
498 * Sets default_charset variable according to the one that is used by user's translations.
499 *
500 * Function changes global $default_charset variable in order to be sure, that it
501 * contains charset used by user's translation. Sanity of $squirrelmail_language
502 * and $default_charset combination is also tested.
503 *
504 * There can be a $default_charset setting in the
505 * config.php file, but the user may have a different language
506 * selected for a user interface. This function checks the
507 * language selected by the user and tags the outgoing messages
508 * with the appropriate charset corresponding to the language
509 * selection. This is "more right" (tm), than just stamping the
510 * message blindly with the system-wide $default_charset.
511 */
512 function set_my_charset(){
513 global $data_dir, $username, $default_charset, $languages, $squirrelmail_language;
514
515 $my_language = getPref($data_dir, $username, 'language');
516 if (!$my_language) {
517 $my_language = $squirrelmail_language ;
518 }
519 // Catch removed translation
520 if (!isset($languages[$my_language])) {
521 $my_language="en_US";
522 }
523 while (isset($languages[$my_language]['ALIAS'])) {
524 $my_language = $languages[$my_language]['ALIAS'];
525 }
526 $my_charset = $languages[$my_language]['CHARSET'];
527 if ($my_language!='en_US') {
528 $default_charset = $my_charset;
529 }
530 }
531
532 /**
533 * Replaces non-braking spaces inserted by some browsers with regular space
534 *
535 * This function can be used to replace non-braking space symbols
536 * that are inserted in forms by some browsers instead of normal
537 * space symbol.
538 *
539 * @param string $string Text that needs to be cleaned
540 * @param string $charset Charset used in text
541 * @return string Cleaned text
542 */
543 function cleanup_nbsp($string,$charset) {
544
545 // reduce number of case statements
546 if (stristr('iso-8859-',substr($charset,0,9))){
547 $output_charset="iso-8859-x";
548 }
549 if (stristr('windows-125',substr($charset,0,11))){
550 $output_charset="cp125x";
551 }
552 if (stristr('koi8',substr($charset,0,4))){
553 $output_charset="koi8-x";
554 }
555 if (! isset($output_charset)){
556 $output_charset=strtolower($charset);
557 }
558
559 // where is non-braking space symbol
560 switch($output_charset):
561 case "iso-8859-x":
562 case "cp125x":
563 case "iso-2022-jp":
564 $nbsp="\xA0";
565 break;
566 case "koi8-x":
567 $nbsp="\x9A";
568 break;
569 case "utf-8":
570 $nbsp="\xC2\xA0";
571 break;
572 default:
573 // don't change string if charset is unmatched
574 return $string;
575 endswitch;
576
577 // return space instead of non-braking space.
578 return str_replace($nbsp,' ',$string);
579 }
580
581 /**
582 * Function informs if it is safe to convert given charset to the one that is used by user.
583 *
584 * It is safe to use conversion only if user uses utf-8 encoding and when
585 * converted charset is similar to the one that is used by user.
586 *
587 * @param string $input_charset Charset of text that needs to be converted
588 * @return bool is it possible to convert to user's charset
589 */
590 function is_conversion_safe($input_charset) {
591 global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
592
593 if (isset($lossy_encoding) && $lossy_encoding )
594 return true;
595
596 // convert to lower case
597 $input_charset = strtolower($input_charset);
598
599 // Is user's locale Unicode based ?
600 if ( $default_charset == "utf-8" ) {
601 return true;
602 }
603
604 // Charsets that are similar
605 switch ($default_charset):
606 case "windows-1251":
607 if ( $input_charset == "iso-8859-5" ||
608 $input_charset == "koi8-r" ||
609 $input_charset == "koi8-u" ) {
610 return true;
611 } else {
612 return false;
613 }
614 case "windows-1257":
615 if ( $input_charset == "iso-8859-13" ||
616 $input_charset == "iso-8859-4" ) {
617 return true;
618 } else {
619 return false;
620 }
621 case "iso-8859-4":
622 if ( $input_charset == "iso-8859-13" ||
623 $input_charset == "windows-1257" ) {
624 return true;
625 } else {
626 return false;
627 }
628 case "iso-8859-5":
629 if ( $input_charset == "windows-1251" ||
630 $input_charset == "koi8-r" ||
631 $input_charset == "koi8-u" ) {
632 return true;
633 } else {
634 return false;
635 }
636 case "iso-8859-13":
637 if ( $input_charset == "iso-8859-4" ||
638 $input_charset == "windows-1257" ) {
639 return true;
640 } else {
641 return false;
642 }
643 case "koi8-r":
644 if ( $input_charset == "windows-1251" ||
645 $input_charset == "iso-8859-5" ||
646 $input_charset == "koi8-u" ) {
647 return true;
648 } else {
649 return false;
650 }
651 case "koi8-u":
652 if ( $input_charset == "windows-1251" ||
653 $input_charset == "iso-8859-5" ||
654 $input_charset == "koi8-r" ) {
655 return true;
656 } else {
657 return false;
658 }
659 default:
660 return false;
661 endswitch;
662 }
663
664
665 /* ------------------------------ main --------------------------- */
666
667 global $squirrelmail_language, $languages, $use_gettext;
668
669 if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
670 $squirrelmail_language = '';
671 }
672
673 /**
674 * Array specifies the available translations.
675 *
676 * Structure of array:
677 * $languages['language']['variable'] = 'value'
678 *
679 * Possible 'variable' names:
680 * NAME - Translation name in English
681 * CHARSET - Encoding used by translation
682 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
683 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
684 * 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
685 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
686 * XTRA_CODE - translation uses special functions. See doc/i18n.txt
687 *
688 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
689 *
690 * @name $languages
691 * @global array $languages
692 */
693 $languages['en_US']['NAME'] = 'English';
694 $languages['en_US']['CHARSET'] = 'iso-8859-1';
695 $languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
696 $languages['en']['ALIAS'] = 'en_US';
697
698 /**
699 * Automatic translation loading from setup.php files.
700 * Solution for bug. 1240889.
701 * setup.php file can contain $languages array entries and XTRA_CODE functions.
702 */
703 if (is_dir(SM_PATH . 'locale') &&
704 is_readable(SM_PATH . 'locale')) {
705 $localedir = dir(SM_PATH . 'locale');
706 while($lang_dir=$localedir->read()) {
707 // remove trailing slash, if present
708 if (substr($lang_dir,-1)=='/') {
709 $lang_dir = substr($lang_dir,0,-1);
710 }
711 if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
712 is_dir(SM_PATH.'locale/'.$lang_dir) &&
713 file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
714 include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
715 }
716 }
717 $localedir->close();
718 }
719
720 /* Detect whether gettext is installed. */
721 $gettext_flags = 0;
722 if (function_exists('_')) {
723 $gettext_flags += 1;
724 }
725 if (function_exists('bindtextdomain')) {
726 $gettext_flags += 2;
727 }
728 if (function_exists('textdomain')) {
729 $gettext_flags += 4;
730 }
731 if (function_exists('ngettext')) {
732 $gettext_flags += 8;
733 }
734
735 /* If gettext is fully loaded, cool */
736 if ($gettext_flags == 15) {
737 $use_gettext = true;
738 }
739
740 /* If ngettext support is missing, load it */
741 elseif ($gettext_flags == 7) {
742 $use_gettext = true;
743 // load internal ngettext functions
744 include_once(SM_PATH . 'class/l10n.class.php');
745 include_once(SM_PATH . 'functions/ngettext.php');
746 }
747
748 /* If we can fake gettext, try that */
749 elseif ($gettext_flags == 0) {
750 $use_gettext = true;
751 include_once(SM_PATH . 'functions/gettext.php');
752 } else {
753 /* Uh-ho. A weird install */
754 if (! $gettext_flags & 1) {
755 /**
756 * Function is used as replacement in broken installs
757 * @ignore
758 */
759 function _($str) {
760 return $str;
761 }
762 }
763 if (! $gettext_flags & 2) {
764 /**
765 * Function is used as replacement in broken installs
766 * @ignore
767 */
768 function bindtextdomain() {
769 return;
770 }
771 }
772 if (! $gettext_flags & 4) {
773 /**
774 * Function is used as replacemet in broken installs
775 * @ignore
776 */
777 function textdomain() {
778 return;
779 }
780 }
781 if (! $gettext_flags & 8) {
782 /**
783 * Function is used as replacemet in broken installs
784 * @ignore
785 */
786 function ngettext($str,$str2,$number) {
787 if ($number>1) {
788 return $str2;
789 } else {
790 return $str;
791 }
792 }
793 }
794 if (! function_exists('dgettext')) {
795 /**
796 * Replacement for broken setups.
797 * @ignore
798 */
799 function dgettext($domain,$str) {
800 return $str;
801 }
802 }
803 if (! function_exists('dngettext')) {
804 /**
805 * Replacement for broken setups
806 * @ignore
807 */
808 function dngettext($domain,$str1,$strn,$number) {
809 return ($number==1 ? $str1 : $strn);
810 }
811 }
812 }
813 ?>