]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/_WikiTranslation.php
getName should not translate
[SourceForge/phpwiki.git] / lib / plugin / _WikiTranslation.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  *  <<_WikiTranslation page=HomePage languages=fr >>
30  *     Translation service for HomePage into french (redirect to translate.google.com)
31  *  <<_WikiTranslation what=pages >>
32  *     Translation matrix of all pages with proper translations (all in pgsrc)
33  *  <<_WikiTranslation what=wikiwords match="W*" limit=20 >>
34  *     Translation matrix of the first 20 wikiwords matching "W*"
35  *  <<_WikiTranslation string=HomePage languages=fr,de,sv >>
36  *     Translation matrix for all given languages
37  *  <<_WikiTranslation string=HomePage >>
38  *     Translation matrix for all supported languages
39  *  <<_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     _("AddComment") . ',' .
48     _("AllPages") . ',' .
49     _("AllPagesCreatedByMe") . ',' .
50     _("AllPagesLastEditedByMe") . ',' .
51     _("AllPagesOwnedByMe") . ',' .
52     _("AllUsers") . ',' .
53     _("AnalyseAccessLogSql") . ',' .
54     _("AppendText") . ',' .
55     _("AsciiMath") . ',' .
56     _("AsciiSVG") . ',' .
57     _("AtomFeed") . ',' .
58     _("_AuthInfo") . ',' .
59     _("AuthorHistory") . ',' .
60     _("_BackendInfo") . ',' .
61     _("BackLinks") . ',' .
62     _("BlogArchives") . ',' .
63     _("BlogJournal") . ',' .
64     _("BoxRight") . ',' .
65     _("CacheTest") . ',' .
66     _("Calendar") . ',' .
67     _("CalendarList") . ',' .
68     _("CategoryCategory") . ',' .
69     _("CategoryHomePages") . ',' .
70     _("CategoryPage") . ',' .
71     _("Chart") . ',' .
72     _("Comment") . ',' .
73     _("CreateBib") . ',' .
74     _("CreatePage") . ',' .
75     _("CreateToc") . ',' .
76     _("CurrentTime") . ',' .
77     _("DeadEndPages") . ',' .
78     _("DebugInfo") . ',' .
79     _("Diff") . ',' .
80     _("DynamicIncludePage") . ',' .
81     _("EditMetaData") . ',' .
82     _("ExternalSearch") . ',' .
83     _("FacebookLike") . ',' .
84     _("FileInfo") . ',' .
85     _("FindPage") . ',' .
86     _("FoafViewer") . ',' .
87     _("FrameInclude") . ',' .
88     _("FullRecentChanges") . ',' .
89     _("FullTextSearch") . ',' .
90     _("FuzzyPages") . ',' .
91     _("GoogleMaps") . ',' .
92     _("GooglePlugin") . ',' .
93     _("GoTo") . ',' .
94     _("GraphViz") . ',' .
95     _("_GroupInfo") . ',' .
96     _("HelloWorld") . ',' .
97     _("Help/AddCommentPlugin") . ',' .
98     _("Help/AddingPages") . ',' .
99     _("Help/AuthorHistoryPlugin") . ',' .
100     _("Help/CalendarListPlugin") . ',' .
101     _("Help/CalendarPlugin") . ',' .
102     _("Help/CommentPlugin") . ',' .
103     _("Help/CreateTocPlugin") . ',' .
104     _("Help/EditMetaDataPlugin") . ',' .
105     _("Help/ExternalSearchPlugin") . ',' .
106     _("Help/FoafViewerPlugin") . ',' .
107     _("Help/FrameIncludePlugin") . ',' .
108     _("Help/HelloWorldPlugin") . ',' .
109     _("Help/IncludePagePlugin") . ',' .
110     _("Help/LinkIcons") . ',' .
111     _("Help/MagicPhpWikiURLs") . ',' .
112     _("Help/MoreAboutMechanics") . ',' .
113     _("Help/OldStyleTablePlugin") . ',' .
114     _("Help/PhotoAlbumPlugin") . ',' .
115     _("Help/PhpHighlightPlugin") . ',' .
116     _("Help/PhpWeatherPlugin") . ',' .
117     _("Help/PhpWiki") . ',' .
118     _("Help/PloticusPlugin") . ',' .
119     _("Help/RawHtmlPlugin") . ',' .
120     _("Help/RedirectToPlugin") . ',' .
121     _("Help/RichTablePlugin") . ',' .
122     _("Help/SystemInfoPlugin") . ',' .
123     _("Help/TranscludePlugin") . ',' .
124     _("Help/UnfoldSubpagesPlugin") . ',' .
125     _("Help/UpLoadPlugin") . ',' .
126     _("Help/WabiSabi") . ',' .
127     _("Help/WikiBlogPlugin") . ',' .
128     _("Help/WikiPlugin") . ',' .
129     _("Help/WikiWikiWeb") . ',' .
130     _("HomePageAlias") . ',' .
131     _("HtmlConverter") . ',' .
132     _("Imdb") . ',' .
133     _("IncludePage") . ',' .
134     _("IncludePages") . ',' .
135     _("IncludeSiteMap") . ',' .
136     _("IncludeTree") . ',' .
137     _("InterWiki") . ',' .
138     _("InterWikiSearch") . ',' .
139     _("JabberPresence") . ',' .
140     _("LdapSearch") . ',' .
141     _("LikePages") . ',' .
142     _("LinkDatabase") . ',' .
143     _("LinkSearch") . ',' .
144     _("ListPages") . ',' .
145     _("ListRelations") . ',' .
146     _("ListSubpages") . ',' .
147     _("MediawikiTable") . ',' .
148     _("ModeratedPage") . ',' .
149     _("MostPopular") . ',' .
150     _("NewPagesPerUser") . ',' .
151     _("NoCache") . ',' .
152     _("OldStyleTable") . ',' .
153     _("OrphanedPages") . ',' .
154     _("PageDump") . ',' .
155     _("PageGroup") . ',' .
156     _("PageHistory") . ',' .
157     _("PageInfo") . ',' .
158     _("PageTrail") . ',' .
159     _("PasswordReset") . ',' .
160     _("PhotoAlbum") . ',' .
161     _("PhpHighlight") . ',' .
162     _("PhpWeather") . ',' .
163     _("PhpWikiAdministration/Chmod") . ',' .
164     _("PhpWikiAdministration/Chown") . ',' .
165     _("PhpWikiAdministration/Remove") . ',' .
166     _("PhpWikiAdministration/Rename") . ',' .
167     _("PhpWikiAdministration/Replace") . ',' .
168     _("PhpWikiAdministration/SetAcl") . ',' .
169     _("PhpWikiDocumentation") . ',' .
170     _("PhpWikiPoll") . ',' .
171     _("Ploticus") . ',' .
172     _("PluginManager") . ',' .
173     _("PopularNearby") . ',' .
174     _("PopularTags") . ',' .
175     _("PopUp") . ',' .
176     _("PreferenceApp") . ',' .
177     _("_PreferencesInfo") . ',' .
178     _("PrevNext") . ',' .
179     _("Processing") . ',' .
180     _("RandomPage") . ',' .
181     _("RateIt") . ',' .
182     _("RawHtml") . ',' .
183     _("RecentChanges") . ',' .
184     _("RecentChangesCached") . ',' .
185     _("RecentComments") . ',' .
186     _("RecentEdits") . ',' .
187     _("RecentReferrers") . ',' .
188     _("RecentVisitors") . ',' .
189     _("RedirectTo") . ',' .
190     _("RelatedChanges") . ',' .
191     _("ReleaseNotes") . ',' .
192     _("_Retransform") . ',' .
193     _("RichTable") . ',' .
194     _("RssFeed") . ',' .
195     _("SearchHighlight") . ',' .
196     _("SemanticRelations") . ',' .
197     _("SemanticSearch") . ',' .
198     _("SemanticSearchAdvanced") . ',' .
199     _("SiteMap") . ',' .
200     _("SpellCheck") . ',' .
201     _("SqlResult") . ',' .
202     _("SyncWiki") . ',' .
203     _("SyntaxHighlighter") . ',' .
204     _("SystemInfo") . ',' .
205     _("Template") . ',' .
206     _("TeX2png") . ',' .
207     _("text2png") . ',' .
208     _("TexToPng") . ',' .
209     _("TitleSearch") . ',' .
210     _("Transclude") . ',' .
211     _("TranslateText") . ',' .
212     _("UnfoldSubpages") . ',' .
213     _("UpLoad") . ',' .
214     _("UriResolver") . ',' .
215     _("UserPreferences") . ',' .
216     _("UserRatings") . ',' .
217     _("Video") . ',' .
218     _("VisualWiki") . ',' .
219     _("WantedPages") . ',' .
220     _("WantedPagesOld") . ',' .
221     _("WatchPage") . ',' .
222     _("WhoIsOnline") . ',' .
223     _("WikiAdminChmod") . ',' .
224     _("WikiAdminChown") . ',' .
225     _("WikiAdminDeleteAcl") . ',' .
226     _("WikiAdminMarkup") . ',' .
227     _("WikiAdminPurge") . ',' .
228     _("WikiAdminRemove") . ',' .
229     _("WikiAdminRename") . ',' .
230     _("WikiAdminSearchReplace") . ',' .
231     _("WikiAdminSelect") . ',' .
232     _("WikiAdminSetAcl") . ',' .
233     _("WikiAdminSetAclSimple") . ',' .
234     _("WikiAdminSetExternal") . ',' .
235     _("WikiAdminUtils") . ',' .
236     _("WikiBlog") . ',' .
237     _("WikicreoleTable") . ',' .
238     _("WikiForm") . ',' .
239     _("WikiFormRich") . ',' .
240     _("WikiForum") . ',' .
241     _("WikiPoll") . ',' .
242     _("_WikiTranslation") . ',' .
243     _("YouTube");
244
245 require_once 'lib/PageList.php';
246
247 class WikiPlugin__WikiTranslation
248     extends WikiPlugin
249 {
250
251     function getDescription()
252     {
253         return _("Show translations of various words or pages.");
254     }
255
256     function getDefaultArguments()
257     {
258         return array_merge
259         (
260             PageList::supportedArgs(),
261             array('languages' => '', // comma delimited string of de,en,sv,...
262                 'string' => '',
263                 'page' => '', // use a translation service
264                 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
265
266                 'match' => '*',
267                 'from_lang' => false,
268                 'include_empty' => false,
269                 //'exclude'       => '',
270                 //'sortby'        => '',
271                 //'limit'         => 0,
272                 'nolinks' => false, // don't display any links
273                 // (for development only)
274                 'noT' => false // don't display the T link
275                 // (for development only)
276             ));
277     }
278
279     function init_locale($lang)
280     {
281         if ($lang != $this->lang)
282             update_locale($lang);
283         if ($lang == 'en') {
284             // Hack alert! we need hash for stepping through it, even if it's
285             // in the wrong language
286             include (FindFile("locale/de/LC_MESSAGES/phpwiki.php", 0, 'reinit'));
287             foreach ($locale as $en => $de) {
288                 $locale[$en] = $en;
289             }
290             // gettext module loaded: must load the LC_MESSAGES php hash
291         } elseif (function_exists('bindtextdomain')) {
292             include (FindFile("locale/$lang/LC_MESSAGES/phpwiki.php", 0, 'reinit'));
293             //include (FindLocalizedFile("LC_MESSAGES/phpwiki.php", 0,'reinit'));
294             // we already have a $locale, but maybe it's in the wrong language
295         } elseif ($lang != $this->lang or empty($GLOBALS['locale'])) {
296             include (FindFile("locale/$lang/LC_MESSAGES/phpwiki.php", 0, 'reinit'));
297         } else {
298             $locale = & $GLOBALS['locale'];
299         }
300         $this->_locales[$lang] = $locale;
301     }
302
303     // reverse translation:
304     function translate_to_en($text, $lang = false)
305     {
306         if (!$lang) $lang = $this->lang; // current locale
307         if ($lang == 'en') return $text;
308
309         $this->_locales = array();
310         $this->_reverse_locales = array();
311
312         if (!isset($this->_locales[$lang])) {
313             $this->init_locale($lang);
314         }
315         assert(!empty($this->_locales[$lang]));
316         if (!isset($this->_reverse_locales[$lang])) {
317             // and now do a reverse lookup in the $locale hash
318             $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
319         }
320         if (!empty($this->_reverse_locales[$lang][$text])) {
321             return $this->_reverse_locales[$lang][$text];
322         } else {
323             return $text;
324         }
325     }
326
327     /**
328      * setlocale() switching with the gettext extension is by far too slow.
329      * So use the hash regardless if gettext is loaded or not.
330      */
331     function fast_translate($text, $to_lang, $from_lang = false)
332     {
333         if (!$from_lang) $from_lang = $this->lang; // current locale
334         if ($from_lang == $to_lang) return $text;
335         // setup hash from en => to_lang
336         if (!isset($this->_locales[$to_lang]))
337             $this->init_locale($to_lang);
338         if ($from_lang != 'en') {
339             // get reverse gettext: translate to english
340             $text = $this->translate_to_en($text, $from_lang);
341         }
342         return !empty($this->_locales[$to_lang][$text])
343             ? $this->_locales[$to_lang][$text]
344             : $text;
345     }
346
347     //FIXME! There's something wrong.
348     function translate($text, $to_lang, $from_lang = false)
349     {
350         if (!$from_lang) $from_lang = $this->lang; // current locale
351         if ($from_lang == $to_lang) return $text;
352         // Speed up hash lookup. Not needed for gettext module
353         if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
354             $this->init_locale($from_lang);
355         }
356         if ($from_lang != 'en') {
357             // get reverse gettext: translate to english
358             $en = $this->translate_to_en($text, $from_lang);
359             // and then to target
360             update_locale($to_lang);
361             $result = gettext($en);
362             update_locale($from_lang);
363         } else {
364             // locale switching is very slow with the gettext extension.
365             // better use fast_translate
366             if ($from_lang != $to_lang) {
367                 update_locale($to_lang);
368             }
369             $result = gettext($text);
370             if ($from_lang != $to_lang) {
371                 update_locale($from_lang);
372             }
373         }
374         return $result;
375     }
376
377     function run($dbi, $argstr, &$request, $basepage)
378     {
379         $this->args = $this->getArgs($argstr, $request);
380         extract($this->args);
381         $this->request = &$request;
382         if (!$from_lang) $from_lang = $request->getPref('lang');
383         if (!$from_lang) $from_lang = $GLOBALS['LANG'];
384         $this->lang = $from_lang;
385
386         if (empty($languages)) {
387             $available_languages = listAvailableLanguages();
388             if ($from_lang == 'en') {
389                 // "en" is always the first.
390                 array_shift($available_languages);
391             }
392             // put from_lang to the very end.
393             if (in_array($from_lang, $available_languages))
394                 $languages = $available_languages;
395             else
396                 $languages = array_merge($available_languages, array($from_lang));
397         } elseif (strstr($languages, ',')) {
398             $languages = explode(',', $languages);
399         } else {
400             $languages = array($languages);
401         }
402         $to_lang = $languages[0];
403         if (!empty($string) and count($languages) == 1) {
404             return $this->translate($string, $to_lang, $from_lang);
405         }
406         if (!empty($page)) {
407             $pagename = $page;
408             if ($dbi->isWikiPage($pagename)) {
409                 $url = '';
410                 // google can only translate from english and french
411                 if (in_array($from_lang, array('en', 'fr'))) {
412                     $url = "http://translate.google.com/translate";
413                     $url .= "?langpair=" . urlencode($from_lang . "|" . $to_lang);
414                     $url .= "&u=" . urlencode(WikiURL($pagename, false, true));
415                 }
416                 // redirect or transclude?
417                 if ($url) {
418                     return $request->redirect($url);
419                 }
420                 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
421                     WikiURL($pagename, false, true),
422                     $to_lang));
423             } else {
424                 return $this->error(fmt("%s is empty.", $pagename));
425             }
426         }
427
428         $pagelist = new PageList('', $exclude, $this->args);
429         $pagelist->_columns[0]->_heading = "$from_lang";
430         foreach ($languages as $lang) {
431             if ($lang == $from_lang) continue;
432             $field = "custom:$lang";
433             $pagelist->addColumnObject(
434                 new _PageList_Column_customlang($field, $from_lang, $this));
435         }
436         if (!empty($string)) {
437             $pagelist->addPage($string);
438             return $pagelist;
439         }
440         switch ($what) {
441             case 'allpages':
442                 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
443                     $limit, $exclude));
444                 break;
445             case 'pages':
446                 // not all pages, only the pgsrc pages
447                 if (!is_array($exclude))
448                     $exclude = $pagelist->explodePageList($exclude, false, $sortby,
449                         $limit, $exclude);
450                 $path = FindLocalizedFile(WIKI_PGSRC);
451                 $pgsrc = new fileSet($path);
452                 foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
453                     $pagename = urldecode($pagename);
454                     if (substr($pagename, -1, 1) == '~') continue;
455                     if (in_array($pagename, $exclude))
456                         continue; // exclude page.
457                     if ($match != '*' and !glob_match($match, $pagename))
458                         continue;
459                     $page_handle = $dbi->getPage($pagename);
460                     $pagelist->addPage($page_handle);
461                 }
462                 break;
463             case 'wikiwords':
464                 if (!isset($this->_locales[$from_lang])) {
465                     $this->init_locale($from_lang);
466                 }
467                 $locale = & $this->_locales[$from_lang];
468                 if (is_array($locale)) {
469                     $count = 0;
470                     foreach ($locale as $from => $to) {
471                         if ($match != '*' and !glob_match($match, $from))
472                             continue;
473                         if (isWikiWord($from)) {
474                             $count++;
475                             $pagelist->addPage($from);
476                             if ($limit and $count > $limit) break;
477                         }
478                     }
479                 }
480                 break;
481             // all Button texts, which need a localized .png
482             // where to get them from? templates/*.tmpl: Button()
483             // and WikiLink(?,'button')
484             // navbar links, actionpages, and admin requests
485             case 'buttons':
486                 $buttons = $GLOBALS['AllActionPages'];
487                 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
488                     "*.png");
489                 foreach ($fileset->getFiles() as $file) {
490                     $b = urldecode(substr($file, 0, -4));
491                     if (!in_array($b, $buttons))
492                         $buttons[] = $b;
493                 }
494                 $count = 0;
495                 foreach ($buttons as $button) {
496                     $pagelist->addPage($button);
497                     if ($limit and ++$count > $limit) break;
498                 }
499                 break;
500         }
501         return $pagelist;
502     }
503 }
504
505 class _PageList_Column_customlang extends _PageList_Column
506 {
507     function _PageList_Column_customlang($field, $from_lang, $plugin)
508     {
509         $this->_field = $field;
510         $this->_from_lang = $from_lang;
511         $this->_plugin =& $plugin;
512         $this->_what = $plugin->args['what'];
513         $this->_noT = $plugin->args['noT'];
514         $this->_nolinks = $plugin->args['nolinks'];
515         $this->_iscustom = substr($field, 0, 7) == 'custom:';
516         if ($this->_iscustom)
517             $this->_field = substr($field, 7);
518         //$heading = $field;
519         $this->dbi = &$GLOBALS['request']->getDbh();
520         $this->_PageList_Column_base($this->_field);
521     }
522
523     function _getValue($page, &$revision_handle)
524     {
525         if (is_object($page)) $text = $page->getName();
526         else $text = $page;
527         $trans = $this->_plugin->fast_translate($text, $this->_field,
528             $this->_from_lang);
529         // how to markup untranslated words and not existing pages?
530         // untranslated: (TODO) link to translation editor
531         if ($trans == $text or // untranslated
532             (($this->_from_lang != 'en') and
533                 ($this->_field != 'en') and
534                     ($trans == $this->_plugin->fast_translate($text, 'en',
535                         $this->_from_lang))
536             )
537         ) {
538             global $WikiTheme;
539             $link = $WikiTheme->linkUnknownWikiWord($trans);
540             if (!($this->_noT or $this->_nolinks)
541                 and $this->dbi->isWikiPage($trans)
542             ) {
543                 $url = WikiURL($trans, array('action' => 'TranslateText',
544                     'lang' => $this->_field));
545                 $button = $WikiTheme->makeButton('T', $url);
546                 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
547                     $trans, $this->_field));
548                 $link = HTML::span($button);
549                 $link->setAttr('class', 'wikiunknown');
550                 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
551                 $text->setAttr('style', 'text-decoration:line-through');
552                 $link->pushContent($text);
553                 return $link;
554             } elseif (is_object($page))
555                 return ''; else // not existing: empty
556                 return '';
557         } elseif (is_object($page)) {
558             if (!$this->_nolinks)
559                 return WikiLink($trans, 'auto');
560             else
561                 return $trans;
562         } else {
563             return $trans;
564         }
565     }
566 }
567
568 // Local Variables:
569 // mode: php
570 // tab-width: 8
571 // c-basic-offset: 4
572 // c-hanging-comment-ender-p: nil
573 // indent-tabs-mode: nil
574 // End: