Fix E_ALL notice, thanks Alexandros.
[squirrelmail.git] / plugins / translate / setup.php
1 <?php
2
3 /**
4 * setup.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 /* Easy plugin that sends the body of the message to a new browser
13 window using the specified translator. It can also translate your
14 outgoing message if you send it to someone in a different country.
15
16 Languages from i18n, incorporated in the auto-language selection:
17 en - English
18 no - Norwegian (Bokm&aring;l)
19 no_NO_ny - Norwegian (Nynorsk)
20 de - Deutsch
21 ru - Russian KOI8-R
22 pl - Polish
23 sv - Swedish
24 nl - Dutch
25 pt_BR - Portuguese (Brazil)
26 fr - French
27 it - Italian
28 cs - Czech
29 es - Spanish
30 ko - Korean
31 */
32
33
34 /* Initialize the translation plugin */
35 function squirrelmail_plugin_init_translate() {
36 global $squirrelmail_plugin_hooks;
37
38 $squirrelmail_plugin_hooks['read_body_bottom']['translate'] = 'translate_read_form';
39 $squirrelmail_plugin_hooks['optpage_register_block']['translate'] = 'translate_optpage_register_block';
40 $squirrelmail_plugin_hooks['loading_prefs']['translate'] = 'translate_pref';
41 $squirrelmail_plugin_hooks['compose_button_row']['translate'] = 'translate_button';
42 }
43
44
45 /* Show the translation for a message you're reading */
46 function translate_read_form() {
47 global $color, $translate_server;
48 global $message, $translate_dir;
49 global $translate_show_read;
50 global $imapConnection, $wrap_at, $passed_id, $mailbox;
51
52 if (!$translate_show_read) {
53 return;
54 }
55
56 $translate_dir = 'to';
57
58 $trans_ar = $message->findDisplayEntity(array(), array('text/plain'));
59 $body = '';
60 if ($trans_ar[0] != '') {
61 for ($i = 0; $i < count($trans_ar); $i++) {
62 $body .= formatBody($imapConnection, $message, $color, $wrap_at, $trans_ar[$i], $passed_id, $mailbox);
63 }
64 $hookResults = do_hook('message_body', $body);
65 $body = $hookResults[1];
66 } else {
67 $body = 'Message can\'t be translated';
68 }
69
70 $new_body = $body;
71 $pos = strpos($new_body,
72 '">'. _("Download this as a file") . '</A></CENTER><BR></SMALL>');
73 if (is_int($pos)) {
74 $new_body = substr($new_body, 0, $pos);
75 }
76
77 $trans = get_html_translation_table(HTML_ENTITIES);
78 $trans[' '] = '&nbsp;';
79 $trans = array_flip($trans);
80 $new_body = strtr($new_body, $trans);
81
82 $new_body = urldecode($new_body);
83 $new_body = strip_tags($new_body);
84
85 /* I really don't like this next part ... */
86 $new_body = str_replace('"', "''", $new_body);
87 $new_body = strtr($new_body, "\n", ' ');
88
89 $function = 'translate_form_' . $translate_server;
90 $function($new_body);
91 }
92
93 function translate_table_end() {
94 ?></td>
95 </tr>
96 </table>
97 </td>
98 </tr>
99 </table>
100 </form>
101 <?php
102 }
103
104
105 function translate_button() {
106 global $translate_show_send;
107
108 if (! $translate_show_send) {
109 return;
110 }
111 }
112
113
114 function translate_optpage_register_block() {
115 global $optpage_blocks;
116 $optpage_blocks[] = array(
117 'name' => _("Translation Options"),
118 'url' => '../plugins/translate/options.php',
119 'desc' => _("Which translator should be used when you get messages in a different language?"),
120 'js' => false
121 );
122 }
123
124 function translate_pref() {
125 global $username, $data_dir;
126 global $translate_server, $translate_location;
127 global $translate_show_send, $translate_show_read;
128 global $translate_same_window;
129
130 $translate_server = getPref($data_dir, $username, 'translate_server');
131 if ($translate_server == '') {
132 $translate_server = 'babelfish';
133 }
134
135 $translate_location = getPref($data_dir, $username, 'translate_location');
136 if ($translate_location == '') {
137 $translate_location = 'center';
138 }
139
140 $translate_show_send = getPref($data_dir, $username, 'translate_show_send');
141 $translate_show_read = getPref($data_dir, $username, 'translate_show_read');
142 $translate_same_window = getPref($data_dir, $username, 'translate_same_window');
143 }
144
145
146 /**
147 * This function could be sped up.
148 * It basically negates the process if a ! is found in the beginning and
149 * matches a * at the end with 0 or more characters.
150 */
151 function translate_does_it_match_language($test) {
152 global $squirrelmail_language;
153 $true = 1;
154 $false = 0;
155 $index = 0;
156 $smindex = 0;
157
158 if (! $test || ! $squirrelmail_language) {
159 return $false;
160 }
161
162 if ($test[$index] == '!') {
163 $index ++;
164 $true = 0;
165 $false = 1;
166 }
167
168 if (($index == 0) && ($test == $squirrelmail_language)) {
169 return $true;
170 }
171
172 while (isset($test[$index]) && $test[$index]) {
173 if ($test[$index] == '*') {
174 return $true;
175 }
176 if ($test[$index] != $squirrelmail_language[$smindex]) {
177 return $false;
178 }
179 $index ++;
180 $smindex ++;
181 }
182
183 return $false;
184 }
185
186
187 function translate_lang_opt($from, $to, $value, $text) {
188 global $translate_dir;
189
190 $ret = ' <option value="' . $value . '"';
191
192 if (translate_does_it_match_language($to) && ($translate_dir == 'to')) {
193 $ret .= ' SELECTED';
194 }
195
196 if (translate_does_it_match_language($from) && ($translate_dir == 'from')) {
197 $ret .= ' SELECTED';
198 }
199
200 $ret .= '>' . $text . "</option>\n";
201
202 return( $ret );
203 }
204
205
206 function translate_new_form($action) {
207 global $translate_dir, $translate_new_window, $translate_location;
208 global $color, $translate_same_window;
209
210 echo '<form action="';
211
212 if ($translate_dir == 'to') {
213 echo $action;
214 } else {
215 echo 'translate.php';
216 }
217
218 echo '" method="post"';
219
220 if (!$translate_same_window) {
221 echo ' target="_blank"';
222 }
223
224 echo ">\n";
225
226 ?><table align="<?php echo $translate_location ?>" cellpadding=3 cellspacing=0 border=0 bgcolor=<?php echo $color[10] ?>>
227 <tr>
228 <td>
229 <table cellpadding=2 cellspacing=1 border=0 bgcolor="<?php echo $color[5] ?>">
230 <tr>
231 <td><?php
232 }
233
234 function translate_form_babelfish($message) {
235 translate_new_form('http://babelfish.altavista.com/babelfish/tr');
236 ?>
237 <input type="hidden" name="doit" value="done">
238 <input type="hidden" name="intl" value="1">
239 <input type="hidden" name="tt" value="urltext">
240 <input type="hidden" name="urltext" value="<?php echo $message; ?>">
241 <select name="lp"><?php
242 echo translate_lang_opt('en', 'zh', 'en_zh',
243 sprintf( _("%s to %s"),
244 _("English"),
245 _("Chinese"))) .
246 translate_lang_opt('en', 'fr', 'en_fr',
247 sprintf( _("%s to %s"),
248 _("English"),
249 _("French"))) .
250 translate_lang_opt('', 'de', 'en_de',
251 sprintf( _("%s to %s"),
252 _("English"),
253 _("German"))) .
254 translate_lang_opt('', 'it', 'en_it',
255 sprintf( _("%s to %s"),
256 _("English"),
257 _("Italian"))) .
258 translate_lang_opt('en', 'ja', 'en_ja',
259 sprintf( _("%s to %s"),
260 _("English"),
261 _("Japanese"))) .
262 translate_lang_opt('en', 'ko', 'en_ko',
263 sprintf( _("%s to %s"),
264 _("English"),
265 _("Korean"))) .
266 translate_lang_opt('', 'pt*', 'en_pt',
267 sprintf( _("%s to %s"),
268 _("English"),
269 _("Portuguese"))) .
270 translate_lang_opt('', 'es', 'en_es',
271 sprintf( _("%s to %s"),
272 _("English"),
273 _("Spanish"))) .
274 translate_lang_opt('zh', 'en', 'zh_en',
275 sprintf( _("%s to %s"),
276 _("Chinese"),
277 _("English"))) .
278 translate_lang_opt('fr', 'en', 'fr_en',
279 sprintf( _("%s to %s"),
280 _("French"),
281 _("English"))) .
282 translate_lang_opt('de', '', 'de_en',
283 sprintf( _("%s to %s"),
284 _("German"),
285 _("English"))) .
286 translate_lang_opt('it', '', 'it_en',
287 sprintf( _("%s to %s"),
288 _("Italian"),
289 _("English"))) .
290 translate_lang_opt('ja', 'en', 'ja_en',
291 sprintf( _("%s to %s"),
292 _("Japanese"),
293 _("English"))) .
294 translate_lang_opt('ko', 'en', 'ko_en',
295 sprintf( _("%s to %s"),
296 _("Korean"),
297 _("English"))) .
298 translate_lang_opt('pt*', '', 'pt_en',
299 sprintf( _("%s to %s"),
300 _("Portuguese"),
301 _("English"))) .
302 translate_lang_opt('es', '', 'es_en',
303 sprintf( _("%s to %s"),
304 _("Spanish"),
305 _("English"))) .
306 translate_lang_opt('', '', 'de_fr',
307 sprintf( _("%s to %s"),
308 _("German"),
309 _("French"))) .
310 translate_lang_opt('', '', 'fr_de',
311 sprintf( _("%s to %s"),
312 _("French"),
313 _("German"))) .
314 translate_lang_opt('ru', '', 'ru_en',
315 sprintf( _("%s to %s"),
316 _("Russian"),
317 _("English")));
318 echo '</select>'.
319 'Babelfish: <input type="Submit" value="' . _("Translate") . '">';
320
321 translate_table_end();
322 }
323
324 function translate_form_go($message) {
325 translate_new_form('http://translator.go.com/cb/trans_entry');
326 ?>
327 <input type=hidden name=input_type value=text>
328 <select name=lp><?php
329 echo translate_lang_opt('en', 'es', 'en_sp',
330 sprintf( _("%s to %s"),
331 _("English"),
332 _("Spanish"))) .
333 translate_lang_opt('', 'fr', 'en_fr',
334 sprintf( _("%s to %s"),
335 _("English"),
336 _("French"))) .
337 translate_lang_opt('', 'de', 'en_ge',
338 sprintf( _("%s to %s"),
339 _("English"),
340 _("German"))) .
341 translate_lang_opt('', 'it', 'en_it',
342 sprintf( _("%s to %s"),
343 _("English"),
344 _("Italian"))) .
345 translate_lang_opt('', 'pt', 'en_pt',
346 sprintf( _("%s to %s"),
347 _("English"),
348 _("Portuguese"))) .
349 translate_lang_opt('es', 'en', 'sp_en',
350 sprintf( _("%s to %s"),
351 _("Spanish"),
352 _("English"))) .
353 translate_lang_opt('fr', '', 'fr_en',
354 sprintf( _("%s to %s"),
355 _("French"),
356 _("English"))) .
357 translate_lang_opt('de', '', 'ge_en',
358 sprintf( _("%s to %s"),
359 _("German"),
360 _("English"))) .
361 translate_lang_opt('it', '', 'it_en',
362 sprintf( _("%s to %s"),
363 _("Italian"),
364 _("English"))) .
365 translate_lang_opt('pt', '', 'pt_en',
366 sprintf( _("%s to %s"),
367 _("Portuguese"),
368 _("English")));
369 echo '</select>'.
370 "<input type=\"hidden\" name=\"text\" value=\"$message\">".
371 'Go.com: <input type="Submit" value="' . _("Translate") . '">';
372
373 translate_table_end();
374 }
375
376 function translate_form_intertran($message) {
377 translate_new_form('http://www.tranexp.com:2000/InterTran');
378 echo '<INPUT TYPE="hidden" NAME="topframe" VALUE="yes">'.
379 '<INPUT TYPE="hidden" NAME="type" VALUE="text">'.
380 "<input type=\"hidden\" name=\"text\" value=\"$message\">";
381
382 $left = '<SELECT name="from">' .
383 translate_lang_opt('pt_BR', '', 'pob', _("Brazilian Portuguese")).
384 translate_lang_opt('', '', 'bul', _("Bulgarian") . ' (CP 1251)').
385 translate_lang_opt('', '', 'cro', _("Croatian") . ' (CP 1250)').
386 translate_lang_opt('cs', '', 'che', _("Czech") . ' (CP 1250)').
387 translate_lang_opt('', '', 'dan', _("Danish")).
388 translate_lang_opt('nl', '', 'dut', _("Dutch")).
389 translate_lang_opt('en', '!en', 'eng', _("English")).
390 translate_lang_opt('', '', 'spe', _("European Spanish")).
391 translate_lang_opt('', '', 'fin', _("Finnish")).
392 translate_lang_opt('fr', '', 'fre', _("French")).
393 translate_lang_opt('de', '', 'ger', _("German")).
394 translate_lang_opt('', '', 'grk', _("Greek")).
395 translate_lang_opt('', '', 'hun', _("Hungarian") . ' (CP 1250)').
396 translate_lang_opt('', '', 'ice', _("Icelandic")).
397 translate_lang_opt('it', '', 'ita', _("Italian")).
398 translate_lang_opt('', '', 'jpn', _("Japanese") . ' (Shift JIS)').
399 translate_lang_opt('', '', 'spl', _("Latin American Spanish")).
400 translate_lang_opt('no*', '', 'nor', _("Norwegian")).
401 translate_lang_opt('pl', '', 'pol', _("Polish") . ' (ISO 8859-2)').
402 translate_lang_opt('', '', 'poe', _("Portuguese")).
403 translate_lang_opt('', '', 'rom', _("Romanian") . ' (CP 1250)').
404 translate_lang_opt('ru', '', 'rus', _("Russian") . ' (CP 1251)').
405 translate_lang_opt('', '', 'sel', _("Serbian") . ' (CP 1250)').
406 translate_lang_opt('', '', 'slo', _("Slovenian") . ' (CP 1250)').
407 translate_lang_opt('es', '', 'spa', _("Spanish")).
408 translate_lang_opt('sv', '', 'swe', _("Swedish")).
409 translate_lang_opt('', '', 'tur', _("Turkish") . ' (CP 1254)').
410 translate_lang_opt('', '', 'wel', _("Welsh")).
411 '</SELECT>';
412
413 $right = '<SELECT name="to">'.
414 translate_lang_opt('', 'pt_BR', 'pob', _("Brazilian Portuguese")).
415 translate_lang_opt('', '', 'bul', _("Bulgarian") . ' (CP 1251)').
416 translate_lang_opt('', '', 'cro', _("Croatian") . ' (CP 1250)').
417 translate_lang_opt('', 'cs', 'che', _("Czech") . ' (CP 1250)').
418 translate_lang_opt('', '', 'dan', _("Danish")).
419 translate_lang_opt('', 'nl', 'dut', _("Dutch")).
420 translate_lang_opt('!en', 'en', 'eng', _("English")).
421 translate_lang_opt('', '', 'spe', _("European Spanish")).
422 translate_lang_opt('', '', 'fin', _("Finnish")).
423 translate_lang_opt('', 'fr', 'fre', _("French")).
424 translate_lang_opt('', 'de', 'ger', _("German")).
425 translate_lang_opt('', '', 'grk', _("Greek")).
426 translate_lang_opt('', '', 'hun', _("Hungarian") . ' (CP 1250)').
427 translate_lang_opt('', '', 'ice', _("Icelandic")).
428 translate_lang_opt('', 'it', 'ita', _("Italian")).
429 translate_lang_opt('', '', 'jpn', _("Japanese") . ' (Shift JIS)').
430 translate_lang_opt('', '', 'spl', _("Latin American Spanish")).
431 translate_lang_opt('', 'no*', 'nor', _("Norwegian")).
432 translate_lang_opt('', 'pl', 'pol', _("Polish") . ' (ISO 8859-2)').
433 translate_lang_opt('', '', 'poe', _("Portuguese")).
434 translate_lang_opt('', '', 'rom', _("Romanian") . ' (CP 1250)').
435 translate_lang_opt('', 'ru', 'rus', _("Russian") . ' (CP 1251)').
436 translate_lang_opt('', '', 'sel', _("Serbian") . ' (CP 1250)').
437 translate_lang_opt('', '', 'slo', _("Slovenian") . ' (CP 1250)').
438 translate_lang_opt('', 'es', 'spa', _("Spanish")).
439 translate_lang_opt('', 'sv', 'swe', _("Swedish")).
440 translate_lang_opt('', '', 'tur', _("Turkish") . ' (CP 1254)').
441 translate_lang_opt('', '', 'wel', _("Welsh")).
442 '</SELECT>';
443 printf( _("%s to %s"), $left, $right );
444 echo 'InterTran: <input type=submit value="' . _("Translate") . '">';
445
446 translate_table_end();
447 }
448
449 function translate_form_gpltrans($message) {
450 translate_new_form('http://www.translator.cx/cgi-bin/gplTrans');
451 echo '<select name="language">'.
452 translate_lang_opt('nl', 'nl', 'dutch_dict', _("Dutch")).
453 translate_lang_opt('fr', 'fr', 'french_dict', _("French")).
454 translate_lang_opt('de', 'de', 'german_dict', _("German")).
455 translate_lang_opt('', '', 'indonesian_dict', _("Indonesian")).
456 translate_lang_opt('it', 'it', 'italian_dict', _("Italian")).
457 translate_lang_opt('', '', 'latin_dict', _("Latin")).
458 translate_lang_opt('pt', 'pt', 'portuguese_dict', _("Portuguese")).
459 translate_lang_opt('es', 'es', 'spanish_dict', _("Spanish")).
460 '</select>';
461 echo '<select name="toenglish">';
462 echo '<option value="yes" >'. _("to English") . '</option>';
463 echo '<option value="no" selected>' . _("from English") . '</option></select>';
464 echo "<input type=hidden name=text value=\"$message\">".
465 'GPLTrans: <input type="submit" value="' . _("Translate") . '">';
466
467 translate_table_end();
468 }
469
470 function translate_form_dictionary($message) {
471 translate_new_form('http://dictionary.reference.com/translate/text.html');
472 list($usec, $sec) = explode(" ",microtime());
473 $time = $sec . (float)$usec*100000000;
474 echo "<input type=hidden name=text value=\"$message\">".
475 "<input type=hidden name=r value=\"$time\">".
476 '<SELECT NAME="lp">'.
477 translate_lang_opt('en', 'fr', 'en_fr',
478 sprintf( _("%s to %s"),
479 _("English"),
480 _("French"))) .
481 translate_lang_opt('', 'ge', 'en_ge',
482 sprintf( _("%s to %s"),
483 _("English"),
484 _("German"))) .
485 translate_lang_opt('', 'it', 'en_it',
486 sprintf( _("%s to %s"),
487 _("English"),
488 _("Italian"))) .
489 translate_lang_opt('', 'pt*', 'en_pt',
490 sprintf( _("%s to %s"),
491 _("English"),
492 _("Portuguese"))) .
493 translate_lang_opt('', 'es', 'en_es',
494 sprintf( _("%s to %s"),
495 _("English"),
496 _("Spanish"))) .
497 translate_lang_opt('fr', '', 'fr_en',
498 sprintf( _("%s to %s"),
499 _("French"),
500 _("English"))) .
501 translate_lang_opt('', '', 'fr_ge',
502 sprintf( _("%s to %s"),
503 _("French"),
504 _("German"))) .
505 translate_lang_opt('', '', 'ge_fr',
506 sprintf( _("%s to %s"),
507 _("German"),
508 _("French"))) .
509 translate_lang_opt('de', '', 'ge_en',
510 sprintf( _("%s to %s"),
511 _("German"),
512 _("English"))) .
513 translate_lang_opt('it', '', 'it_en',
514 sprintf( _("%s to %s"),
515 _("Italian"),
516 _("English"))) .
517 translate_lang_opt('pt*', '', 'pt_en',
518 sprintf( _("%s to %s"),
519 _("Portuguese"),
520 _("English"))) .
521 translate_lang_opt('es', '', 'es_en',
522 sprintf( _("%s to %s"),
523 _("Spanish"),
524 _("English"))) .
525 '</SELECT>'.
526 'Dictionary.com: <INPUT TYPE="submit" VALUE="'._("Translate").'">';
527
528 translate_table_end();
529 }
530 ?>