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