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