-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_popup.cgi
executable file
·126 lines (107 loc) · 3.21 KB
/
translate_popup.cgi
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
#!/usr/bin/perl
# Copyright (C) 2004-2021
# Emmanuel Saracco <emmanuel@esaracco.fr>
#
# GNU GENERAL PUBLIC LICENSE
use LWP::UserAgent;
use HTML::Entities;
use JSON;
require './translator-lib.pl';
# Reverso languages
# public web page (http://www.reverso.net/text_translation.aspx)
require "$root_directory/$module_name/data/reverso_languages.pm";
my $color = 'blue';
my $translation = '';
print qq(Content-Type: text/html; charset=utf-8\n\n);
##### POST actions #####
#
# do the translation
if ($in{'translate'})
{
my $str = $in{'text'};
$str =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//msgi;
my $req = HTTP::Request->new ('POST', 'https://api.reverso.net/translate/v1/translation');
$req->content (to_json ({
'input' => $str,
'from' => $in{'lang1'},
'to' => $in{'lang2'},
'format' => 'text',
'options' => {
'sentenceSplitter' => \0,
'contextResults' => \0,
'languageDetection' => \1
}
}));
my $ua = LWP::UserAgent->new ();
$ua->timeout (30);
$ua->default_headers->header (
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Accept-Charset' => 'utf-8',
'Connection' => 'close',
'Referer' => 'https://www.reverso.net/text_translation.aspx',
'User-Agent' =>
'Mozilla/5.0 (X11; Linux x86_64) '.
'AppleWebKit/537.36 (KHTML, like Gecko) '.
'Chrome/32.0.1700.123 Safari/537.36'
);
my $r = $ua->request ($req);
if ($r->is_success)
{
my $data = from_json ($r->decoded_content, {'utf8' => 1});
$translation = ucfirst (decode_entities ($data->{'translation'}->[0]));
}
else
{
$translation = $text{'TRANSLATE_ERROR'};
$color = 'red';
}
}
else
{
($in{'lang1'}, $in{'lang2'}) =
&trans_init_translate_popup_langs ($in{'lang2'}, \%reverso_langs);
}
#
########################
printf (qq(
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>$text{'TRANSLATE_CONSOLE_LINK'}</title>
%s
</head>
<body>), &trans_header_extra());
print qq(<form action="translate_popup.cgi" method="post">);
print qq(<table class="trans" width="100%">);
print qq(<tr><td>);
print qq(<select name="lang1">);
foreach my $lang (sort keys (%reverso_langs))
{
my $code = $reverso_langs{$lang};
my $selected = ($code eq $in{'lang1'}) ? ' selected="selected"' : '';
print qq(<option value="$code"$selected>$lang</option>\n);
}
print qq(</select>);
print " -> ";
print qq(<select name="lang2">);
foreach my $lang (sort keys (%reverso_langs))
{
my $code = $reverso_langs{$lang};
my $selected = ($code eq $in{'lang2'}) ? ' selected="selected"' : '';
print qq(<option value="$code"$selected>$lang</option>\n);
}
print qq(</select>);
print qq( <input type="submit" value="$text{'TRANSLATE'}" name="translate"> <small>(<a href="http://www.reverso.net/text_translation.aspx" target="_blank">Reverso.net</a>)</small></td></tr>);
if ($translation)
{
print qq(<tr><td><font color="$color"><code>);
$translation =~ s/\\//g;
print &html_escape ($translation);
print qq(</code></font></td></tr>);
}
print '<tr><td><textarea name="text" rows=18>',&html_escape($in{'text'}),'</textarea></td></tr>';
print qq(</table>);
print qq(</form>);
print qq(</body></html>);