4 * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
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
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.
42 * @author: Reini Urban
45 /* Container for untranslated pagenames. Needed to show up in locale/po/phpwiki.pot */
47 _("AddComment") . ',' .
49 _("AllPagesCreatedByMe") . ',' .
50 _("AllPagesLastEditedByMe") . ',' .
51 _("AllPagesOwnedByMe") . ',' .
53 _("AnalyseAccessLogSql") . ',' .
54 _("AppendText") . ',' .
55 _("AsciiMath") . ',' .
58 _("_AuthInfo") . ',' .
59 _("AuthorHistory") . ',' .
60 _("_BackendInfo") . ',' .
61 _("BackLinks") . ',' .
62 _("BlogArchives") . ',' .
63 _("BlogJournal") . ',' .
65 _("CacheTest") . ',' .
67 _("CalendarList") . ',' .
68 _("CategoryCategory") . ',' .
69 _("CategoryHomePages") . ',' .
70 _("CategoryPage") . ',' .
73 _("CreateBib") . ',' .
74 _("CreatePage") . ',' .
75 _("CreateToc") . ',' .
76 _("CurrentTime") . ',' .
77 _("DeadEndPages") . ',' .
78 _("DebugInfo") . ',' .
80 _("DynamicIncludePage") . ',' .
81 _("EditMetaData") . ',' .
82 _("ExternalSearch") . ',' .
83 _("FacebookLike") . ',' .
86 _("FoafViewer") . ',' .
87 _("FrameInclude") . ',' .
88 _("FullRecentChanges") . ',' .
89 _("FullTextSearch") . ',' .
90 _("FuzzyPages") . ',' .
91 _("GoogleMaps") . ',' .
92 _("GooglePlugin") . ',' .
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") . ',' .
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") . ',' .
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") . ',' .
176 _("PreferenceApp") . ',' .
177 _("_PreferencesInfo") . ',' .
178 _("PrevNext") . ',' .
179 _("Processing") . ',' .
180 _("RandomPage") . ',' .
183 _("RecentChanges") . ',' .
184 _("RecentChangesCached") . ',' .
185 _("RecentComments") . ',' .
186 _("RecentEdits") . ',' .
187 _("RecentReferrers") . ',' .
188 _("RecentVisitors") . ',' .
189 _("RedirectTo") . ',' .
190 _("RelatedChanges") . ',' .
191 _("ReleaseNotes") . ',' .
192 _("_Retransform") . ',' .
193 _("RichTable") . ',' .
195 _("SearchHighlight") . ',' .
196 _("SemanticRelations") . ',' .
197 _("SemanticSearch") . ',' .
198 _("SemanticSearchAdvanced") . ',' .
200 _("SpellCheck") . ',' .
201 _("SqlResult") . ',' .
202 _("SyncWiki") . ',' .
203 _("SyntaxHighlighter") . ',' .
204 _("SystemInfo") . ',' .
205 _("Template") . ',' .
207 _("text2png") . ',' .
208 _("TexToPng") . ',' .
209 _("TitleSearch") . ',' .
210 _("Transclude") . ',' .
211 _("TranslateText") . ',' .
212 _("UnfoldSubpages") . ',' .
214 _("UriResolver") . ',' .
215 _("UserPreferences") . ',' .
216 _("UserRatings") . ',' .
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") . ',' .
245 require_once 'lib/PageList.php';
247 class WikiPlugin__WikiTranslation
251 function getDescription()
253 return _("Show translations of various words or pages.");
256 function getDefaultArguments()
260 PageList::supportedArgs(),
261 array('languages' => '', // comma delimited string of de,en,sv,...
263 'page' => '', // use a translation service
264 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
267 'from_lang' => false,
268 'include_empty' => false,
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)
279 function init_locale($lang)
281 if ($lang != $this->lang)
282 update_locale($lang);
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) {
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'));
298 $locale = & $GLOBALS['locale'];
300 $this->_locales[$lang] = $locale;
303 // reverse translation:
304 function translate_to_en($text, $lang = false)
306 if (!$lang) $lang = $this->lang; // current locale
307 if ($lang == 'en') return $text;
309 $this->_locales = array();
310 $this->_reverse_locales = array();
312 if (!isset($this->_locales[$lang])) {
313 $this->init_locale($lang);
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]);
320 if (!empty($this->_reverse_locales[$lang][$text])) {
321 return $this->_reverse_locales[$lang][$text];
328 * setlocale() switching with the gettext extension is by far too slow.
329 * So use the hash regardless if gettext is loaded or not.
331 function fast_translate($text, $to_lang, $from_lang = false)
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);
342 return !empty($this->_locales[$to_lang][$text])
343 ? $this->_locales[$to_lang][$text]
347 //FIXME! There's something wrong.
348 function translate($text, $to_lang, $from_lang = false)
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);
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);
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);
369 $result = gettext($text);
370 if ($from_lang != $to_lang) {
371 update_locale($from_lang);
377 function run($dbi, $argstr, &$request, $basepage)
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;
386 if (empty($languages)) {
387 $available_languages = listAvailableLanguages();
388 if ($from_lang == 'en') {
389 // "en" is always the first.
390 array_shift($available_languages);
392 // put from_lang to the very end.
393 if (in_array($from_lang, $available_languages))
394 $languages = $available_languages;
396 $languages = array_merge($available_languages, array($from_lang));
397 } elseif (strstr($languages, ',')) {
398 $languages = explode(',', $languages);
400 $languages = array($languages);
402 $to_lang = $languages[0];
403 if (!empty($string) and count($languages) == 1) {
404 return $this->translate($string, $to_lang, $from_lang);
408 if ($dbi->isWikiPage($pagename)) {
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));
416 // redirect or transclude?
418 return $request->redirect($url);
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),
424 return $this->error(fmt("%s is empty.", $pagename));
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));
436 if (!empty($string)) {
437 $pagelist->addPage($string);
442 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
446 // not all pages, only the pgsrc pages
447 if (!is_array($exclude))
448 $exclude = $pagelist->explodePageList($exclude, false, $sortby,
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))
459 $page_handle = $dbi->getPage($pagename);
460 $pagelist->addPage($page_handle);
464 if (!isset($this->_locales[$from_lang])) {
465 $this->init_locale($from_lang);
467 $locale = & $this->_locales[$from_lang];
468 if (is_array($locale)) {
470 foreach ($locale as $from => $to) {
471 if ($match != '*' and !glob_match($match, $from))
473 if (isWikiWord($from)) {
475 $pagelist->addPage($from);
476 if ($limit and $count > $limit) 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
486 $buttons = $GLOBALS['AllActionPages'];
487 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
489 foreach ($fileset->getFiles() as $file) {
490 $b = urldecode(substr($file, 0, -4));
491 if (!in_array($b, $buttons))
495 foreach ($buttons as $button) {
496 $pagelist->addPage($button);
497 if ($limit and ++$count > $limit) break;
505 class _PageList_Column_customlang extends _PageList_Column
507 function _PageList_Column_customlang($field, $from_lang, $plugin)
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);
519 $this->dbi = &$GLOBALS['request']->getDbh();
520 $this->_PageList_Column_base($this->_field);
523 function _getValue($page, &$revision_handle)
525 if (is_object($page)) $text = $page->getName();
527 $trans = $this->_plugin->fast_translate($text, $this->_field,
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',
539 $link = $WikiTheme->linkUnknownWikiWord($trans);
540 if (!($this->_noT or $this->_nolinks)
541 and $this->dbi->isWikiPage($trans)
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);
554 } elseif (is_object($page))
555 return ''; else // not existing: empty
557 } elseif (is_object($page)) {
558 if (!$this->_nolinks)
559 return WikiLink($trans, 'auto');
572 // c-hanging-comment-ender-p: nil
573 // indent-tabs-mode: nil