]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TranslateText.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / TranslateText.php
1 <?php
2
3 /*
4  * Copyright 2004 $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  * TranslateText:  Translation helper
25  * The (bogus) pagename is the text to be translated.
26  * One required argument: lang
27  * Requires that an action page with the <<TranslateText >> line exists.
28  *
29  * Usually called from <<WikiTranslation >>
30  * Contributed translation are stored in UsersPage/ContributedTranslations
31  *
32  * Examples:
33  *    pagename="Some text in english" action=TranslateText lang=es
34  *
35  * @author:  Reini Urban
36  */
37
38 require_once 'lib/plugin/WikiTranslation.php';
39
40 class WikiPlugin_TranslateText
41     extends WikiPlugin_WikiTranslation
42 {
43     function getDescription()
44     {
45         return _("Define a translation for a specified text.");
46     }
47
48     function getDefaultArguments()
49     {
50         return
51             array('lang' => false,
52                 'pagename' => '[pagename]',
53                 'translate' => false,
54             );
55     }
56
57     function run($dbi, $argstr, &$request, $basepage)
58     {
59         extract($this->getArgs($argstr, $request));
60         if (!$lang)
61             return $this->error(
62                 _("This internal action page cannot viewed.") . "\n" .
63                     _("You can only use it via the WikiTranslation plugin."));
64
65         $this->lang = $lang;
66         //action=save
67         if (!empty($translate) and isset($translate['submit']) and $request->isPost()) {
68             $trans = $translate["content"];
69             if (empty($trans) or $trans == $pagename) {
70                 $header = HTML(HTML::h2(_("Translation Error!")),
71                     HTML::p(_("Your translated text is either empty or equal to the untranslated text. Please try again.")));
72             } else {
73                 //save translation in a users subpage
74                 $user = $request->getUser();
75                 $homepage = $user->_HomePagehandle;
76                 $transpagename = $homepage->getName() . SUBPAGE_SEPARATOR . _("ContributedTranslations");
77
78                 $page = $dbi->getPage($transpagename);
79                 $current = $page->getCurrentRevision();
80                 $version = $current->getVersion();
81                 if ($version) {
82                     $text = $current->getPackedContent() . "\n";
83                     $meta = $current->_data;
84                 } else {
85                     $text = '';
86                     $meta = array('author' => $user->getId());
87                 }
88                 $text .= $user->getId() . " " . Iso8601DateTime() . "\n" .
89                     "* " . sprintf(_("Translate ā€œ%sā€ to ā€œ%sā€ in *%s*"),
90                     $pagename, $trans, $lang);
91                 $text .= "\n  <verbatim>locale/po/$lang.po:\n  msgid \"" . $pagename . "\"\n  msgstr \"" . $trans . "\"\n  </verbatim>";
92                 $meta['summary'] = sprintf(_("Translate %s to %s in %s"),
93                     substr($pagename, 0, 15), substr($trans, 0, 15), $lang);
94                 $page->save($text, $version + 1, $meta);
95                 // TODO: admin notification
96                 return HTML(HTML::h2(_("Thanks for adding this translation!")),
97                     HTML::p(fmt("Your translated text doesn't yet appear in this %s, but the Administrator will pick it up and add to the installation.",
98                         WIKI_NAME)),
99                     fmt("Your translation is stored in %s", WikiLink($transpagename)));
100             }
101         }
102         $trans = $this->translate($pagename, $lang, 'en');
103         //Todo: google lookup or at least a google lookup button.
104         if (isset($header))
105             $header = HTML($header, fmt("From english to %s: ", HTML::strong($lang)));
106         else
107             $header = fmt("From english to %s: ", HTML::strong($lang));
108         $button_label = _("Translate");
109
110         $buttons = HTML::p(Button('submit:translate[submit]', $button_label, 'wikiadmin'),
111             Button('submit:translate[cancel]', _("Cancel"), 'button'));
112         return HTML::form(array('action' => $request->getPostURL(),
113                 'method' => 'post'),
114             $header,
115             HTML::textarea(array('class' => 'wikiedit',
116                     'name' => 'translate[content]',
117                     'id' => 'translate[content]',
118                     'rows' => 4,
119                     'cols' => $request->getPref('editWidth')
120                 ),
121                 $trans),
122             HiddenInputs($request->getArgs(),
123                 false,
124                 array('translate')),
125             HiddenInputs(array('translate[action]' => $pagename,
126                 'require_authority_for_post' => WIKIAUTH_BOGO,
127             )),
128             $buttons);
129     }
130 }
131
132 // Local Variables:
133 // mode: php
134 // tab-width: 8
135 // c-basic-offset: 4
136 // c-hanging-comment-ender-p: nil
137 // indent-tabs-mode: nil
138 // End: