forked from GSam/Capture2Text
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Translate.cpp
233 lines (191 loc) · 6.81 KB
/
Translate.cpp
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
/*
Copyright (C) 2010-2017 Christopher Brochtrup
This file is part of Capture2Text.
Capture2Text is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Capture2Text 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Capture2Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtNetwork/QNetworkRequest>
#include <QThread>
#include <QDebug>
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include "Translate.h"
#include "ReplyTimeout.h"
Translate::Translate()
{
connect(&manager, &QNetworkAccessManager::finished, this, &Translate::requestFinished);
}
void Translate::requestFinished(QNetworkReply *reply)
{
QString origPhrase = "";
QString replyUrlStr = reply->request().url().toString();
if(requestMap.contains(replyUrlStr))
{
origPhrase = requestMap[replyUrlStr];
requestMap.remove(replyUrlStr);
}
if(!reply->isOpen())
{
qDebug() << "Translation timeout occured!";
emit translationComplete(origPhrase, "", true);
reply->deleteLater();
return;
}
QString translationResponse(reply->readAll());
reply->close();
reply->deleteLater();
// Example single-line response (input was "Cat Dog Horse"):
// [[[\"Katze Hund Pferd\",\"Cat Dog Horse\",null,null,3,null,null,null,[[[\"8fd9cdd8f4db2bd633174a12abc58066\",\"en_de_transformer_2019q2.md\"]]]]],null,\"en\"]
// Example multi-line response (input was "Cat\nDog\nHorse"):
// [[["Katze\n","Cat\n",null,null,2],["Hund\n","Dog\n",null,null,1],["Pferd","Horse",null,null,1]],null,"en"]
QString translation;
QJsonDocument jsonDoc = QJsonDocument::fromJson(translationResponse.toUtf8());
QJsonArray jsonArray1 = jsonDoc.array();
if(jsonArray1.size() > 0)
{
QJsonValue jsonValue1 = jsonArray1[0];
QJsonArray jsonArray2 = jsonValue1.toArray();
foreach (const QJsonValue & jsonValue2, jsonArray2)
{
QJsonArray jsonArray3 = jsonValue2.toArray();
if(jsonArray3.size() > 0)
{
QJsonValue jsonTranslation = jsonArray3[0];
translation += jsonTranslation.toString();
}
}
}
translation = translation.replace("\\n", "\n");
//qDebug() << "Translation: " << translation;
emit translationComplete(origPhrase, translation, false);
}
// https://stackoverflow.com/questions/8550147/how-to-use-google-translate-api-with-c/18813418#18813418
bool Translate::startTranslate(QString phrase, QString from, QString to, int timeoutMillisec=2000)
{
QString langCodeFrom;
QString langCodeTo;
if(mapLang.contains(from))
{
langCodeFrom = mapLang[from];
}
else
{
return false;
}
if(mapLang.contains(to))
{
langCodeTo = mapLang[to];
}
else
{
return false;
}
// Example request:
// http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ja&dt=t&q=hello
// Answer:
// [[["こんにちは","hello",,,1]],,"en",,,,1,,[["en"],,[1],["en"]]]
QString url = QString("http://translate.googleapis.com/translate_a/single?client=gtx&sl=%1&tl=%2&dt=t&q=%3")
.arg(langCodeFrom, langCodeTo, phrase);
// For debug, here is a URL that will timeout
//QString url = QString("http://wiki.forum.nokia.com/index.php/How_to_wait_synchronously_for_a_Signal_in_Qt");
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
requestMap.insert(reply->request().url().toString(), phrase);
ReplyTimeout::set(reply, timeoutMillisec);
//qDebug() << "Starting translation...";
return true;
}
QStringList Translate::getAvailableLangs()
{
QStringList list;
for (auto item : mapLang.keys())
{
list.append(item);
}
list.sort();
return list;
}
bool Translate::isValidLang(QString lang)
{
return mapLang.contains(lang);
}
QMap<QString, QString> Translate::populateLangMap()
{
QMap<QString, QString> map;
map.insert("Afrikaans", "af");
map.insert("Albanian", "sq");
map.insert("Arabic", "ar");
map.insert("Azerbaijani", "az");
map.insert("Basque", "eu");
map.insert("Bengali", "bn");
map.insert("Belarusian", "be");
map.insert("Bulgarian", "bg");
map.insert("Catalan", "ca");
map.insert("Chinese - Simplified", "zh-CN");
map.insert("Chinese - Traditional", "zh-TW");
map.insert("Croatian", "hr");
map.insert("Czech", "cs");
map.insert("Danish", "da");
map.insert("Dutch", "nl");
map.insert("English", "en");
map.insert("Esperanto", "eo");
map.insert("Estonian", "et");
map.insert("Filipino", "tl");
map.insert("Finnish", "fi");
map.insert("French", "fr");
map.insert("Galician", "gl");
map.insert("Georgian", "ka");
map.insert("German", "de");
map.insert("Greek", "el");
map.insert("Gujarati", "gu");
map.insert("Haitian", "ht");
map.insert("Hebrew", "iw");
map.insert("Hindi", "hi");
map.insert("Hungarian", "hu");
map.insert("Icelandic", "is");
map.insert("Indonesian", "id");
map.insert("Irish", "ga");
map.insert("Italian", "it");
map.insert("Japanese", "ja");
map.insert("Kannada", "kn");
map.insert("Korean", "ko");
map.insert("Latin", "la");
map.insert("Latvian", "lv");
map.insert("Lithuanian", "lt");
map.insert("Macedonian", "mk");
map.insert("Malay", "ms");
map.insert("Maltese", "mt");
map.insert("Norwegian", "no");
map.insert("Persian", "fa");
map.insert("Polish", "pl");
map.insert("Portuguese", "pt");
map.insert("Romanian", "ro");
map.insert("Russian", "ru");
map.insert("Serbian", "sr");
map.insert("Slovak", "sk");
map.insert("Slovenian", "sl");
map.insert("Spanish", "es");
map.insert("Swahili", "sw");
map.insert("Swedish", "sv");
map.insert("Tamil", "ta");
map.insert("Telugu", "te");
map.insert("Thai", "th");
map.insert("Turkish", "tr");
map.insert("Ukrainian", "uk");
map.insert("Urdu", "ur");
map.insert("Vietnamese", "vi");
map.insert("Welsh", "cy");
map.insert("Yiddish", "yi");
return map;
}
const QMap<QString, QString> Translate::mapLang = populateLangMap();