1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* This file is part of the KDE Project
Copyright (C) 2001 Kurt Granroth <granroth@kde.org>
Original code: plugin code, connecting to Babelfish and support for selected text
Copyright (C) 2003 Rand2342 <rand2342@yahoo.com>
Submenus, TDEConfig file and support for other translation engines
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "plugin_babelfish.h"
#include <kaction.h>
#include <kinstance.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <tdeconfig.h>
#include <kgenericfactory.h>
#include <kaboutdata.h>
#include <tdehtml_part.h>
typedef KGenericFactory<PluginBabelFish> BabelFishFactory;
static const TDEAboutData aboutdata("babelfish", I18N_NOOP("Translate Web Page") , "1.0" );
K_EXPORT_COMPONENT_FACTORY( libbabelfishplugin, BabelFishFactory( &aboutdata ) )
PluginBabelFish::PluginBabelFish( TQObject* parent, const char* name,
const TQStringList & )
: Plugin( parent, name )
{
setInstance(BabelFishFactory::instance());
m_menu = new KActionMenu( i18n("Translate Web &Page"), "babelfish",
actionCollection(), "translatewebpage" );
m_menu->setDelayed( false );
m_en = new KActionMenu( i18n("&English To"), "babelfish",
actionCollection(), "translatewebpage_en" );
m_fr = new KActionMenu( i18n("&French To"), "babelfish",
actionCollection(), "translatewebpage_fr" );
m_de = new KActionMenu( i18n("&German To"), "babelfish",
actionCollection(), "translatewebpage_de" );
m_es = new KActionMenu( i18n("&Spanish To"), "babelfish",
actionCollection(), "translatewebpage_es" );
m_pt = new KActionMenu( i18n("&Portuguese To"), "babelfish",
actionCollection(), "translatewebpage_pt" );
m_it = new KActionMenu( i18n("&Italian To"), "babelfish",
actionCollection(), "translatewebpage_it" );
m_nl = new KActionMenu( i18n("&Dutch To"), "babelfish",
actionCollection(), "translatewebpage_nl" );
m_en->insert( new KAction( i18n("&Chinese (Simplified)"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_zh") );
m_en->insert( new KAction( i18n("Chinese (&Traditional)"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_zhTW") );
m_en->insert( new KAction( i18n("&Dutch"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_nl") );
m_en->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_fr") );
m_en->insert( new KAction( i18n("&German"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_de") );
m_en->insert( new KAction( i18n("&Italian"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_it") );
m_en->insert( new KAction( i18n("&Japanese"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_ja") );
m_en->insert( new KAction( i18n("&Korean"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_ko") );
m_en->insert( new KAction( i18n("&Norwegian"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_no") );
m_en->insert( new KAction( i18n("&Portuguese"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_pt") );
m_en->insert( new KAction( i18n("&Russian"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_ru") );
m_en->insert( new KAction( i18n("&Spanish"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_es") );
m_en->insert( new KAction( i18n("T&hai"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "en_th") );
m_fr->insert( new KAction( i18n("&Dutch"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_nl") );
m_fr->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_en") );
m_fr->insert( new KAction( i18n("&German"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_de") );
m_fr->insert( new KAction( i18n("&Italian"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_it") );
m_fr->insert( new KAction( i18n("&Portuguese"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_pt") );
m_fr->insert( new KAction( i18n("&Spanish"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "fr_es") );
m_de->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "de_en") );
m_de->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "de_fr") );
m_es->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "es_en") );
m_es->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "es_fr") );
m_pt->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "pt_en") );
m_pt->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "pt_fr") );
m_it->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "it_en") );
m_it->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "it_fr") );
m_nl->insert( new KAction( i18n("&English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "nl_en") );
m_nl->insert( new KAction( i18n("&French"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "nl_fr") );
m_menu->insert( new KAction( i18n("&Chinese (Simplified) to English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "zh_en") );
m_menu->insert( new KAction( i18n("Chinese (&Traditional) to English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "zhTW_en") );
m_menu->insert( m_nl );
m_menu->insert( m_en );
m_menu->insert( m_fr );
m_menu->insert( m_de );
m_menu->insert( m_it );
m_menu->insert( new KAction( i18n("&Japanese to English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "ja_en") );
m_menu->insert( new KAction( i18n("&Korean to English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "ko_en") );
m_menu->insert( m_pt );
m_menu->insert( new KAction( i18n("&Russian to English"), 0,
this, TQT_SLOT(translateURL()),
actionCollection(), "ru_en") );
m_menu->insert( m_es );
m_menu->setEnabled( true );
// TODO: we could also support plain text viewers...
if ( parent && parent->inherits( "KHTMLPart" ) )
{
KParts::ReadOnlyPart* part = static_cast<KParts::ReadOnlyPart *>(parent);
connect( part, TQT_SIGNAL(started(TDEIO::Job*)), this,
TQT_SLOT(slotStarted(TDEIO::Job*)) );
}
}
PluginBabelFish::~PluginBabelFish()
{
delete m_menu;
}
void PluginBabelFish::slotStarted( TDEIO::Job* )
{
if ( parent()->inherits("KHTMLPart") &&
// Babelfish wants http URLs only. No https.
static_cast<KParts::ReadOnlyPart *>(parent())->url().protocol().lower() == "http" )
{
m_menu->setEnabled( true );
}
else
{
m_menu->setEnabled( false );
}
}
void PluginBabelFish::translateURL()
{
// we need the sender() for the language name
if ( !sender() )
return;
// The parent is assumed to be a KHTMLPart
if ( !parent()->inherits("KHTMLPart") )
{
TQString title = i18n( "Cannot Translate Source" );
TQString text = i18n( "Only web pages can be translated using "
"this plugin." );
KMessageBox::sorry( 0L, text, title );
return;
}
// Select engine
TDEConfig cfg( "translaterc", true );
TQString engine = cfg.readEntry( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name(), "babelfish" );
// Get URL
KHTMLPart *part = dynamic_cast<KHTMLPart *>(parent());
if ( !part )
return;
// we check if we have text selected. if so, we translate that. If
// not, we translate the url
TQString totrans;
if ( part->hasSelection() )
{
if( engine == "reverso" || engine == "tsail" )
{
KMessageBox::sorry( 0L,
i18n( "Only full webpages can be translated for this language pair." ),i18n( "Translation Error" ) );
return;
}
totrans = KURL::encode_string( part->selectedText() );
} else {
KURL url = part->url();
// Check syntax
if ( !url.isValid() )
{
TQString title = i18n( "Malformed URL" );
TQString text = i18n( "The URL you entered is not valid, please "
"correct it and try again." );
KMessageBox::sorry( 0L, text, title );
return;
}
totrans = KURL::encode_string( url.url() );
}
// Create URL
KURL result;
TQString query;
if( engine == "freetranslation" ) {
query = "sequence=core&Submit=FREE Translation&language=";
if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_es" ) )
query += "English/Spanish";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_de" ) )
query += "English/German";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_it" ) )
query += "English/Italian";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_nl" ) )
query += "English/Dutch";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_pt" ) )
query += "English/Portuguese";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_no" ) )
query += "English/Norwegian";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_zh" ) )
query += "English/SimplifiedChinese";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_zhTW" ) )
query += "English/TraditionalChinese";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "es_en" ) )
query += "Spanish/English";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "fr_en" ) )
query += "French/English";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "de_en" ) )
query += "German/English";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "it_en" ) )
query += "Italian/English";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "nl_en" ) )
query += "Dutch/English";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "pt_en" ) )
query += "Portuguese/English";
else // Should be en_fr
query += "English/French";
if ( part->hasSelection() )
{
result = KURL( "http://ets.freetranslation.com" );
query += "&mode=html&template=results_en-us.htm&srctext=";
} else {
result = KURL( "http://www.freetranslation.com/web.asp" );
query += "&url=";
}
query += totrans;
} else if( engine == "parsit" ) {
// Does only English -> Thai
result = KURL( "http://c3po.links.nectec.or.th/cgi-bin/Parsitcgi.exe" );
query = "mode=test&inputtype=";
if ( part->hasSelection() )
query += "text&TxtEng=";
else
query += "html&inputurl=";
query += totrans;
} else if( engine == "reverso" ) {
result = KURL( "http://www.reverso.net/url/frame.asp" );
query = "autotranslate=on&templates=0&x=0&y=0&directions=";
if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "de_fr" ) )
query += "524292";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "fr_en" ) )
query += "65544";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "fr_de" ) )
query += "262152";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "de_en" ) )
query += "65540";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_de" ) )
query += "262145";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_es" ) )
query += "2097153";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "es_en" ) )
query += "65568";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "fr_es" ) )
query += "2097160";
else // "en_fr"
query += "524289";
query += "&url=";
query += totrans;
} else if( engine == "tsail" ) {
result = KURL( "http://www.t-mail.com/cgi-bin/tsail" );
query = "sail=full&lp=";
if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "zhTW_en" ) )
query += "tw-en";
else if( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() == TQString( "en_zhTW" ) )
query += "en-tw";
else
{
query += TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name();
query[15] = '-';
}
query += totrans;
} else if( engine == "voila" ) {
result = KURL( "http://trans.voila.fr/voila" );
query = "systran_id=Voila-fr&systran_lp=";
query += TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name();
if ( part->hasSelection() )
query += "&systran_charset=utf-8&systran_text=";
else
query += "&systran_url=";
query += totrans;
} else {
// Using the altavista babelfish engine
result = KURL( "http://babelfish.altavista.com/babelfish/tr" );
query = "lp=";
query += TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name();
if ( part->hasSelection() )
query += "&text=";
else
query += "&url=";
query += totrans;
}
result.setQuery( query );
// Connect to the fish
emit part->browserExtension()->openURLRequest( result );
}
#include <plugin_babelfish.moc>
|