2020-03-26 20:12:20 +08:00
|
|
|
|
#include "qtcommon.h"
|
2019-02-11 10:46:39 +08:00
|
|
|
|
#include "network.h"
|
2018-11-04 11:26:53 +08:00
|
|
|
|
#include <ctime>
|
|
|
|
|
|
2019-02-28 00:33:17 +08:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
|
2021-01-21 22:07:09 +08:00
|
|
|
|
extern Synchronized<std::wstring> translateTo, translateFrom, authKey;
|
2019-07-03 14:53:10 +08:00
|
|
|
|
|
2020-03-16 16:56:04 +08:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "Google Translate";
|
2020-03-30 10:55:12 +08:00
|
|
|
|
const char* GET_API_KEY_FROM = "https://codelabs.developers.google.com/codelabs/cloud-translation-intro";
|
2018-11-04 11:26:53 +08:00
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"Afrikaans: af",
|
2020-03-26 20:12:20 +08:00
|
|
|
|
"Albanian: sq",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Amharic: am",
|
|
|
|
|
"Arabic: ar",
|
|
|
|
|
"Armenian: hy",
|
|
|
|
|
"Azerbaijani: az",
|
|
|
|
|
"Basque: eu",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Belarusian: be",
|
|
|
|
|
"Bengali: bn",
|
|
|
|
|
"Bosnian: bs",
|
|
|
|
|
"Bulgarian: bg",
|
|
|
|
|
"Catalan: ca",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Cebuano: ceb",
|
|
|
|
|
"Chichewa: ny",
|
|
|
|
|
"Chinese (simplified): zh",
|
2020-03-27 18:07:05 +08:00
|
|
|
|
"Chinese (traditional): zh-TW",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Corsican: co",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
2020-02-25 19:39:27 +08:00
|
|
|
|
"English: en",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Esperanto: eo",
|
|
|
|
|
"Estonian: et",
|
|
|
|
|
"Filipino: tl",
|
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Frisian: fy",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Galician: gl",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Georgian: ka",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Gujarati: gu",
|
|
|
|
|
"Haitian Creole: ht",
|
|
|
|
|
"Hausa: ha",
|
|
|
|
|
"Hawaiian: haw",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Hebrew: iw",
|
|
|
|
|
"Hindi: hi",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Hmong: hmn",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Hungarian: hu",
|
|
|
|
|
"Icelandic: is",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Igbo: ig",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Indonesian: id",
|
|
|
|
|
"Irish: ga",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Javanese: jw",
|
|
|
|
|
"Kannada: kn",
|
|
|
|
|
"Kazakh: kk",
|
|
|
|
|
"Khmer: km",
|
|
|
|
|
"Kinyarwanda: rw",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Korean: ko",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Kurdish (Kurmanji): ku",
|
|
|
|
|
"Kyrgyz: ky",
|
|
|
|
|
"Lao: lo",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Latin: la",
|
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Luxembourgish: lb",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Macedonian: mk",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Malagasy: mg",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Malay: ms",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Malayalam: ml",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Maltese: mt",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Maori: mi",
|
|
|
|
|
"Marathi: mr",
|
|
|
|
|
"Mongolian: mn",
|
|
|
|
|
"Myanmar (Burmese): my",
|
|
|
|
|
"Nepali: ne",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Norwegian: no",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Odia (Oriya): or",
|
|
|
|
|
"Pashto: ps",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
|
|
|
|
"Portuguese: pt",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Punjabi: pa",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Samoan: sm",
|
|
|
|
|
"Scots Gaelic: gd",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Serbian: sr",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Sesotho: st",
|
|
|
|
|
"Shona: sn",
|
|
|
|
|
"Sindhi: sd",
|
|
|
|
|
"Sinhala: si",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Somali: so",
|
|
|
|
|
"Spanish: es",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Sundanese: su",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Swahili: sw",
|
|
|
|
|
"Swedish: sv",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Tajik: tg",
|
|
|
|
|
"Tamil: ta",
|
|
|
|
|
"Tatar: tt",
|
|
|
|
|
"Telugu: te",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Thai: th",
|
|
|
|
|
"Turkish: tr",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Turkmen: tk",
|
|
|
|
|
"Ukrainian: uk",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Urdu: ur",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Uyghur: ug",
|
|
|
|
|
"Uzbek: uz",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Vietnamese: vi",
|
|
|
|
|
"Welsh: cy",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Xhosa: xh",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Yiddish: yi",
|
2020-03-27 19:55:11 +08:00
|
|
|
|
"Yoruba: yo",
|
2018-11-04 11:26:53 +08:00
|
|
|
|
"Zulu: zu"
|
|
|
|
|
};
|
2021-01-21 22:07:09 +08:00
|
|
|
|
std::wstring autoDetectLanguage = L"auto";
|
2018-11-04 11:26:53 +08:00
|
|
|
|
|
2020-03-27 18:07:05 +08:00
|
|
|
|
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true;
|
2021-01-15 21:07:23 +08:00
|
|
|
|
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
|
2020-03-26 20:12:20 +08:00
|
|
|
|
|
2020-04-26 10:39:12 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
2018-11-04 11:26:53 +08:00
|
|
|
|
{
|
2021-01-19 09:58:21 +08:00
|
|
|
|
if (!authKey->empty())
|
2021-01-21 22:07:09 +08:00
|
|
|
|
{
|
|
|
|
|
std::wstring translateFromComponent = translateFrom.Copy() == autoDetectLanguage ? L"" : L"&source=" + translateFrom.Copy();
|
2020-03-26 20:12:20 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"translation.googleapis.com",
|
2020-03-30 10:55:12 +08:00
|
|
|
|
L"POST",
|
2021-01-21 22:07:09 +08:00
|
|
|
|
FormatString(L"/language/translate/v2?format=text&target=%s&key=%s%s", translateTo.Copy(), authKey.Copy(), translateFromComponent).c_str(),
|
2020-12-14 21:26:01 +08:00
|
|
|
|
FormatString(R"({"q":["%s"]})", JSON::Escape(WideStringToString(text)))
|
2020-03-26 20:12:20 +08:00
|
|
|
|
})
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"data"][L"translations"][0][L"translatedText"].String())) return { true, translation.value() };
|
|
|
|
|
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
|
2020-03-26 20:12:20 +08:00
|
|
|
|
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
2021-01-21 22:07:09 +08:00
|
|
|
|
}
|
2020-03-26 20:12:20 +08:00
|
|
|
|
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"translate.google.com",
|
|
|
|
|
L"POST",
|
|
|
|
|
L"/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc",
|
2021-01-21 22:07:09 +08:00
|
|
|
|
"f.req=" + Escape(WideStringToString(
|
|
|
|
|
FormatString(LR"([[["MkEWBc","[[\"%s\",\"%s\",\"%s\",true],[null]]",null,"generic"]]])", JSON::Escape((JSON::Escape(text))), translateFrom.Copy(), translateTo.Copy())
|
|
|
|
|
)),
|
2020-12-14 21:26:01 +08:00
|
|
|
|
L"Content-Type: application/x-www-form-urlencoded"
|
|
|
|
|
})
|
2018-11-04 11:26:53 +08:00
|
|
|
|
{
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (auto start = httpRequest.response.find(L"[["); start != std::string::npos)
|
2019-06-13 16:01:29 +08:00
|
|
|
|
{
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (auto blob = Copy(JSON::Parse(httpRequest.response.substr(start))[0][2].String())) if (auto translations = Copy(JSON::Parse(blob.value())[1][0].Array()))
|
|
|
|
|
{
|
|
|
|
|
std::wstring translation;
|
2021-01-15 21:07:23 +08:00
|
|
|
|
if (translations->size() == 1)
|
2020-12-14 21:26:01 +08:00
|
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
|
if (translations = Copy(translations.value()[0][5].Array()))
|
|
|
|
|
for (const auto& sentence : translations.value())
|
|
|
|
|
if (sentence[0].String()) (translation += *sentence[0].String()) += L" ";
|
2020-12-14 21:26:01 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (const auto& conjugation : translations.value())
|
|
|
|
|
if (auto sentence = conjugation[0].String()) if (auto gender = conjugation[2].String()) translation += FormatString(L"%s %s\n", *sentence, *gender);
|
|
|
|
|
}
|
|
|
|
|
if (!translation.empty()) return { true, translation };
|
|
|
|
|
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, blob.value()) };
|
|
|
|
|
}
|
2019-06-13 16:01:29 +08:00
|
|
|
|
}
|
2020-12-14 21:26:01 +08:00
|
|
|
|
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
|
2018-11-04 11:26:53 +08:00
|
|
|
|
}
|
2019-06-13 16:01:29 +08:00
|
|
|
|
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
2018-12-19 05:32:28 +08:00
|
|
|
|
}
|