2 rcs_id('$Id: _WikiTranslation.php,v 1.5 2004-03-17 15:38:03 rurban Exp $');
4 Copyright 2004 $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
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * _WikiTranslation: Display pagenames and other strings in various languages.
25 * Can 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 * <?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.
42 * @author: Reini Urban
45 /* Container for untranslated pagenames. Needed to show up in locale/po/phpwiki.pot */
47 _("AddCommentPlugin") .','.
48 _("AddingPages") .','.
49 _("AuthorHistoryPlugin") .','.
51 _("CalendarListPlugin") .','.
52 _("CalendarPlugin") .','.
53 _("CategoryCategory") .','.
54 _("CategoryHomePages") .','.
55 _("CommentPlugin") .','.
56 _("CreateTocPlugin") .','.
58 _("EditMetaDataPlugin") .','.
59 _("ExternalSearchPlugin") .','.
61 _("FrameIncludePlugin") .','.
62 _("FullRecentChanges") .','.
63 _("HelloWorldPlugin") .','.
64 _("HomePageAlias") .','.
65 _("IncludePagePlugin") .','.
68 _("MagicPhpWikiURLs") .','.
69 _("MoreAboutMechanics") .','.
70 _("NewMarkupTestPage") .','.
71 _("OldMarkupTestPage") .','.
72 _("OldStyleTablePlugin") .','.
74 _("PageGroupTest") .','.
75 _("PageGroupTestFour") .','.
76 _("PageGroupTestOne") .','.
77 _("PageGroupTestThree") .','.
78 _("PageGroupTestTwo") .','.
79 _("PgsrcTranslation") .','.
80 _("PhotoAlbumPlugin") .','.
81 _("PhpHighlightPlugin") .','.
82 _("PhpWeatherPlugin") .','.
84 _("PhpWikiAdministration/Chmod") .','.
85 _("PhpWikiAdministration/Remove") .','.
86 _("PhpWikiAdministration/Rename") .','.
87 _("PhpWikiAdministration/Replace") .','.
88 _("PhpWikiDocumentation") .','.
89 _("PhpWikiPoll") .','.
90 _("RawHtmlPlugin") .','.
91 _("RecentVisitors") .','.
92 _("RedirectToPlugin") .','.
93 _("ReleaseNotes") .','.
94 _("RichTablePlugin") .','.
95 _("SteveWainstead") .','.
96 _("SystemInfoPlugin") .','.
97 _("TranscludePlugin") .','.
98 _("TranslateText") .','.
99 _("UnfoldSubpagesPlugin") .','.
102 _("WikiBlogPlugin") .','.
103 _("WikiPlugin") .','.
106 require_once('lib/PageList.php');
108 class WikiPlugin__WikiTranslation
113 return _("_WikiTranslation");
116 function getDescription() {
117 return _("Show translations of various words or pages");
120 function getVersion() {
121 return preg_replace("/[Revision: $]/", '',
122 "\$Revision: 1.5 $");
125 function getDefaultArguments() {
127 array( 'languages' => '', // comma delimited string of de,en,sv,...
129 'page' => '', // use a translation service
130 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
133 'from_lang' => false,
134 'include_empty' => false,
142 function init_locale($lang) {
143 if ($lang != $this->lang)
144 update_locale($lang);
145 // gettext module loaded: must load the LC_MESSAGES php hash
146 if (function_exists ('bindtextdomain')) {
147 include (FindLocalizedFile("LC_MESSAGES/phpwiki.php", 0,'reinit'));
148 } elseif ($lang == 'en') {
149 //hack alert! we need hash for stepping through it, even if it's in the wrong language
150 include (FindFile("locale/de/LC_MESSAGES/phpwiki.php", 0,'reinit'));
151 foreach ($locale as $en => $de) {
154 // we already have a $locale, but maybe it's in the wrong language
155 } elseif ($lang != $this->lang or empty($GLOBALS['locale'])) {
156 include (FindFile("LC_MESSAGES/phpwiki.php", 0,'reinit'));
158 $locale = & $GLOBALS['locale'];
160 $this->_locales[$lang] = $locale;
163 // reverse translation:
164 function translate_to_en($text,$lang=false) {
165 if (!$lang) $lang = $this->lang; // current locale
166 if ($lang == 'en') return $text;
168 $this->_locales = array();
169 $this->_reverse_locales = array();
171 if (!isset($this->_locales[$lang])) {
172 $this->init_locale($lang);
174 assert(!empty($this->_locales[$lang]));
175 if (!isset($this->_reverse_locales[$lang])) {
176 // and now do a reverse lookup in the $locale hash
177 $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
179 if (!empty($this->_reverse_locales[$lang][$text])) {
180 return $this->_reverse_locales[$lang][$text];
186 function translate($text,$to_lang,$from_lang=false) {
187 if (!$from_lang) $from_lang = $this->lang; // current locale
188 if ($from_lang == $to_lang) return $text;
189 // speed up hash lookup. not needed for gettext module
190 if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
191 $this->init_locale($from_lang);
193 if ($from_lang != 'en') {
194 // get reverse gettext: translate to english
195 $en = $this->translate_to_en($text,$from_lang);
196 // and then to target
197 update_locale($to_lang);
198 $result = gettext($en);
199 update_locale($from_lang);
201 if ($from_lang != $to_lang) {
202 update_locale($to_lang);
204 $result = gettext($text);
205 if ($from_lang != $to_lang) {
206 update_locale($from_lang);
212 function run($dbi, $argstr, $request, $basepage) {
213 extract($this->getArgs($argstr, $request));
214 $this->request = &$request;
215 if (!$from_lang) $from_lang = $request->getPref('lang');
216 if (!$from_lang) $from_lang = $GLOBALS['LANG'];
217 $this->lang = $from_lang;
219 if (empty($languages)) {
220 // from lib/plugin/UserPreferences.php
221 $available_languages = array();
222 if ($from_lang != 'en')
223 array_push($available_languages, 'en');
224 $dir_root = 'locale/';
225 if (defined('PHPWIKI_DIR'))
226 $dir_root = PHPWIKI_DIR . "/$dir_root";
227 $dir = dir($dir_root);
229 while ($entry = $dir->read()) {
230 if (is_dir($dir_root.$entry)
231 && (substr($entry,0,1) != '.')
233 && $entry != $from_lang
234 && $entry != 'CVS') {
235 array_push($available_languages, $entry);
240 if (in_array($from_lang,$available_languages))
241 $languages = $available_languages;
243 $languages = array_merge($available_languages,array($from_lang));
244 } elseif (strstr($languages,',')) {
245 $languages = explode(',',$languages);
247 $languages = array($languages);
249 $to_lang = $languages[0];
250 if (!empty($string) and count($languages)==1) {
251 return $this->translate($string,$to_lang,$from_lang);
255 if ($dbi->isWikiPage($pagename)) {
257 // google can only translate from english and french
258 if (in_array($from_lang,array('en','fr'))) {
259 $url = "http://translate.google.com/translate";
260 $url .= "?langpair=" . urlencode($from_lang."|".$to_lang);
261 $url .= "&u=" . urlencode(WikiUrl($pagename,false,true));
263 // redirect or transclude?
265 return $request->redirect($url);
266 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
267 WikiUrl($pagename,false,true),
270 return $this->error(fmt("%s is empty",$pagename));
273 $pagelist = new PageList('', $exclude, $this->getArgs($argstr, $request));
274 $pagelist->_columns[0]->_heading = "$from_lang";
275 foreach ($languages as $lang) {
276 if ($lang == $from_lang) continue;
277 $field = "custom:$lang";
278 $column = new _PageList_Column_custom($field,$from_lang,$this);
279 $pagelist->_types["custom"] = $column;
280 $pagelist->_addColumn($field);
282 if (!empty($string)) {
283 $pagelist->addPage( $string );
288 $pagelist->addPages( $dbi->getAllPages($include_empty, $sortby, $limit) );
291 // not all pages, only the pgsrc pages
292 if (!is_array($exclude))
293 $exclude = $pagelist->explodePageList($exclude,false,$sortby,$limit);
294 $path = FindLocalizedFile(WIKI_PGSRC);
295 $pgsrc = new fileSet($path);
296 foreach ($pgsrc->getFiles($exclude,$sortby,$limit) as $pagename) {
297 $pagename = urldecode($pagename);
298 if (substr($pagename,-1,1) == '~') continue;
299 if (in_array($pagename, $exclude))
300 continue; // exclude page.
301 if ($match != '*' and !glob_match($match,$pagename))
303 $page_handle = $dbi->getPage($pagename);
304 $pagelist->addPage( $page_handle );
308 if (!isset($this->_locales[$from_lang])) {
309 $this->init_locale($from_lang);
311 $locale = & $this->_locales[$from_lang];
312 if (is_array($locale)) {
314 foreach ($locale as $from => $to) {
315 if ($match != '*' and !glob_match($match,$from))
317 if (isWikiWord($from)) {
319 $pagelist->addPage( $from );
320 if ($limit and $count > $limit) break;
330 class _PageList_Column_custom extends _PageList_Column {
331 function _PageList_Column_custom($field, $from_lang, $plugin) {
332 $this->_field = $field;
333 $this->_from_lang = $from_lang;
334 $this->_plugin =& $plugin;
335 $this->_iscustom = substr($field, 0, 7) == 'custom:';
336 if ($this->_iscustom)
337 $this->_field = substr($field, 7);
339 $this->dbi = &$GLOBALS['request']->getDbh();
340 $this->_PageList_Column_base($field);
343 function _getValue($page, &$revision_handle) {
344 if (is_object($page)) $text = $page->getName();
346 $trans = $this->_plugin->translate($text, $this->_field, $this->_from_lang);
347 // how to markup untranslated words and not existing pages?
348 // untranslated: (TODO) link to translation editor
349 if ($trans == $text or // untranslated
350 (($this->_from_lang != 'en') and
351 ($this->_field != 'en') and
352 ($trans == $this->_plugin->translate($text, 'en', $this->_from_lang))
356 $link = $Theme->linkUnknownWikiWord($trans);
357 if ($this->dbi->isWikiPage($trans)) {
358 $url = WikiURL($trans, array('action' => 'TranslateText','lang' => $this->_field));
359 $button = $Theme->makeButton('T', $url);
360 $button->addTooltip(sprintf(_("Define the translation for %s in %s"), $trans, $this->_field));
361 $link = HTML::span($button);
362 $link->setAttr('class', 'wikiunknown');
363 $text = HTML::span($Theme->maybeSplitWikiWord($trans));
364 $text->setAttr('style', 'text-decoration:line-through');
365 $link->pushContent($text);
367 } elseif (is_object($page))
369 else // not existing: empty
371 } elseif (is_object($page)) {
372 return WikiLink($trans,'auto');
379 // $Log: not supported by cvs2svn $
380 // Revision 1.4 2004/03/17 13:20:31 rurban
381 // Placeholder for all yet untranslated pgsrc pagenames. Add german translations of these.
383 // Revision 1.3 2004/03/16 20:22:32 rurban
384 // added link to TranslateText action
386 // Revision 1.2 2004/03/16 15:47:27 rurban
387 // added match, fixed reverse translation, added page=, what=allpages, what=wikiwords, fixed what=pages, simplified _PageList_Column_custom
389 // Revision 1.1 2004/03/14 16:45:10 rurban
390 // Just the page matrix for now.
391 // doesn't work yet, if the default langauge != en
400 // c-hanging-comment-ender-p: nil
401 // indent-tabs-mode: nil