]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TranslateText.php
action plugin to store user contributed translations via <?plugin _WikiTranslation ?>
[SourceForge/phpwiki.git] / lib / plugin / TranslateText.php
1 <?php // -*-php-*-
2 rcs_id('$Id: TranslateText.php,v 1.1 2004-03-17 11:42:37 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 pagename is the text to be translated.
26  * One required argument: lang
27  * Usually called from <?plugin _WikiTranslation ?>
28  *
29  * Examples:
30  *    pagename="Some text in english" action=TranslateText lang=es
31  *
32  * @author:  Reini Urban
33  */
34
35 require_once("lib/plugin/_WikiTranslation.php");
36
37 class WikiPlugin_TranslateText
38 extends WikiPlugin__WikiTranslation
39 {
40     function getName() {
41         return _("TranslateText");
42     }
43
44     function getDescription() {
45         return _("Define a translation for a specified text");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision: 1.1 $");
51     }
52
53     function getDefaultArguments() {
54         return 
55             array( 'lang'      => false,
56                    'pagename'  => '[pagename]',
57                    'translate' => false,
58                  );
59     }
60
61     function run($dbi, $argstr, $request, $basepage) {
62         extract($this->getArgs($argstr, $request));
63         if (!$lang)
64             return $this->error("This internal action page cannot viewed. You can only use it via the _WikiTranslation plugin.");
65             
66         $this->lang = $lang;
67         //action=save
68         if (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 = $text .
91                         $user->getId() . " " . Iso8601DateTime() . "\n" .
92                         "* " . sprintf(_("Translate %s to %s in %s"),$pagename,
93                                                                      $trans,$lang);
94                 $meta['summary'] = sprintf(_("Translate %s to %s in %s"),substr($pagename,0,15),
95                                            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("Your translated text doesn't yet appear in this PhpWiki, but the Administrator will pick it up and add to the installation."),
100                             fmt("Your translation is stored in %s",WikiLink($transpagename)));
101             }
102         }
103         $trans = $this->translate($pagename,$lang,'en');
104         //Todo: google lookup or at least a google lookup button.
105         if (isset($header))
106             $header = HTML($header,fmt("From english to %s: ", HTML::strong($lang)));
107         else
108             $header = fmt("From english to %s: ", HTML::strong($lang));
109         $next_action = 'save';
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                                                'wrap' => 'virtual'),
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 // $Log: not supported by cvs2svn $
135 //
136
137 // For emacs users
138 // Local Variables:
139 // mode: php
140 // tab-width: 8
141 // c-basic-offset: 4
142 // c-hanging-comment-ender-p: nil
143 // indent-tabs-mode: nil
144 // End:
145 ?>