]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/_WikiTranslation.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / _WikiTranslation.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * _WikiTranslation:  Display pagenames and other strings in various languages.
25  * Can also be used to let a favorite translation service translate a whole page.
26  * Current favorite: translate.google.com if from_lang = en or fr
27  *
28  * Examples:
29  *  <?plugin _WikiTranslation page=HomePage languages=fr ?>
30  *     Translation service for HomePage into french (redirect to translate.google.com)
31  *  <?plugin _WikiTranslation what=pages ?>
32  *     Translation matrix of all pages with proper translations (all in pgsrc)
33  *  <?plugin _WikiTranslation what=wikiwords match="W*" limit=20 ?>
34  *     Translation matrix of the first 20 wikiwords matching "W*"
35  *  <?plugin _WikiTranslation string=HomePage languages=fr,de,sv ?>
36  *     Translation matrix for all given languages
37  *  <?plugin _WikiTranslation string=HomePage ?>
38  *     Translation matrix for all supported languages
39  *  <?plugin _WikiTranslation string=HomePage languages=fr ?>
40  *     Just return the translated string for this language.
41  *
42  * @author:  Reini Urban
43  */
44
45 /* Container for untranslated pagenames. Needed to show up in locale/po/phpwiki.pot */
46 $pgsrc_container =
47     _("AddCommentPlugin")  .','.
48     _("AddingPages")  .','.
49     _("AllPagesCreatedByMe")  .','.
50     _("AllPagesLastEditedByMe")  .','.
51     _("AllPagesOwnedByMe")  .','.
52     _("AuthorHistoryPlugin") .','.
53     _("BackLinks") .','.
54     _("CalendarListPlugin") .','.
55     _("CalendarPlugin") .','.
56     _("CategoryCategory")  .','.
57     _("CategoryHomePages")  .','.
58     _("CommentPlugin")  .','.
59     _("CreateTocPlugin")  .','.
60     _("DebugInfo") .','.
61     _("EditMetaData") .','.
62     _("EditMetaDataPlugin") .','.
63     _("ExternalSearchPlugin") .','.
64     _("FindPage") .','.
65     _("FoafViewerPlugin") .','.
66     _("FrameIncludePlugin") .','.
67     _("FullRecentChanges") .','.
68     _("HelloWorldPlugin") .','.
69     _("HomePageAlias") .','.
70     _("IncludePagePlugin") .','.
71     _("InterWiki") .','.
72     _("LinkIcons") .','.
73     _("MagicPhpWikiURLs") .','.
74     _("MoreAboutMechanics") .','.
75     _("NewMarkupTestPage") .','.
76     _("OldMarkupTestPage") .','.
77     _("OldStyleTablePlugin") .','.
78 //  _("PageDump") .','.
79     _("PageGroupTest") .','.
80     _("PageGroupTest/Four") .','.
81     _("PageGroupTest/One") .','.
82     _("PageGroupTest/Three") .','.
83     _("PageGroupTest/Two") .','.
84     _("PgsrcTranslation") .','.
85     _("PhotoAlbumPlugin") .','.
86     _("PhpHighlightPlugin") .','.
87     _("PhpWeatherPlugin") .','.
88     _("PhpWiki") .','.
89     _("PhpWikiAdministration/Chmod") .','.
90     _("PhpWikiAdministration/Chown") .','.
91     _("PhpWikiAdministration/Remove") .','.
92     _("PhpWikiAdministration/Rename") .','.
93     _("PhpWikiAdministration/Replace") .','.
94     _("PhpWikiAdministration/SetAcl") .','.
95     _("PhpWikiDocumentation") .','.
96     _("PhpWikiPoll") .','.
97     _("PloticusPlugin") .','.
98     _("PgsrcTranslation") .','.
99     _("PgsrcTranslation/de") .','.
100     _("PgsrcTranslation/fr") .','.
101     _("PgsrcTranslation/it") .','.
102     _("PgsrcTranslation/es") .','.
103     _("PgsrcTranslation/nl") .','.
104     _("PgsrcTranslation/sv") .','.
105     _("PgsrcTranslation/ja") .','.
106     _("PgsrcTranslation/zh") .','.
107     _("RawHtmlPlugin") .','.
108     _("RecentVisitors") .','.
109     _("RedirectToPlugin") .','.
110     _("ReleaseNotes") .','.
111     _("RichTablePlugin") .','.
112 //    _("SpellCheck") .','.
113     _("SteveWainstead") .','.
114     _("SystemInfoPlugin") .','.
115     _("TranscludePlugin") .','.
116     _("TranslateText") .','.
117     _("UnfoldSubpagesPlugin") .','.
118     _("UpLoad") .','.
119     _("UpLoadPlugin") .','.
120     _("WabiSabi") .','.
121     _("WikiBlogPlugin") .','.
122     _("WikiPlugin") .','.
123     _("WikiWikiWeb");
124
125 require_once('lib/PageList.php');
126
127 class WikiPlugin__WikiTranslation
128 extends WikiPlugin
129 {
130
131     function getName() {
132         return _("_WikiTranslation");
133     }
134
135     function getDescription() {
136         return _("Show translations of various words or pages");
137     }
138
139     function getVersion() {
140         return preg_replace("/[Revision: $]/", '',
141                             "\$Revision$");
142     }
143
144     function getDefaultArguments() {
145         return array_merge
146             (
147              PageList::supportedArgs(),
148              array( 'languages'  => '',  // comma delimited string of de,en,sv,...
149                     'string'     => '',
150                     'page'       => '',  // use a translation service
151                     'what'       => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
152
153                     'match'         => '*',
154                     'from_lang'     => false,
155                     'include_empty' => false,
156                     //'exclude'       => '',
157                     //'sortby'        => '',
158                     //'limit'         => 0,
159                     'nolinks'       => false,  // don't display any links
160                                                    // (for development only)
161                     'noT'           => false   // don't display the T link
162                                                   // (for development only)
163                     ));
164     }
165
166     function init_locale($lang) {
167         if ($lang != $this->lang)
168             update_locale($lang);
169         if ($lang == 'en') {
170             // Hack alert! we need hash for stepping through it, even if it's
171             // in the wrong language
172             include (FindFile("locale/de/LC_MESSAGES/phpwiki.php", 0,'reinit'));
173             foreach ($locale as $en => $de) {
174                     $locale[$en] = $en;
175             }
176         // gettext module loaded: must load the LC_MESSAGES php hash
177         } elseif (function_exists ('bindtextdomain')) {
178             include (FindFile("locale/$lang/LC_MESSAGES/phpwiki.php", 0,'reinit'));
179             //include (FindLocalizedFile("LC_MESSAGES/phpwiki.php", 0,'reinit'));
180         // we already have a $locale, but maybe it's in the wrong language
181         } elseif ($lang != $this->lang or empty($GLOBALS['locale'])) {
182             include (FindFile("locale/$lang/LC_MESSAGES/phpwiki.php", 0,'reinit'));
183         } else {
184            $locale = & $GLOBALS['locale'];
185         }
186         $this->_locales[$lang] = $locale;
187     }
188
189     // reverse translation:
190     function translate_to_en($text, $lang=false) {
191         if (!$lang) $lang = $this->lang; // current locale
192         if ($lang == 'en') return $text;
193
194         $this->_locales = array();
195         $this->_reverse_locales = array();
196
197         if (!isset($this->_locales[$lang])) {
198             $this->init_locale($lang);
199         }
200         assert(!empty($this->_locales[$lang]));
201         if (!isset($this->_reverse_locales[$lang])) {
202             // and now do a reverse lookup in the $locale hash
203             $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
204         }
205         if (!empty($this->_reverse_locales[$lang][$text])) {
206             return $this->_reverse_locales[$lang][$text];
207         } else {
208             return $text;
209         }
210     }
211
212     /**
213      * setlocale() switching with the gettext extension is by far too slow.
214      * So use the hash regardless if gettext is loaded or not.
215      */
216     function fast_translate($text, $to_lang, $from_lang=false) {
217         if (!$from_lang) $from_lang = $this->lang; // current locale
218         if ($from_lang == $to_lang) return $text;
219         // setup hash from en => to_lang
220         if (!isset($this->_locales[$to_lang]))
221             $this->init_locale($to_lang);
222         if ($from_lang != 'en') {
223             // get reverse gettext: translate to english
224             $text = $this->translate_to_en($text, $from_lang);
225         }
226         return !empty($this->_locales[$to_lang][$text])
227                  ? $this->_locales[$to_lang][$text]
228                  : $text;
229     }
230
231     //FIXME! There's something wrong.
232     function translate($text, $to_lang, $from_lang=false) {
233         if (!$from_lang) $from_lang = $this->lang; // current locale
234         if ($from_lang == $to_lang) return $text;
235         // Speed up hash lookup. Not needed for gettext module
236         if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
237             $this->init_locale($from_lang);
238         }
239         if ($from_lang != 'en') {
240             // get reverse gettext: translate to english
241             $en = $this->translate_to_en($text, $from_lang);
242             // and then to target
243             update_locale($to_lang);
244             $result = gettext($en);
245             update_locale($from_lang);
246         } else {
247             // locale switching is very slow with the gettext extension.
248             // better use fast_translate
249             if ($from_lang != $to_lang) {
250                 update_locale($to_lang);
251             }
252             $result = gettext($text);
253             if ($from_lang != $to_lang) {
254                 update_locale($from_lang);
255             }
256         }
257         return $result;
258     }
259
260     function run($dbi, $argstr, &$request, $basepage) {
261         $this->args = $this->getArgs($argstr, $request);
262         extract($this->args);
263         $this->request = &$request;
264         if (!$from_lang) $from_lang = $request->getPref('lang');
265         if (!$from_lang) $from_lang = $GLOBALS['LANG'];
266         $this->lang = $from_lang;
267
268         if (empty($languages)) {
269             $available_languages = listAvailableLanguages();
270             if ($from_lang == 'en') {
271                 // "en" is always the first.
272                 array_shift($available_languages);
273             }
274             // put from_lang to the very end.
275             if (in_array($from_lang, $available_languages))
276                 $languages = $available_languages;
277             else
278                     $languages = array_merge($available_languages, array($from_lang));
279         } elseif (strstr($languages,',')) {
280             $languages = explode(',', $languages);
281         } else {
282             $languages = array($languages);
283         }
284         if (in_array('zh', $languages) or in_array('ja', $languages)) {
285
286             // If the current charset != utf-8 the text will not be displayed correctly.
287             // But here we cannot change the header anymore. So we can decide to ignore them,
288             // or display them with all the errors.
289             //FIXME: do iconv the ob
290             if ($GLOBALS['charset'] != 'utf-8' and !defined('NEED_ICONV_TO')) {
291                     define('NEED_ICONV_TO', 'utf-8');
292                     //either the extension or external
293                 //$GLOBALS['charset'] = 'utf-8';
294             }
295         }
296         $to_lang = $languages[0];
297         if (!empty($string) and count($languages)==1) {
298             return $this->translate($string, $to_lang, $from_lang);
299         }
300         if (!empty($page)) {
301             $pagename = $page;
302             if ($dbi->isWikiPage($pagename)) {
303                     $url = '';
304                     // google can only translate from english and french
305                     if (in_array($from_lang, array('en', 'fr'))) {
306                     $url = "http://translate.google.com/translate";
307                     $url .= "?langpair=" . urlencode($from_lang."|".$to_lang);
308                     $url .= "&u=" . urlencode(WikiURL($pagename, false, true));
309                     }
310                     // redirect or transclude?
311                 if ($url) {
312                     return $request->redirect($url);
313                 }
314                     return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
315                                     WikiURL($pagename, false, true),
316                                     $to_lang));
317             } else {
318                 return $this->error(fmt("%s is empty",$pagename));
319             }
320         }
321
322         $pagelist = new PageList('', $exclude, $this->args);
323         $pagelist->_columns[0]->_heading = "$from_lang";
324         foreach ($languages as $lang) {
325             if ($lang == $from_lang) continue;
326             $field = "custom:$lang";
327             $pagelist->addColumnObject (
328               new _PageList_Column_customlang($field, $from_lang, $this));
329         }
330         if (!empty($string)) {
331             $pagelist->addPage( $string );
332             return $pagelist;
333         }
334         switch ($what) {
335         case 'allpages':
336             $pagelist->addPages( $dbi->getAllPages($include_empty, $sortby,
337                                                    $limit, $exclude) );
338             break;
339         case 'pages':
340             // not all pages, only the pgsrc pages
341             if (!is_array($exclude))
342                 $exclude = $pagelist->explodePageList($exclude, false, $sortby,
343                                                       $limit, $exclude);
344             $path = FindLocalizedFile(WIKI_PGSRC);
345             $pgsrc = new fileSet($path);
346             foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
347                 $pagename = urldecode($pagename);
348                 if (substr($pagename,-1,1) == '~') continue;
349                 if (in_array($pagename, $exclude))
350                     continue;             // exclude page.
351                 if ($match != '*' and !glob_match($match, $pagename))
352                     continue;
353                 $page_handle = $dbi->getPage($pagename);
354                 $pagelist->addPage( $page_handle );
355             }
356             break;
357         case 'wikiwords':
358             if (!isset($this->_locales[$from_lang])) {
359                 $this->init_locale($from_lang);
360             }
361             $locale = & $this->_locales[$from_lang];
362             if (is_array($locale)) {
363                 $count = 0;
364                 foreach ($locale as $from => $to) {
365                     if ($match != '*' and !glob_match($match, $from))
366                         continue;
367                     if (isWikiWord($from)) {
368                         $count++;
369                         $pagelist->addPage( $from );
370                         if ($limit and $count > $limit) break;
371                     }
372                 }
373             }
374             break;
375         // all Button texts, which need a localized .png
376         // where to get them from? templates/*.tmpl: Button()
377         // and WikiLink(?,'button')
378         // navbar links, actionpages, and admin requests
379         case 'buttons':
380             $buttons = $GLOBALS['AllActionPages'];
381             $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
382                                    "*.png");
383             foreach ($fileset->getFiles() as $file) {
384                 $b = urldecode(substr($file, 0, -4));
385                 if (!in_array($b,$buttons))
386                     $buttons[] = $b;
387             }
388             $count = 0;
389             foreach ($buttons as $button) {
390                 $pagelist->addPage( $button );
391                 if ($limit and ++$count > $limit) break;
392             }
393             break;
394         }
395         return $pagelist;
396     }
397 };
398
399 class _PageList_Column_customlang extends _PageList_Column {
400     function _PageList_Column_customlang($field, $from_lang, $plugin) {
401         $this->_field = $field;
402         $this->_from_lang = $from_lang;
403         $this->_plugin =& $plugin;
404         $this->_what = $plugin->args['what'];
405         $this->_noT = $plugin->args['noT'];
406         $this->_nolinks = $plugin->args['nolinks'];
407         $this->_iscustom = substr($field, 0, 7) == 'custom:';
408         if ($this->_iscustom)
409             $this->_field = substr($field, 7);
410         //$heading = $field;
411         $this->dbi = &$GLOBALS['request']->getDbh();
412         $this->_PageList_Column_base($this->_field);
413     }
414
415     function _getValue($page, &$revision_handle) {
416             if (is_object($page)) $text = $page->getName();
417         else $text = $page;
418             $trans = $this->_plugin->fast_translate($text, $this->_field,
419                                                 $this->_from_lang);
420         // how to markup untranslated words and not existing pages?
421         // untranslated: (TODO) link to translation editor
422         if ($trans == $text or // untranslated
423             (($this->_from_lang != 'en') and
424              ($this->_field != 'en') and
425              ($trans == $this->_plugin->fast_translate($text, 'en',
426                                                        $this->_from_lang))
427              ))
428         {
429             global $WikiTheme;
430             $link = $WikiTheme->linkUnknownWikiWord($trans);
431             if (!($this->_noT or $this->_nolinks)
432                 and $this->dbi->isWikiPage($trans))
433             {
434                 $url = WikiURL($trans, array('action' => 'TranslateText',
435                                              'lang' => $this->_field));
436                 $button = $WikiTheme->makeButton('T', $url);
437                 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
438                                             $trans, $this->_field));
439                 $link = HTML::span($button);
440                 $link->setAttr('class', 'wikiunknown');
441                 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
442                 $text->setAttr('style', 'text-decoration:line-through');
443                 $link->pushContent($text);
444                 return $link;
445             } elseif (is_object($page))
446                 return '';
447             else                        // not existing: empty
448                 return '';
449         } elseif (is_object($page)) {
450             if (!$this->_nolinks)
451                 return WikiLink($trans, 'auto');
452             else
453                 return $trans;
454         } else {
455             return $trans;
456         }
457     }
458 }
459
460 // For emacs users
461 // Local Variables:
462 // mode: php
463 // tab-width: 8
464 // c-basic-offset: 4
465 // c-hanging-comment-ender-p: nil
466 // indent-tabs-mode: nil
467 // End:
468 ?>