]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TranslateText.php
Harmonize file footer
[SourceForge/phpwiki.git] / lib / plugin / TranslateText.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 getName() {
44         return _("TranslateText");
45     }
46
47     function getDescription() {
48         return _("Define a translation for a specified text");
49     }
50
51     function getDefaultArguments() {
52         return
53             array( 'lang'      => false,
54                    'pagename'  => '[pagename]',
55                    'translate' => false,
56                  );
57     }
58
59     function run($dbi, $argstr, &$request, $basepage) {
60         extract($this->getArgs($argstr, $request));
61         if (!$lang)
62             return $this->error(
63                 _("This internal action page cannot viewed.")."\n".
64                 _("You can only use it via the _WikiTranslation plugin."));
65
66         $this->lang = $lang;
67         //action=save
68         if (!empty($translate) and isset($translate['submit']) and $request->isPost()) {
69             $trans = $translate["content"];
70             if (empty($trans) or $trans == $pagename) {
71                 $header = HTML(HTML::h2(_("Translation Error!")),
72                                HTML::p(_("Your translated text is either empty or equal to the untranslated text. Please try again.")));
73             } else {
74                 //save translation in a users subpage
75                 $user = $request->getUser();
76                 $homepage = $user->_HomePagehandle;
77                 $transpagename = $homepage->getName() . SUBPAGE_SEPARATOR . _("ContributedTranslations");
78
79                 $page    = $dbi->getPage($transpagename);
80                 $current = $page->getCurrentRevision();
81                 $version = $current->getVersion();
82                 if ($version) {
83                     $text = $current->getPackedContent() . "\n";
84                     $meta = $current->_data;
85                 } else {
86                     $text = '';
87                     $meta = array('markup' => 2.0,
88                                   'author' => $user->getId());
89                 }
90                 $text .= $user->getId() . " " . Iso8601DateTime() . "\n" .
91                          "* " . sprintf(_("Translate '%s' to '%s' in *%s*"),
92                                         $pagename, $trans, $lang);
93                 $text .= "\n  <verbatim>locale/po/$lang.po:\n  msgid \"".$pagename."\"\n  msgstr \"".$trans."\"\n  </verbatim>";
94                 $meta['summary'] = sprintf(_("Translate %s to %s in %s"),
95                                            substr($pagename,0,15),substr($trans,0,15),$lang);
96                 $page->save($text, $version + 1, $meta);
97                 // TODO: admin notification
98                 return HTML(HTML::h2(_("Thanks for adding this translation!")),
99                             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.",
100                                        WIKI_NAME)),
101                             fmt("Your translation is stored in %s",WikiLink($transpagename)));
102             }
103         }
104         $trans = $this->translate($pagename,$lang,'en');
105         //Todo: google lookup or at least a google lookup button.
106         if (isset($header))
107             $header = HTML($header,fmt("From english to %s: ", HTML::strong($lang)));
108         else
109             $header = fmt("From english to %s: ", HTML::strong($lang));
110         $button_label = _("Translate");
111
112         $buttons = HTML::p(Button('submit:translate[submit]', $button_label, 'wikiadmin'),
113                            Button('submit:translate[cancel]', _("Cancel"), 'button'));
114         return HTML::form(array('action' => $request->getPostURL(),
115                                 'method' => 'post'),
116                           $header,
117                           HTML::textarea(array('class' => 'wikiedit',
118                                                'name' => 'translate[content]',
119                                                'id'   => 'translate[content]',
120                                                'rows' => 4,
121                                                'cols' => $request->getPref('editWidth')
122                                                ),
123                                          $trans),
124                           HiddenInputs($request->getArgs(),
125                                         false,
126                                         array('translate')),
127                           HiddenInputs(array('translate[action]' => $pagename,
128                                              'require_authority_for_post' => WIKIAUTH_BOGO,
129                                              )),
130                           $buttons);
131        }
132 };
133
134 // Local Variables:
135 // mode: php
136 // tab-width: 8
137 // c-basic-offset: 4
138 // c-hanging-comment-ender-p: nil
139 // indent-tabs-mode: nil
140 // End:
141 ?>