6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
11 * @subpackage translate
14 /* Easy plugin that sends the body of the message to a new browser
15 window using the specified translator. It can also translate your
16 outgoing message if you send it to someone in a different country.
18 Languages from i18n, incorporated in the auto-language selection:
20 no - Norwegian (Bokmål)
21 no_NO_ny - Norwegian (Nynorsk)
27 pt_BR - Portuguese (Brazil)
37 * Initialize the translation plugin
40 function squirrelmail_plugin_init_translate() {
41 global $squirrelmail_plugin_hooks;
43 $squirrelmail_plugin_hooks['read_body_bottom']['translate'] = 'translate_read_form';
44 $squirrelmail_plugin_hooks['optpage_register_block']['translate'] = 'translate_optpage_register_block';
45 $squirrelmail_plugin_hooks['loading_prefs']['translate'] = 'translate_pref';
46 $squirrelmail_plugin_hooks['compose_button_row']['translate'] = 'translate_button';
50 /* Show the translation for a message you're reading */
51 function translate_read_form() {
52 global $color, $translate_server;
53 global $message, $translate_dir;
54 global $translate_show_read;
55 global $imapConnection, $wrap_at, $passed_id, $mailbox;
57 if (!$translate_show_read) {
61 $translate_dir = 'to';
63 $trans_ar = $message->findDisplayEntity(array(), array('text/plain'));
65 if ($trans_ar[0] != '') {
66 for ($i = 0; $i < count($trans_ar); $i++
) {
67 $body .= formatBody($imapConnection, $message, $color, $wrap_at, $trans_ar[$i], $passed_id, $mailbox);
69 $hookResults = do_hook('message_body', $body);
70 $body = $hookResults[1];
72 $body = 'Message can\'t be translated';
76 $pos = strpos($new_body,
77 '">'. _("Download this as a file") . '</A></CENTER><BR></SMALL>');
79 $new_body = substr($new_body, 0, $pos);
82 $trans = get_html_translation_table(HTML_ENTITIES
);
83 $trans[' '] = ' ';
84 $trans = array_flip($trans);
85 $new_body = strtr($new_body, $trans);
87 $new_body = urldecode($new_body);
88 $new_body = strip_tags($new_body);
90 /* I really don't like this next part ... */
91 $new_body = str_replace('"', "''", $new_body);
92 $new_body = strtr($new_body, "\n", ' ');
94 $function = 'translate_form_' . $translate_server;
98 function translate_table_end() {
110 function translate_button() {
111 global $translate_show_send;
113 if (! $translate_show_send) {
119 function translate_optpage_register_block() {
120 global $optpage_blocks;
121 $optpage_blocks[] = array(
122 'name' => _("Translation Options"),
123 'url' => '../plugins/translate/options.php',
124 'desc' => _("Which translator should be used when you get messages in a different language?"),
129 function translate_pref() {
130 global $username, $data_dir;
131 global $translate_server, $translate_location;
132 global $translate_show_send, $translate_show_read;
133 global $translate_same_window;
135 $translate_server = getPref($data_dir, $username, 'translate_server');
136 if ($translate_server == '') {
137 $translate_server = 'babelfish';
140 $translate_location = getPref($data_dir, $username, 'translate_location');
141 if ($translate_location == '') {
142 $translate_location = 'center';
145 $translate_show_send = getPref($data_dir, $username, 'translate_show_send');
146 $translate_show_read = getPref($data_dir, $username, 'translate_show_read');
147 $translate_same_window = getPref($data_dir, $username, 'translate_same_window');
152 * This function could be sped up.
153 * It basically negates the process if a ! is found in the beginning and
154 * matches a * at the end with 0 or more characters.
156 function translate_does_it_match_language($test) {
157 global $squirrelmail_language;
163 if (! $test ||
! $squirrelmail_language) {
167 if ($test[$index] == '!') {
173 if (($index == 0) && ($test == $squirrelmail_language)) {
177 while (isset($test[$index]) && $test[$index]) {
178 if ($test[$index] == '*') {
181 if ($test[$index] != $squirrelmail_language[$smindex]) {
192 function translate_lang_opt($from, $to, $value, $text) {
193 global $translate_dir;
195 $ret = ' <option value="' . $value . '"';
197 if (translate_does_it_match_language($to) && ($translate_dir == 'to')) {
201 if (translate_does_it_match_language($from) && ($translate_dir == 'from')) {
205 $ret .= '>' . $text . "</option>\n";
211 function translate_new_form($action) {
212 global $translate_dir, $translate_new_window, $translate_location;
213 global $color, $translate_same_window;
215 echo '<form action="';
217 if ($translate_dir == 'to') {
220 echo 'translate.php';
223 echo '" method="post"';
225 if (!$translate_same_window) {
226 echo ' target="_blank"';
231 ?
><table align
="<?php echo $translate_location ?>" cellpadding
=3 cellspacing
=0 border
=0 bgcolor
=<?php
echo $color[10] ?
>>
234 <table cellpadding
=2 cellspacing
=1 border
=0 bgcolor
="<?php echo $color[5] ?>">
239 function translate_form_babelfish($message) {
240 translate_new_form('http://babelfish.altavista.com/babelfish/tr');
242 <input type
="hidden" name
="doit" value
="done">
243 <input type
="hidden" name
="intl" value
="1">
244 <input type
="hidden" name
="tt" value
="urltext">
245 <input type
="hidden" name
="urltext" value
="<?php echo $message; ?>">
246 <select name
="lp"><?php
247 echo translate_lang_opt('en', 'zh', 'en_zh',
248 sprintf( _("%s to %s"),
251 translate_lang_opt('en', 'fr', 'en_fr',
252 sprintf( _("%s to %s"),
255 translate_lang_opt('', 'de', 'en_de',
256 sprintf( _("%s to %s"),
259 translate_lang_opt('', 'it', 'en_it',
260 sprintf( _("%s to %s"),
263 translate_lang_opt('en', 'ja', 'en_ja',
264 sprintf( _("%s to %s"),
267 translate_lang_opt('en', 'ko', 'en_ko',
268 sprintf( _("%s to %s"),
271 translate_lang_opt('', 'pt*', 'en_pt',
272 sprintf( _("%s to %s"),
275 translate_lang_opt('', 'es', 'en_es',
276 sprintf( _("%s to %s"),
279 translate_lang_opt('zh', 'en', 'zh_en',
280 sprintf( _("%s to %s"),
283 translate_lang_opt('fr', 'en', 'fr_en',
284 sprintf( _("%s to %s"),
287 translate_lang_opt('de', '', 'de_en',
288 sprintf( _("%s to %s"),
291 translate_lang_opt('it', '', 'it_en',
292 sprintf( _("%s to %s"),
295 translate_lang_opt('ja', 'en', 'ja_en',
296 sprintf( _("%s to %s"),
299 translate_lang_opt('ko', 'en', 'ko_en',
300 sprintf( _("%s to %s"),
303 translate_lang_opt('pt*', '', 'pt_en',
304 sprintf( _("%s to %s"),
307 translate_lang_opt('es', '', 'es_en',
308 sprintf( _("%s to %s"),
311 translate_lang_opt('', '', 'de_fr',
312 sprintf( _("%s to %s"),
315 translate_lang_opt('', '', 'fr_de',
316 sprintf( _("%s to %s"),
319 translate_lang_opt('ru', '', 'ru_en',
320 sprintf( _("%s to %s"),
324 'Babelfish: <input type="Submit" value="' . _("Translate") . '">';
326 translate_table_end();
329 function translate_form_go($message) {
330 translate_new_form('http://translator.go.com/cb/trans_entry');
332 <input type
=hidden name
=input_type value
=text
>
333 <select name
=lp
><?php
334 echo translate_lang_opt('en', 'es', 'en_sp',
335 sprintf( _("%s to %s"),
338 translate_lang_opt('', 'fr', 'en_fr',
339 sprintf( _("%s to %s"),
342 translate_lang_opt('', 'de', 'en_ge',
343 sprintf( _("%s to %s"),
346 translate_lang_opt('', 'it', 'en_it',
347 sprintf( _("%s to %s"),
350 translate_lang_opt('', 'pt', 'en_pt',
351 sprintf( _("%s to %s"),
354 translate_lang_opt('es', 'en', 'sp_en',
355 sprintf( _("%s to %s"),
358 translate_lang_opt('fr', '', 'fr_en',
359 sprintf( _("%s to %s"),
362 translate_lang_opt('de', '', 'ge_en',
363 sprintf( _("%s to %s"),
366 translate_lang_opt('it', '', 'it_en',
367 sprintf( _("%s to %s"),
370 translate_lang_opt('pt', '', 'pt_en',
371 sprintf( _("%s to %s"),
375 "<input type=\"hidden\" name=\"text\" value=\"$message\">".
376 'Go.com: <input type="Submit" value="' . _("Translate") . '">';
378 translate_table_end();
381 function translate_form_intertran($message) {
382 translate_new_form('http://www.tranexp.com:2000/InterTran');
383 echo '<INPUT TYPE="hidden" NAME="topframe" VALUE="yes">'.
384 '<INPUT TYPE="hidden" NAME="type" VALUE="text">'.
385 "<input type=\"hidden\" name=\"text\" value=\"$message\">";
387 $left = '<SELECT name="from">' .
388 translate_lang_opt('pt_BR', '', 'pob', _("Brazilian Portuguese")).
389 translate_lang_opt('', '', 'bul', _("Bulgarian") . ' (CP 1251)').
390 translate_lang_opt('', '', 'cro', _("Croatian") . ' (CP 1250)').
391 translate_lang_opt('cs', '', 'che', _("Czech") . ' (CP 1250)').
392 translate_lang_opt('', '', 'dan', _("Danish")).
393 translate_lang_opt('nl', '', 'dut', _("Dutch")).
394 translate_lang_opt('en', '!en', 'eng', _("English")).
395 translate_lang_opt('', '', 'spe', _("European Spanish")).
396 translate_lang_opt('', '', 'fin', _("Finnish")).
397 translate_lang_opt('fr', '', 'fre', _("French")).
398 translate_lang_opt('de', '', 'ger', _("German")).
399 translate_lang_opt('', '', 'grk', _("Greek")).
400 translate_lang_opt('', '', 'hun', _("Hungarian") . ' (CP 1250)').
401 translate_lang_opt('', '', 'ice', _("Icelandic")).
402 translate_lang_opt('it', '', 'ita', _("Italian")).
403 translate_lang_opt('', '', 'jpn', _("Japanese") . ' (Shift JIS)').
404 translate_lang_opt('', '', 'spl', _("Latin American Spanish")).
405 translate_lang_opt('no*', '', 'nor', _("Norwegian")).
406 translate_lang_opt('pl', '', 'pol', _("Polish") . ' (ISO 8859-2)').
407 translate_lang_opt('', '', 'poe', _("Portuguese")).
408 translate_lang_opt('', '', 'rom', _("Romanian") . ' (CP 1250)').
409 translate_lang_opt('ru', '', 'rus', _("Russian") . ' (CP 1251)').
410 translate_lang_opt('', '', 'sel', _("Serbian") . ' (CP 1250)').
411 translate_lang_opt('', '', 'slo', _("Slovenian") . ' (CP 1250)').
412 translate_lang_opt('es', '', 'spa', _("Spanish")).
413 translate_lang_opt('sv', '', 'swe', _("Swedish")).
414 translate_lang_opt('', '', 'tur', _("Turkish") . ' (CP 1254)').
415 translate_lang_opt('', '', 'wel', _("Welsh")).
418 $right = '<SELECT name="to">'.
419 translate_lang_opt('', 'pt_BR', 'pob', _("Brazilian Portuguese")).
420 translate_lang_opt('', '', 'bul', _("Bulgarian") . ' (CP 1251)').
421 translate_lang_opt('', '', 'cro', _("Croatian") . ' (CP 1250)').
422 translate_lang_opt('', 'cs', 'che', _("Czech") . ' (CP 1250)').
423 translate_lang_opt('', '', 'dan', _("Danish")).
424 translate_lang_opt('', 'nl', 'dut', _("Dutch")).
425 translate_lang_opt('!en', 'en', 'eng', _("English")).
426 translate_lang_opt('', '', 'spe', _("European Spanish")).
427 translate_lang_opt('', '', 'fin', _("Finnish")).
428 translate_lang_opt('', 'fr', 'fre', _("French")).
429 translate_lang_opt('', 'de', 'ger', _("German")).
430 translate_lang_opt('', '', 'grk', _("Greek")).
431 translate_lang_opt('', '', 'hun', _("Hungarian") . ' (CP 1250)').
432 translate_lang_opt('', '', 'ice', _("Icelandic")).
433 translate_lang_opt('', 'it', 'ita', _("Italian")).
434 translate_lang_opt('', '', 'jpn', _("Japanese") . ' (Shift JIS)').
435 translate_lang_opt('', '', 'spl', _("Latin American Spanish")).
436 translate_lang_opt('', 'no*', 'nor', _("Norwegian")).
437 translate_lang_opt('', 'pl', 'pol', _("Polish") . ' (ISO 8859-2)').
438 translate_lang_opt('', '', 'poe', _("Portuguese")).
439 translate_lang_opt('', '', 'rom', _("Romanian") . ' (CP 1250)').
440 translate_lang_opt('', 'ru', 'rus', _("Russian") . ' (CP 1251)').
441 translate_lang_opt('', '', 'sel', _("Serbian") . ' (CP 1250)').
442 translate_lang_opt('', '', 'slo', _("Slovenian") . ' (CP 1250)').
443 translate_lang_opt('', 'es', 'spa', _("Spanish")).
444 translate_lang_opt('', 'sv', 'swe', _("Swedish")).
445 translate_lang_opt('', '', 'tur', _("Turkish") . ' (CP 1254)').
446 translate_lang_opt('', '', 'wel', _("Welsh")).
448 printf( _("%s to %s"), $left, $right );
449 echo 'InterTran: <input type=submit value="' . _("Translate") . '">';
451 translate_table_end();
454 function translate_form_gpltrans($message) {
455 translate_new_form('http://www.translator.cx/cgi-bin/gplTrans');
456 echo '<select name="language">'.
457 translate_lang_opt('nl', 'nl', 'dutch_dict', _("Dutch")).
458 translate_lang_opt('fr', 'fr', 'french_dict', _("French")).
459 translate_lang_opt('de', 'de', 'german_dict', _("German")).
460 translate_lang_opt('', '', 'indonesian_dict', _("Indonesian")).
461 translate_lang_opt('it', 'it', 'italian_dict', _("Italian")).
462 translate_lang_opt('', '', 'latin_dict', _("Latin")).
463 translate_lang_opt('pt', 'pt', 'portuguese_dict', _("Portuguese")).
464 translate_lang_opt('es', 'es', 'spanish_dict', _("Spanish")).
466 echo '<select name="toenglish">';
467 echo '<option value="yes" >'. _("to English") . '</option>';
468 echo '<option value="no" selected>' . _("from English") . '</option></select>';
469 echo "<input type=hidden name=text value=\"$message\">".
470 'GPLTrans: <input type="submit" value="' . _("Translate") . '">';
472 translate_table_end();
475 function translate_form_dictionary($message) {
476 translate_new_form('http://dictionary.reference.com/translate/text.html');
477 list($usec, $sec) = explode(" ",microtime());
478 $time = $sec . (float)$usec*100000000;
479 echo "<input type=hidden name=text value=\"$message\">".
480 "<input type=hidden name=r value=\"$time\">".
481 '<SELECT NAME="lp">'.
482 translate_lang_opt('en', 'fr', 'en_fr',
483 sprintf( _("%s to %s"),
486 translate_lang_opt('', 'ge', 'en_ge',
487 sprintf( _("%s to %s"),
490 translate_lang_opt('', 'it', 'en_it',
491 sprintf( _("%s to %s"),
494 translate_lang_opt('', 'pt*', 'en_pt',
495 sprintf( _("%s to %s"),
498 translate_lang_opt('', 'es', 'en_es',
499 sprintf( _("%s to %s"),
502 translate_lang_opt('fr', '', 'fr_en',
503 sprintf( _("%s to %s"),
506 translate_lang_opt('', '', 'fr_ge',
507 sprintf( _("%s to %s"),
510 translate_lang_opt('', '', 'ge_fr',
511 sprintf( _("%s to %s"),
514 translate_lang_opt('de', '', 'ge_en',
515 sprintf( _("%s to %s"),
518 translate_lang_opt('it', '', 'it_en',
519 sprintf( _("%s to %s"),
522 translate_lang_opt('pt*', '', 'pt_en',
523 sprintf( _("%s to %s"),
526 translate_lang_opt('es', '', 'es_en',
527 sprintf( _("%s to %s"),
531 'Dictionary.com: <INPUT TYPE="submit" VALUE="'._("Translate").'">';
533 translate_table_end();
536 function translate_form_otenet($message) {
537 translate_new_form('http://systran.otenet.gr/cgi-bin/systran.cgi');
539 <input type
="hidden" name
="doit" value
="done">
540 <INPUT NAME
="partner" VALUE
="OTEnet-en" type
="hidden">
541 <input type
="hidden" name
="urltext" value
="<?php echo $message; ?>">
542 <select name
="lp" size
="1"><?php
543 echo translate_lang_opt('en', 'el', 'en_el',
544 sprintf( _("%s to %s"),
547 translate_lang_opt('el', 'en', 'el_en',
548 sprintf( _("%s to %s"),
551 translate_lang_opt('fr', 'el', 'fr_el',
552 sprintf( _("%s to %s"),
555 translate_lang_opt('el', 'fr', 'el_fr',
556 sprintf( _("%s to %s"),
559 translate_lang_opt('#', '', '', "----------------") .
560 translate_lang_opt('en', 'fr', 'en_fr',
561 sprintf( _("%s to %s"),
564 translate_lang_opt('fr', 'en', 'fr_en',
565 sprintf( _("%s to %s"),
568 translate_lang_opt('en', 'de', 'en_de',
569 sprintf( _("%s to %s"),
572 translate_lang_opt('de', 'en', 'de_en',
573 sprintf( _("%s to %s"),
576 translate_lang_opt('en', 'es', 'en_es',
577 sprintf( _("%s to %s"),
580 translate_lang_opt('es', 'en', 'es_en',
581 sprintf( _("%s to %s"),
584 translate_lang_opt('en', 'it', 'en_it',
585 sprintf( _("%s to %s"),
588 translate_lang_opt('it', 'en', 'it_en',
589 sprintf( _("%s to %s"),
592 translate_lang_opt('en', 'pt', 'en_pt',
593 sprintf( _("%s to %s"),
596 translate_lang_opt('pt', 'en', 'pt_en',
597 sprintf( _("%s to %s"),
600 translate_lang_opt('fr', 'de', 'fr_de',
601 sprintf( _("%s to %s"),
604 translate_lang_opt('de', 'fr', 'de_fr',
605 sprintf( _("%s to %s"),
608 translate_lang_opt('fr', 'es', 'fr_es',
609 sprintf( _("%s to %s"),
612 translate_lang_opt('es', 'fr', 'es_fr',
613 sprintf( _("%s to %s"),
616 translate_lang_opt('fr', 'nl', 'fr_nl',
617 sprintf( _("%s to %s"),
620 translate_lang_opt('nl', 'fr', 'nl_fr',
621 sprintf( _("%s to %s"),
625 'OTEnet: <input type="Submit" value="' . _("Translate") . '">';
626 translate_table_end();
630 function translate_form_promt($message) {
631 translate_new_form('http://www.translate.ru/text.asp#tr_form');
632 echo '<input type="hidden" name="status" value="translate">';
633 echo "<input type=\"hidden\" name=\"source\" value=\"$message\">";
634 echo _("Interface language")." : ";
635 echo "<select size=\"1\" name=\"lang\">\n";
636 echo "<option value=\"en\">" . _("English") . "</option>\n";
637 echo "<option value=\"ru\">" . _("Russian") . "</option>\n";
638 echo "<option value=\"de\">" . _("German") . "</option>\n";
639 echo "<option value=\"fr\">" . _("French") . "</option>\n";
640 echo "<option value=\"es\">" . _("Spanish") . "</option>\n";
641 echo "</select><br>\n";
642 echo _("Translation direction")." : ";
643 echo '<select size="1" id="direction" name="direction">';
644 echo translate_lang_opt('en', 'ru', 'er',
645 sprintf( _("%s to %s"),
648 translate_lang_opt('ru', 'en', 're',
649 sprintf( _("%s to %s"),
652 translate_lang_opt('de', 'ru', 'gr',
653 sprintf( _("%s to %s"),
656 translate_lang_opt('ru', 'de', 'rg',
657 sprintf( _("%s to %s"),
660 translate_lang_opt('fr', 'ru', 'fr',
661 sprintf( _("%s to %s"),
664 translate_lang_opt('ru', 'fr', 'rf',
665 sprintf( _("%s to %s"),
668 translate_lang_opt('es', 'ru', 'sr',
669 sprintf( _("%s to %s"),
672 translate_lang_opt('it', 'ru', 'ir',
673 sprintf( _("%s to %s"),
676 translate_lang_opt('en', 'de', 'eg',
677 sprintf( _("%s to %s"),
680 translate_lang_opt('de', 'en', 'ge',
681 sprintf( _("%s to %s"),
684 translate_lang_opt('en', 'es', 'es',
685 sprintf( _("%s to %s"),
688 translate_lang_opt('es', 'en', 'se',
689 sprintf( _("%s to %s"),
692 echo "</select><br>\n";
693 echo "<input type=\"hidden\" name=\"template\" value=\"General\">\n";
694 echo 'PROMT: <input type="submit" value="' . _("Translate") . '">';
696 translate_table_end();