6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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.
16 Languages from i18n, incorporated in the auto-language selection:
18 no - Norwegian (Bokmål)
19 no_NO_ny - Norwegian (Nynorsk)
25 pt_BR - Portuguese (Brazil)
34 /* Initialize the translation plugin */
35 function squirrelmail_plugin_init_translate() {
36 global $squirrelmail_plugin_hooks;
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';
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;
52 if (!$translate_show_read) {
56 $translate_dir = 'to';
58 $trans_ar = $message->findDisplayEntity(array(), array('text/plain'));
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);
64 $hookResults = do_hook('message_body', $body);
65 $body = $hookResults[1];
67 $body = 'Message can\'t be translated';
71 $pos = strpos($new_body,
72 '">'. _("Download this as a file") . '</A></CENTER><BR></SMALL>');
74 $new_body = substr($new_body, 0, $pos);
77 $trans = get_html_translation_table(HTML_ENTITIES
);
78 $trans[' '] = ' ';
79 $trans = array_flip($trans);
80 $new_body = strtr($new_body, $trans);
82 $new_body = urldecode($new_body);
83 $new_body = strip_tags($new_body);
85 /* I really don't like this next part ... */
86 $new_body = str_replace('"', "''", $new_body);
87 $new_body = strtr($new_body, "\n", ' ');
89 $function = 'translate_form_' . $translate_server;
93 function translate_table_end() {
105 function translate_button() {
106 global $translate_show_send;
108 if (! $translate_show_send) {
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?"),
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;
130 $translate_server = getPref($data_dir, $username, 'translate_server');
131 if ($translate_server == '') {
132 $translate_server = 'babelfish';
135 $translate_location = getPref($data_dir, $username, 'translate_location');
136 if ($translate_location == '') {
137 $translate_location = 'center';
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');
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.
151 function translate_does_it_match_language($test) {
152 global $squirrelmail_language;
158 if (! $test ||
! $squirrelmail_language) {
162 if ($test[$index] == '!') {
168 if (($index == 0) && ($test == $squirrelmail_language)) {
172 while (isset($test[$index]) && $test[$index]) {
173 if ($test[$index] == '*') {
176 if ($test[$index] != $squirrelmail_language[$smindex]) {
187 function translate_lang_opt($from, $to, $value, $text) {
188 global $translate_dir;
190 $ret = ' <option value="' . $value . '"';
192 if (translate_does_it_match_language($to) && ($translate_dir == 'to')) {
196 if (translate_does_it_match_language($from) && ($translate_dir == 'from')) {
200 $ret .= '>' . $text . "</option>\n";
206 function translate_new_form($action) {
207 global $translate_dir, $translate_new_window, $translate_location;
208 global $color, $translate_same_window;
210 echo '<form action="';
212 if ($translate_dir == 'to') {
215 echo 'translate.php';
218 echo '" method="post"';
220 if (!$translate_same_window) {
221 echo ' target="_blank"';
226 ?
><table align
="<?php echo $translate_location ?>" cellpadding
=3 cellspacing
=0 border
=0 bgcolor
=<?php
echo $color[10] ?
>>
229 <table cellpadding
=2 cellspacing
=1 border
=0 bgcolor
="<?php echo $color[5] ?>">
234 function translate_form_babelfish($message) {
235 translate_new_form('http://babelfish.altavista.com/babelfish/tr');
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"),
246 translate_lang_opt('en', 'fr', 'en_fr',
247 sprintf( _("%s to %s"),
250 translate_lang_opt('', 'de', 'en_de',
251 sprintf( _("%s to %s"),
254 translate_lang_opt('', 'it', 'en_it',
255 sprintf( _("%s to %s"),
258 translate_lang_opt('en', 'ja', 'en_ja',
259 sprintf( _("%s to %s"),
262 translate_lang_opt('en', 'ko', 'en_ko',
263 sprintf( _("%s to %s"),
266 translate_lang_opt('', 'pt*', 'en_pt',
267 sprintf( _("%s to %s"),
270 translate_lang_opt('', 'es', 'en_es',
271 sprintf( _("%s to %s"),
274 translate_lang_opt('zh', 'en', 'zh_en',
275 sprintf( _("%s to %s"),
278 translate_lang_opt('fr', 'en', 'fr_en',
279 sprintf( _("%s to %s"),
282 translate_lang_opt('de', '', 'de_en',
283 sprintf( _("%s to %s"),
286 translate_lang_opt('it', '', 'it_en',
287 sprintf( _("%s to %s"),
290 translate_lang_opt('ja', 'en', 'ja_en',
291 sprintf( _("%s to %s"),
294 translate_lang_opt('ko', 'en', 'ko_en',
295 sprintf( _("%s to %s"),
298 translate_lang_opt('pt*', '', 'pt_en',
299 sprintf( _("%s to %s"),
302 translate_lang_opt('es', '', 'es_en',
303 sprintf( _("%s to %s"),
306 translate_lang_opt('', '', 'de_fr',
307 sprintf( _("%s to %s"),
310 translate_lang_opt('', '', 'fr_de',
311 sprintf( _("%s to %s"),
314 translate_lang_opt('ru', '', 'ru_en',
315 sprintf( _("%s to %s"),
319 'Babelfish: <input type="Submit" value="' . _("Translate") . '">';
321 translate_table_end();
324 function translate_form_go($message) {
325 translate_new_form('http://translator.go.com/cb/trans_entry');
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"),
333 translate_lang_opt('', 'fr', 'en_fr',
334 sprintf( _("%s to %s"),
337 translate_lang_opt('', 'de', 'en_ge',
338 sprintf( _("%s to %s"),
341 translate_lang_opt('', 'it', 'en_it',
342 sprintf( _("%s to %s"),
345 translate_lang_opt('', 'pt', 'en_pt',
346 sprintf( _("%s to %s"),
349 translate_lang_opt('es', 'en', 'sp_en',
350 sprintf( _("%s to %s"),
353 translate_lang_opt('fr', '', 'fr_en',
354 sprintf( _("%s to %s"),
357 translate_lang_opt('de', '', 'ge_en',
358 sprintf( _("%s to %s"),
361 translate_lang_opt('it', '', 'it_en',
362 sprintf( _("%s to %s"),
365 translate_lang_opt('pt', '', 'pt_en',
366 sprintf( _("%s to %s"),
370 "<input type=\"hidden\" name=\"text\" value=\"$message\">".
371 'Go.com: <input type="Submit" value="' . _("Translate") . '">';
373 translate_table_end();
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\">";
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")).
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")).
443 printf( _("%s to %s"), $left, $right );
444 echo 'InterTran: <input type=submit value="' . _("Translate") . '">';
446 translate_table_end();
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")).
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") . '">';
467 translate_table_end();
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"),
481 translate_lang_opt('', 'ge', 'en_ge',
482 sprintf( _("%s to %s"),
485 translate_lang_opt('', 'it', 'en_it',
486 sprintf( _("%s to %s"),
489 translate_lang_opt('', 'pt*', 'en_pt',
490 sprintf( _("%s to %s"),
493 translate_lang_opt('', 'es', 'en_es',
494 sprintf( _("%s to %s"),
497 translate_lang_opt('fr', '', 'fr_en',
498 sprintf( _("%s to %s"),
501 translate_lang_opt('', '', 'fr_ge',
502 sprintf( _("%s to %s"),
505 translate_lang_opt('', '', 'ge_fr',
506 sprintf( _("%s to %s"),
509 translate_lang_opt('de', '', 'ge_en',
510 sprintf( _("%s to %s"),
513 translate_lang_opt('it', '', 'it_en',
514 sprintf( _("%s to %s"),
517 translate_lang_opt('pt*', '', 'pt_en',
518 sprintf( _("%s to %s"),
521 translate_lang_opt('es', '', 'es_en',
522 sprintf( _("%s to %s"),
526 'Dictionary.com: <INPUT TYPE="submit" VALUE="'._("Translate").'">';
528 translate_table_end();
531 function translate_form_otenet($message) {
532 translate_new_form('http://systran.otenet.gr/cgi-bin/systran.cgi');
534 <input type
="hidden" name
="doit" value
="done">
535 <INPUT NAME
="partner" VALUE
="OTEnet-en" type
="hidden">
536 <input type
="hidden" name
="urltext" value
="<?php echo $message; ?>">
537 <select name
="lp" size
="1"><?php
538 echo translate_lang_opt('en', 'el', 'en_el',
539 sprintf( _("%s to %s"),
542 translate_lang_opt('el', 'en', 'el_en',
543 sprintf( _("%s to %s"),
546 translate_lang_opt('fr', 'el', 'fr_el',
547 sprintf( _("%s to %s"),
550 translate_lang_opt('el', 'fr', 'el_fr',
551 sprintf( _("%s to %s"),
555 'OTEnet: <input type="Submit" value="' . _("Translate") . '">';
556 translate_table_end();