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") . ',' .
48 _("AddCommentPlugin") . ',' .
49 _("AddingPages") . ',' .
51 _("AllPagesCreatedByMe") . ',' .
52 _("AllPagesLastEditedByMe") . ',' .
53 _("AllPagesOwnedByMe") . ',' .
55 _("AnalyseAccessLogSql") . ',' .
56 _("AppendText") . ',' .
57 _("AsciiMath") . ',' .
60 _("DebugAuthInfo") . ',' .
61 _("AuthorHistory") . ',' .
62 _("AuthorHistoryPlugin") . ',' .
63 _("DebugBackendInfo") . ',' .
64 _("BackLinks") . ',' .
65 _("BlogArchives") . ',' .
66 _("BlogJournal") . ',' .
68 _("CacheTest") . ',' .
70 _("CalendarList") . ',' .
71 _("CalendarListPlugin") . ',' .
72 _("CalendarPlugin") . ',' .
73 _("CategoryCategory") . ',' .
74 _("CategoryHomePages") . ',' .
75 _("CategoryPage") . ',' .
79 _("CommentPlugin") . ',' .
80 _("CreateBib") . ',' .
81 _("CreatePage") . ',' .
82 _("CreateToc") . ',' .
83 _("CreateTocPlugin") . ',' .
84 _("CurrentTime") . ',' .
85 _("DeadEndPages") . ',' .
86 _("DebugInfo") . ',' .
88 _("DynamicIncludePage") . ',' .
89 _("EditMetaData") . ',' .
90 _("EditMetaDataPlugin") . ',' .
91 _("ExternalSearch") . ',' .
92 _("ExternalSearchPlugin") . ',' .
93 _("FacebookLike") . ',' .
96 _("FoafViewer") . ',' .
97 _("FoafViewerPlugin") . ',' .
98 _("FrameInclude") . ',' .
99 _("FrameIncludePlugin") . ',' .
100 _("FullRecentChanges") . ',' .
101 _("FullTextSearch") . ',' .
102 _("FuzzyPages") . ',' .
103 _("GoogleMaps") . ',' .
104 _("GooglePlugin") . ',' .
106 _("GraphViz") . ',' .
107 _("DebugGroupInfo") . ',' .
108 _("HelloWorld") . ',' .
109 _("HelloWorldPlugin") . ',' .
110 _("HomePageAlias") . ',' .
111 _("HtmlConverter") . ',' .
112 _("IncludePage") . ',' .
113 _("IncludePagePlugin") . ',' .
114 _("IncludePages") . ',' .
115 _("IncludeSiteMap") . ',' .
116 _("IncludeTree") . ',' .
117 _("InterWiki") . ',' .
118 _("InterWikiSearch") . ',' .
119 _("JabberPresence") . ',' .
120 _("LdapSearch") . ',' .
121 _("LikePages") . ',' .
122 _("LinkDatabase") . ',' .
123 _("LinkIcons") . ',' .
124 _("LinkSearch") . ',' .
125 _("ListPages") . ',' .
126 _("ListRelations") . ',' .
127 _("ListSubpages") . ',' .
128 _("MagicPhpWikiURLs") . ',' .
129 _("MediawikiTable") . ',' .
130 _("ModeratedPage") . ',' .
131 _("MoreAboutMechanics") . ',' .
132 _("MostPopular") . ',' .
133 _("NewPagesPerUser") . ',' .
135 _("OldStyleTable") . ',' .
136 _("OldStyleTablePlugin") . ',' .
137 _("OrphanedPages") . ',' .
138 _("PageDump") . ',' .
139 _("PageGroup") . ',' .
140 _("PageHistory") . ',' .
141 _("PageInfo") . ',' .
142 _("PageTrail") . ',' .
143 _("PasswordReset") . ',' .
144 _("PhotoAlbum") . ',' .
145 _("PhotoAlbumPlugin") . ',' .
146 _("PhpHighlight") . ',' .
147 _("PhpHighlightPlugin") . ',' .
148 _("PhpWeather") . ',' .
149 _("PhpWeatherPlugin") . ',' .
151 _("PhpWikiAdministration") . ',' .
152 _("PhpWikiDocumentation") . ',' .
153 _("PhpWikiPoll") . ',' .
154 _("Ploticus") . ',' .
155 _("PloticusPlugin") . ',' .
156 _("PluginManager") . ',' .
157 _("PopularNearby") . ',' .
158 _("PopularTags") . ',' .
160 _("PreferenceApp") . ',' .
161 _("PreferencesInfo") . ',' .
162 _("PrevNext") . ',' .
163 _("Processing") . ',' .
164 _("RandomPage") . ',' .
167 _("RawHtmlPlugin") . ',' .
168 _("RecentChanges") . ',' .
169 _("RecentChangesCached") . ',' .
170 _("RecentComments") . ',' .
171 _("RecentEdits") . ',' .
172 _("RecentReferrers") . ',' .
173 _("RecentVisitors") . ',' .
174 _("RedirectTo") . ',' .
175 _("RedirectToPlugin") . ',' .
176 _("RelatedChanges") . ',' .
177 _("ReleaseNotes") . ',' .
181 _("DebugRetransform") . ',' .
182 _("RichTable") . ',' .
183 _("RichTablePlugin") . ',' .
185 _("SearchHighlight") . ',' .
186 _("SemanticRelations") . ',' .
187 _("SemanticSearch") . ',' .
188 _("SemanticSearchAdvanced") . ',' .
191 _("SpellCheck") . ',' .
192 _("SpecialPages") . ',' .
193 _("SqlResult") . ',' .
194 _("SyncWiki") . ',' .
195 _("SyntaxHighlighter") . ',' .
196 _("SystemInfo") . ',' .
197 _("SystemInfoPlugin") . ',' .
198 _("Template") . ',' .
200 _("text2png") . ',' .
201 _("TextFormattingRules") . ',' .
202 _("TexToPng") . ',' .
203 _("TitleSearch") . ',' .
204 _("Transclude") . ',' .
205 _("TranscludePlugin") . ',' .
206 _("TranslateText") . ',' .
207 _("UnfoldSubpages") . ',' .
208 _("UnfoldSubpagesPlugin") . ',' .
210 _("UpLoadPlugin") . ',' .
211 _("UriResolver") . ',' .
212 _("UserPreferences") . ',' .
213 _("UserRatings") . ',' .
215 _("VisualWiki") . ',' .
216 _("WabiSabi") . ',' .
217 _("WantedPages") . ',' .
218 _("WantedPagesOld") . ',' .
219 _("WatchPage") . ',' .
220 _("WhoIsOnline") . ',' .
221 _("WikiAdminChown") . ',' .
222 _("WikiAdminDeleteAcl") . ',' .
223 _("WikiAdminPurge") . ',' .
224 _("WikiAdminRemove") . ',' .
225 _("WikiAdminRename") . ',' .
226 _("WikiAdminSearchReplace") . ',' .
227 _("WikiAdminSelect") . ',' .
228 _("WikiAdminSetAcl") . ',' .
229 _("WikiAdminSetAclSimple") . ',' .
230 _("WikiAdminSetExternal") . ',' .
231 _("WikiAdminUtils") . ',' .
232 _("WikiBlog") . ',' .
233 _("WikiBlogPlugin") . ',' .
234 _("WikicreoleTable") . ',' .
235 _("WikiForm") . ',' .
236 _("WikiFormRich") . ',' .
237 _("WikiForum") . ',' .
238 _("WikiPlugin") . ',' .
239 _("WikiPoll") . ',' .
240 _("WikiTranslation") . ',' .
241 _("WikiWikiWeb") . ',' .
244 require_once 'lib/PageList.php';
246 class WikiPlugin_WikiTranslation
250 function getDescription()
252 return _("Show translations of various words or pages.");
255 function getDefaultArguments()
259 PageList::supportedArgs(),
260 array('languages' => '', // comma delimited string of de,en,sv,...
262 'page' => '', // use a translation service
263 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
266 'from_lang' => false,
267 'include_empty' => false,
271 'nolinks' => false, // don't display any links
272 // (for development only)
273 'noT' => false // don't display the T link
274 // (for development only)
278 function init_locale($lang)
280 if ($lang != $this->lang)
281 update_locale($lang);
282 $this->_locales[$lang] = $locale;
285 // reverse translation:
286 function translate_to_en($text, $lang = false)
288 if (!$lang) $lang = $this->lang; // current locale
289 if ($lang == 'en') return $text;
291 $this->_locales = array();
292 $this->_reverse_locales = array();
294 if (!isset($this->_locales[$lang])) {
295 $this->init_locale($lang);
297 assert(!empty($this->_locales[$lang]));
298 if (!isset($this->_reverse_locales[$lang])) {
299 // and now do a reverse lookup in the $locale hash
300 $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
302 if (!empty($this->_reverse_locales[$lang][$text])) {
303 return $this->_reverse_locales[$lang][$text];
310 * setlocale() switching with the gettext extension is by far too slow.
311 * So use the hash regardless if gettext is loaded or not.
313 function fast_translate($text, $to_lang, $from_lang = false)
315 if (!$from_lang) $from_lang = $this->lang; // current locale
316 if ($from_lang == $to_lang) return $text;
317 // setup hash from en => to_lang
318 if (!isset($this->_locales[$to_lang]))
319 $this->init_locale($to_lang);
320 if ($from_lang != 'en') {
321 // get reverse gettext: translate to english
322 $text = $this->translate_to_en($text, $from_lang);
324 return !empty($this->_locales[$to_lang][$text])
325 ? $this->_locales[$to_lang][$text]
329 //FIXME! There's something wrong.
330 function translate($text, $to_lang, $from_lang = false)
332 if (!$from_lang) $from_lang = $this->lang; // current locale
333 if ($from_lang == $to_lang) return $text;
334 // Speed up hash lookup. Not needed for gettext module
335 if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
336 $this->init_locale($from_lang);
338 if ($from_lang != 'en') {
339 // get reverse gettext: translate to english
340 $en = $this->translate_to_en($text, $from_lang);
341 // and then to target
342 update_locale($to_lang);
343 $result = gettext($en);
344 update_locale($from_lang);
346 // locale switching is very slow with the gettext extension.
347 // better use fast_translate
348 if ($from_lang != $to_lang) {
349 update_locale($to_lang);
351 $result = gettext($text);
352 if ($from_lang != $to_lang) {
353 update_locale($from_lang);
359 function run($dbi, $argstr, &$request, $basepage)
361 $this->args = $this->getArgs($argstr, $request);
362 extract($this->args);
363 $this->request = &$request;
364 if (!$from_lang) $from_lang = $request->getPref('lang');
365 if (!$from_lang) $from_lang = $GLOBALS['LANG'];
366 $this->lang = $from_lang;
368 if (empty($languages)) {
369 $available_languages = listAvailableLanguages();
370 if ($from_lang == 'en') {
371 // "en" is always the first.
372 array_shift($available_languages);
374 // put from_lang to the very end.
375 if (in_array($from_lang, $available_languages))
376 $languages = $available_languages;
378 $languages = array_merge($available_languages, array($from_lang));
379 } elseif (strstr($languages, ',')) {
380 $languages = explode(',', $languages);
382 $languages = array($languages);
384 $to_lang = $languages[0];
385 if (!empty($string) and count($languages) == 1) {
386 return $this->translate($string, $to_lang, $from_lang);
390 if ($dbi->isWikiPage($pagename)) {
392 // google can only translate from english and french
393 if (in_array($from_lang, array('en', 'fr'))) {
394 $url = "http://translate.google.com/translate";
395 $url .= "?langpair=" . urlencode($from_lang . "|" . $to_lang);
396 $url .= "&u=" . urlencode(WikiURL($pagename, false, true));
398 // redirect or transclude?
400 return $request->redirect($url);
402 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
403 WikiURL($pagename, false, true),
406 return $this->error(fmt("%s is empty.", $pagename));
410 $pagelist = new PageList('', $exclude, $this->args);
411 $pagelist->_columns[0]->_heading = "$from_lang";
412 foreach ($languages as $lang) {
413 if ($lang == $from_lang) continue;
414 $field = "custom:$lang";
415 $pagelist->addColumnObject(
416 new _PageList_Column_customlang($field, $from_lang, $this));
418 if (!empty($string)) {
419 $pagelist->addPage($string);
424 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
428 // not all pages, only the pgsrc pages
429 if (!is_array($exclude))
430 $exclude = $pagelist->explodePageList($exclude, false, $sortby,
432 $path = FindLocalizedFile(WIKI_PGSRC);
433 $pgsrc = new fileSet($path);
434 foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
435 $pagename = urldecode($pagename);
436 if (substr($pagename, -1, 1) == '~') continue;
437 if (in_array($pagename, $exclude))
438 continue; // exclude page.
439 if ($match != '*' and !glob_match($match, $pagename))
441 $page_handle = $dbi->getPage($pagename);
442 $pagelist->addPage($page_handle);
446 if (!isset($this->_locales[$from_lang])) {
447 $this->init_locale($from_lang);
449 $locale = & $this->_locales[$from_lang];
450 if (is_array($locale)) {
452 foreach ($locale as $from => $to) {
453 if ($match != '*' and !glob_match($match, $from))
455 if (isWikiWord($from)) {
457 $pagelist->addPage($from);
458 if ($limit and $count > $limit) break;
463 // all Button texts, which need a localized .png
464 // where to get them from? templates/*.tmpl: Button()
465 // and WikiLink(?,'button')
466 // navbar links, actionpages, and admin requests
468 $buttons = $GLOBALS['AllActionPages'];
469 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
471 foreach ($fileset->getFiles() as $file) {
472 $b = urldecode(substr($file, 0, -4));
473 if (!in_array($b, $buttons))
477 foreach ($buttons as $button) {
478 $pagelist->addPage($button);
479 if ($limit and ++$count > $limit) break;
487 class _PageList_Column_customlang extends _PageList_Column
489 function _PageList_Column_customlang($field, $from_lang, $plugin)
491 $this->_field = $field;
492 $this->_from_lang = $from_lang;
493 $this->_plugin =& $plugin;
494 $this->_what = $plugin->args['what'];
495 $this->_noT = $plugin->args['noT'];
496 $this->_nolinks = $plugin->args['nolinks'];
497 $this->_iscustom = substr($field, 0, 7) == 'custom:';
498 if ($this->_iscustom)
499 $this->_field = substr($field, 7);
501 $this->dbi = &$GLOBALS['request']->getDbh();
502 $this->_PageList_Column_base($this->_field);
505 function _getValue($page, &$revision_handle)
507 if (is_object($page)) $text = $page->getName();
509 $trans = $this->_plugin->fast_translate($text, $this->_field,
511 // how to markup untranslated words and not existing pages?
512 // untranslated: (TODO) link to translation editor
513 if ($trans == $text or // untranslated
514 (($this->_from_lang != 'en') and
515 ($this->_field != 'en') and
516 ($trans == $this->_plugin->fast_translate($text, 'en',
521 $link = $WikiTheme->linkUnknownWikiWord($trans);
522 if (!($this->_noT or $this->_nolinks)
523 and $this->dbi->isWikiPage($trans)
525 $url = WikiURL($trans, array('action' => 'TranslateText',
526 'lang' => $this->_field));
527 $button = $WikiTheme->makeButton('T', $url);
528 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
529 $trans, $this->_field));
530 $link = HTML::span($button);
531 $link->setAttr('class', 'wikiunknown');
532 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
533 $text->setAttr('style', 'text-decoration:line-through');
534 $link->pushContent($text);
536 } elseif (is_object($page))
537 return ''; else // not existing: empty
539 } elseif (is_object($page)) {
540 if (!$this->_nolinks)
541 return WikiLink($trans, 'auto');
554 // c-hanging-comment-ender-p: nil
555 // indent-tabs-mode: nil