e5164f242702eab751fcc76f9f3f8557a8d616cf
[atutor.git] / mods / wiki / plugins / action / translation.php
1 <?php
2
3 /*
4    This plugin provides translation of the currently shown page via the
5    Google, Altavista/Babelfish or InterTran online services. The services
6    support different source and destination languages. The bottleneck of
7    this plugin however is the detection of the language, the current page is
8    in (only detectes English, Spanish, French, German and Italian, Portuguese
9    very poorly). So you shouldn't forget to correct EWIKI_DEFAULT_LANG for
10    your Wiki setup.
11 */
12
13
14 #-- order of this array marks the preference
15 $language_service = array(
16    "google" => array("http://translate.google.com/translate_p?hl=en&ie=ISO-8859-1&prev=/language_tools", "langpair", "u"),
17    "babelfish" => array("http://babelfish.altavista.com/babelfish/urlload?tt=url", "lp", "url"),
18    "intertran" => array("http://intertran.tranexp.com.com/Translate/index.shtml&type=url", array("from", "to"), "url"),
19 );
20 $language_detect = array(
21    "en" => "the to of is and this for be if you with an or it on not as that by will are see also can from use only file your at note may no which all false when about has have new one true any else more should without into first these own but must there do other",
22    ".en" => "ing ght y",
23    "de" => "die der und mit das sie den ist wird von eine im ein zu des auf dem werden oder aus wie datei es als nicht nen bei auch wenn einer einen an sind zur sich einem daß dass diese um zum nur gibt eines kann nach beim dann also dieser durch haben hat dabei alle jedoch noch aber dazu er diesem soll keine",
24    ".de" => "en ag",
25    "*de" =>  "isch cht wert ung ä ö ü ß",
26    "es" => "de en la el para que un una no los con por es se del como su las puede al página si ud texto ser sobre lo gina ginas esta entre más debe hay dos son este sus est nueva esto desde versi sin tiene pero dise cual uno sea nuevo mismo nea usar caso as pues ha eso all mucho bien dar estas estar tema tan desee aún nz os hace otros ya muy uno tulo nuevos deber poco le esa tal menos cap buena qu muchos hacer trav modo vaya tres lea nada vez vea ar met ve mi il út así está ese deja bajo sino muchas an qui está eso tema",
27    "fr" => "de la les le des pour est et l un en vous une sur dans du que par il qui es pas avec ne ce ou tre sont plus au qu me comme donn acc on pr cette peut tout si mais re se votre cela bien je sans autres aux vos cr ces ont fa fait puis cas avez ment ils temps autre sous mes fen donc ainsi tous alors apr te doit son chez deux tant res leur devez soit tat moins sa nom non ci faut peu avant avoir elle ses mot tes vue ceci elles bas ois ma er tait voulez ceux ex quoi",
28    "*fr" => "è",
29    "it" => "gli di nel ed per i della sono dell e con pi",
30    ".it" => "zion zione zioni zie zia ierie i",
31    "pt" => "do seu seus",
32    ".pt" => "ê",
33 );
34 $language_comb = array(
35    "google" => array(
36       "en|es", "en|fr", "en|de", "en|it", "en|pt",
37       "es|en", "fr|en", "de|en", "it|en", "pt|en",
38       "fr|de", "de|fr",
39    ),
40    "babelfish" => array(
41       "en|zh", "en|fr", "en|de", "en|it", "en|ja", "en|ko", "en|pt", "en|es",
42       "zh|en", "fr|en", "de|en", "it|en", "ja|en", "ko|en", "pt|en", "es|en",
43       "ru|en", "fr|de", "de|fr",
44    ),
45    "intertran" => array(
46       "*|*",
47    ),
48 );
49 $language_names = array(  // for intertran
50    "ch" => "chi",   "en" => "eng",   "fr" => "fre",   "it" => "ita",
51    "jp" => "jpn",   "nl" => "dut",   "po" => "pol",   "pt" => "poe",
52    "ru" => "rus",   "de" => "ger",   "ed" => "spa",   "lt" => "ltt",
53 );
54 $ewiki_t["C"]["en"] = "English";
55 $ewiki_t["C"]["de"] = "German";
56 $ewiki_t["C"]["fr"] = "French";
57 $ewiki_t["C"]["es"] = "Spanish";
58 $ewiki_t["C"]["it"] = "Italian";
59 $ewiki_t["C"]["pt"] = "Portuguese";
60
61
62
63 $ewiki_plugins["handler"][] = "ewiki_add_action_link_for_translation";
64
65
66
67 function ewiki_add_action_link_for_translation($id, &$data, $action) {
68
69    global $language_comb, $language_service, $language_names, $ewiki_t,
70           $ewiki_config;
71
72    $o = "";
73    $url = "";
74
75    if ($data["version"]) {
76       $lang_src = ewiki_guess_lang($data["content"]);
77
78       #-- check if page is already in desired language
79       if ($ewiki_t["languages"][0] == $lang_src) {
80       }
81       else {
82
83          foreach ($ewiki_t["languages"] as $lang_dest) {
84
85             $url = "";
86             $comb = "$lang_src|$lang_dest";
87
88             foreach ($language_service as $SERVICE=>$params) {
89
90                if (in_array($comb, $language_comb[$SERVICE]) || ($SERVICE=="intertran")) {
91                   if ($SERVICE == "babelfish") {
92                      $lp = "&" . $params[1] . "=" . strtr($comb, "|", "_");
93                   }
94                   elseif ($SERVICE == "google") {
95                      $lp = "&" . $params[1] . "=" . $comb;
96                   }
97                   else {
98                      $from = $language_names[strtok($comb, "|")];
99                      $to = $language_names[strtok("|")];
100                      if (!$from || !$to) { 
101                         continue;
102                      }
103                      $lp = "&" . $params[1][0] . "=" . $from
104                          . "&" . $params[1][1] . "=" . $to;
105                   }
106                   $url = $params[0] . $lp
107                        . "&" . $params[2] . "="
108                        . urlencode( ewiki_script($action, $id, "", 0, 0,
109                                                  ewiki_script_url())     );
110                   break;
111                }
112             }
113
114             #-- add translation link to page
115             if ($url) {
116                $ewiki_config["action_links"]["view"][$url] = "TranslateInto" . $ewiki_t["C"][$lang_dest];
117                /*---
118                   $o = "<br /><a class=\"tool-button\" href=\""
119                   . $url . "\">"
120                   . "TranslateInto" . $ewiki_t["C"][$lang_dest]
121                   . "</a>\n";
122                ---*/
123                break;
124             }
125
126          }
127       }
128    }
129 /*---
130    return($o);
131 ---*/
132 }
133
134
135
136 function ewiki_guess_lang(&$data) {
137
138    global $language_detect;
139
140    #-- prepare
141    $detect = array(
142       "en"=>0,
143       "de"=>0,
144    );
145
146    #-- separate words in text page
147    $text = strtr(
148       "  $data ",
149       "\t\n\r\f_<>\$,.;!()[]{}/",
150       "                        "
151    );
152
153    #-- search for words in text
154    foreach ($language_detect as $lang => $word_str) {
155       foreach (explode(" ", $word_str) as $word) {
156          switch ($lang[0]) {
157             case ".":
158                $word = "$word ";
159             case "*":
160                $lang = substr($lang, 1);
161                break;
162             default:
163                $word = " $word ";
164          }
165          $l = -1;
166          while ($l = strpos($text, $word, $l+1)) {
167             $detect[$lang]++;
168          }
169       }
170    }
171
172    #-- get entry with most counts
173    $lang = EWIKI_DEFAULT_LANG;
174    arsort($detect);
175    $keys = array_keys($detect);
176    if (array_shift($detect) >= 5) {
177       $lang = array_shift($keys);
178    }
179
180    return($lang);
181 }
182
183
184
185 ?>