]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TranslateText.php
more docs
[SourceForge/phpwiki.git] / lib / plugin / TranslateText.php
1 <?php // -*-php-*-
2 rcs_id('$Id: TranslateText.php,v 1.2 2004-03-17 12:04:36 rurban Exp $');
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 <?plugin TranslateText ?> line exists.
28  *
29  * Usually called from <?plugin _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 getVersion() {
52         return preg_replace("/[Revision: $]/", '',
53                             "\$Revision: 1.2 $");
54     }
55
56     function getDefaultArguments() {
57         return 
58             array( 'lang'      => false,
59                    'pagename'  => '[pagename]',
60                    'translate' => false,
61                  );
62     }
63
64     function run($dbi, $argstr, $request, $basepage) {
65         extract($this->getArgs($argstr, $request));
66         if (!$lang)
67             return $this->error("This internal action page cannot viewed. You can only use it via the _WikiTranslation plugin.");
68             
69         $this->lang = $lang;
70         //action=save
71         if (!empty($translate) and isset($translate['submit']) and $request->isPost()) {
72             $trans = $translate["content"];
73             if (empty($trans) or $trans == $pagename) {
74                 $header = HTML(HTML::h2(_("Translation Error!")),
75                                HTML::p(_("Your translated text is either empty or equal to the untranslated text. Please try again.")));
76             } else {
77                 //save translation in a users subpage
78                 $user = $request->getUser();
79                 $homepage = $user->_HomePagehandle;
80                 $transpagename = $homepage->getName() . SUBPAGE_SEPARATOR . _("ContributedTranslations");
81
82                 $page    = $dbi->getPage($transpagename);
83                 $current = $page->getCurrentRevision();
84                 $version = $current->getVersion();
85                 if ($version) {
86                     $text = $current->getPackedContent() . "\n";
87                     $meta = $current->_data;
88                 } else {
89                     $text = '';
90                     $meta = array('markup' => 2.0,
91                                   'author' => $user->getId());
92                 }
93                 $text = $text .
94                         $user->getId() . " " . Iso8601DateTime() . "\n" .
95                         "* " . sprintf(_("Translate %s to %s in %s"),$pagename,
96                                                                      $trans,$lang);
97                 $meta['summary'] = sprintf(_("Translate %s to %s in %s"),substr($pagename,0,15),
98                                            substr($trans,0,15),$lang);
99                 $page->save($text, $version + 1, $meta);
100                 // TODO: admin notification
101                 return HTML(HTML::h2(_("Thanks for adding this translation!")),
102                             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.", 
103                                        WIKI_NAME)),
104                             fmt("Your translation is stored in %s",WikiLink($transpagename)));
105             }
106         }
107         $trans = $this->translate($pagename,$lang,'en');
108         //Todo: google lookup or at least a google lookup button.
109         if (isset($header))
110             $header = HTML($header,fmt("From english to %s: ", HTML::strong($lang)));
111         else
112             $header = fmt("From english to %s: ", HTML::strong($lang));
113         $button_label = _("Translate");
114
115         $buttons = HTML::p(Button('submit:translate[submit]', $button_label, 'wikiadmin'),
116                            Button('submit:translate[cancel]', _("Cancel"), 'button'));
117         return HTML::form(array('action' => $request->getPostURL(),
118                                 'method' => 'post'),
119                           $header,
120                           HTML::textarea(array('class' => 'wikiedit',
121                                                'name' => 'translate[content]',
122                                                'id'   => 'translate[content]',
123                                                'rows' => 4,
124                                                'cols' => $request->getPref('editWidth'),
125                                                'wrap' => 'virtual'),
126                                          $trans),
127                           HiddenInputs($request->getArgs(),
128                                         false,
129                                         array('translate')),
130                           HiddenInputs(array('translate[action]' => $pagename,
131                                              'require_authority_for_post' => WIKIAUTH_BOGO,
132                                              )),
133                           $buttons);
134        }
135 };
136
137 // $Log: not supported by cvs2svn $
138 //
139
140 // For emacs users
141 // Local Variables:
142 // mode: php
143 // tab-width: 8
144 // c-basic-offset: 4
145 // c-hanging-comment-ender-p: nil
146 // indent-tabs-mode: nil
147 // End:
148 ?>