2021-01-21 22:07:09 +08:00
|
|
|
|
#include "qtcommon.h"
|
2021-06-29 12:24:59 +08:00
|
|
|
|
#include "translatewrapper.h"
|
2021-01-21 22:07:09 +08:00
|
|
|
|
#include "network.h"
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
2019-02-28 00:33:17 +08:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
|
2020-03-16 16:56:04 +08:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "Bing Translate";
|
2020-03-30 10:55:12 +08:00
|
|
|
|
const char* GET_API_KEY_FROM = "https://www.microsoft.com/en-us/translator/business/trial/#get-started";
|
2021-06-29 12:24:59 +08:00
|
|
|
|
extern const QStringList languagesTo
|
|
|
|
|
{
|
|
|
|
|
"Afrikaans",
|
|
|
|
|
"Albanian",
|
|
|
|
|
"Amharic",
|
|
|
|
|
"Arabic",
|
|
|
|
|
"Armenian",
|
|
|
|
|
"Assamese",
|
|
|
|
|
"Azerbaijani",
|
|
|
|
|
"Bangla",
|
|
|
|
|
"Bosnian (Latin)",
|
|
|
|
|
"Bulgarian",
|
|
|
|
|
"Cantonese (Traditional)",
|
|
|
|
|
"Catalan",
|
|
|
|
|
"Chinese (Simplified)",
|
|
|
|
|
"Chinese (Traditional)",
|
|
|
|
|
"Croatian",
|
|
|
|
|
"Czech",
|
|
|
|
|
"Danish",
|
|
|
|
|
"Dari",
|
|
|
|
|
"Dutch",
|
|
|
|
|
"English",
|
|
|
|
|
"Estonian",
|
|
|
|
|
"Fijian",
|
|
|
|
|
"Filipino",
|
|
|
|
|
"Finnish",
|
|
|
|
|
"French",
|
|
|
|
|
"French (Canada)",
|
|
|
|
|
"German",
|
|
|
|
|
"Greek",
|
|
|
|
|
"Gujarati",
|
|
|
|
|
"Haitian Creole",
|
|
|
|
|
"Hebrew",
|
|
|
|
|
"Hindi",
|
|
|
|
|
"Hmong Daw",
|
|
|
|
|
"Hungarian",
|
|
|
|
|
"Icelandic",
|
|
|
|
|
"Indonesian",
|
|
|
|
|
"Inuktitut",
|
|
|
|
|
"Irish",
|
|
|
|
|
"Italian",
|
|
|
|
|
"Japanese",
|
|
|
|
|
"Kannada",
|
|
|
|
|
"Kazakh",
|
|
|
|
|
"Khmer",
|
|
|
|
|
"Klingon",
|
|
|
|
|
"Korean",
|
|
|
|
|
"Kurdish (Central)",
|
|
|
|
|
"Kurdish (Northern)",
|
|
|
|
|
"Lao",
|
|
|
|
|
"Latvian",
|
|
|
|
|
"Lithuanian",
|
|
|
|
|
"Malagasy",
|
|
|
|
|
"Malay",
|
|
|
|
|
"Malayalam",
|
|
|
|
|
"Maltese",
|
|
|
|
|
"Maori",
|
|
|
|
|
"Marathi",
|
|
|
|
|
"Myanmar",
|
|
|
|
|
"Nepali",
|
|
|
|
|
"Norwegian",
|
|
|
|
|
"Odia",
|
|
|
|
|
"Pashto",
|
|
|
|
|
"Persian",
|
|
|
|
|
"Polish",
|
|
|
|
|
"Portuguese (Brazil)",
|
|
|
|
|
"Portuguese (Portugal)",
|
|
|
|
|
"Punjabi",
|
|
|
|
|
"Queretaro Otomi",
|
|
|
|
|
"Romanian",
|
|
|
|
|
"Russian",
|
|
|
|
|
"Samoan",
|
|
|
|
|
"Serbian (Cyrillic)",
|
|
|
|
|
"Serbian (Latin)",
|
|
|
|
|
"Slovak",
|
|
|
|
|
"Slovenian",
|
|
|
|
|
"Spanish",
|
|
|
|
|
"Swahili",
|
|
|
|
|
"Swedish",
|
|
|
|
|
"Tahitian",
|
|
|
|
|
"Tamil",
|
|
|
|
|
"Telugu",
|
|
|
|
|
"Thai",
|
|
|
|
|
"Tigrinya",
|
|
|
|
|
"Tongan",
|
|
|
|
|
"Turkish",
|
|
|
|
|
"Ukrainian",
|
|
|
|
|
"Urdu",
|
|
|
|
|
"Vietnamese",
|
|
|
|
|
"Welsh",
|
|
|
|
|
"Yucatec Maya"
|
|
|
|
|
}, languagesFrom = languagesTo;
|
|
|
|
|
extern const std::unordered_map<std::wstring, std::wstring> codes
|
2018-10-18 05:36:56 +08:00
|
|
|
|
{
|
2021-06-29 12:24:59 +08:00
|
|
|
|
{ { L"Afrikaans" }, { L"af" } },
|
|
|
|
|
{ { L"Albanian" }, { L"sq" } },
|
|
|
|
|
{ { L"Amharic" }, { L"am" } },
|
|
|
|
|
{ { L"Arabic" }, { L"ar" } },
|
|
|
|
|
{ { L"Armenian" }, { L"hy" } },
|
|
|
|
|
{ { L"Assamese" }, { L"as" } },
|
|
|
|
|
{ { L"Azerbaijani" }, { L"az" } },
|
|
|
|
|
{ { L"Bangla" }, { L"bn" } },
|
|
|
|
|
{ { L"Bosnian (Latin)" }, { L"bs" } },
|
|
|
|
|
{ { L"Bulgarian" }, { L"bg" } },
|
|
|
|
|
{ { L"Cantonese (Traditional)" }, { L"yue" } },
|
|
|
|
|
{ { L"Catalan" }, { L"ca" } },
|
|
|
|
|
{ { L"Chinese (Simplified)" }, { L"zh-Hans" } },
|
|
|
|
|
{ { L"Chinese (Traditional)" }, { L"zh-Hant" } },
|
|
|
|
|
{ { L"Croatian" }, { L"hr" } },
|
|
|
|
|
{ { L"Czech" }, { L"cs" } },
|
|
|
|
|
{ { L"Danish" }, { L"da" } },
|
|
|
|
|
{ { L"Dari" }, { L"prs" } },
|
|
|
|
|
{ { L"Dutch" }, { L"nl" } },
|
|
|
|
|
{ { L"English" }, { L"en" } },
|
|
|
|
|
{ { L"Estonian" }, { L"et" } },
|
|
|
|
|
{ { L"Fijian" }, { L"fj" } },
|
|
|
|
|
{ { L"Filipino" }, { L"fil" } },
|
|
|
|
|
{ { L"Finnish" }, { L"fi" } },
|
|
|
|
|
{ { L"French" }, { L"fr" } },
|
|
|
|
|
{ { L"French (Canada)" }, { L"fr-ca" } },
|
|
|
|
|
{ { L"German" }, { L"de" } },
|
|
|
|
|
{ { L"Greek" }, { L"el" } },
|
|
|
|
|
{ { L"Gujarati" }, { L"gu" } },
|
|
|
|
|
{ { L"Haitian Creole" }, { L"ht" } },
|
|
|
|
|
{ { L"Hebrew" }, { L"he" } },
|
|
|
|
|
{ { L"Hindi" }, { L"hi" } },
|
|
|
|
|
{ { L"Hmong Daw" }, { L"mww" } },
|
|
|
|
|
{ { L"Hungarian" }, { L"hu" } },
|
|
|
|
|
{ { L"Icelandic" }, { L"is" } },
|
|
|
|
|
{ { L"Indonesian" }, { L"id" } },
|
|
|
|
|
{ { L"Inuktitut" }, { L"iu" } },
|
|
|
|
|
{ { L"Irish" }, { L"ga" } },
|
|
|
|
|
{ { L"Italian" }, { L"it" } },
|
|
|
|
|
{ { L"Japanese" }, { L"ja" } },
|
|
|
|
|
{ { L"Kannada" }, { L"kn" } },
|
|
|
|
|
{ { L"Kazakh" }, { L"kk" } },
|
|
|
|
|
{ { L"Khmer" }, { L"km" } },
|
|
|
|
|
{ { L"Klingon" }, { L"tlh-Latn" } },
|
|
|
|
|
{ { L"Korean" }, { L"ko" } },
|
|
|
|
|
{ { L"Kurdish (Central)" }, { L"ku" } },
|
|
|
|
|
{ { L"Kurdish (Northern)" }, { L"kmr" } },
|
|
|
|
|
{ { L"Lao" }, { L"lo" } },
|
|
|
|
|
{ { L"Latvian" }, { L"lv" } },
|
|
|
|
|
{ { L"Lithuanian" }, { L"lt" } },
|
|
|
|
|
{ { L"Malagasy" }, { L"mg" } },
|
|
|
|
|
{ { L"Malay" }, { L"ms" } },
|
|
|
|
|
{ { L"Malayalam" }, { L"ml" } },
|
|
|
|
|
{ { L"Maltese" }, { L"mt" } },
|
|
|
|
|
{ { L"Maori" }, { L"mi" } },
|
|
|
|
|
{ { L"Marathi" }, { L"mr" } },
|
|
|
|
|
{ { L"Myanmar" }, { L"my" } },
|
|
|
|
|
{ { L"Nepali" }, { L"ne" } },
|
|
|
|
|
{ { L"Norwegian" }, { L"nb" } },
|
|
|
|
|
{ { L"Odia" }, { L"or" } },
|
|
|
|
|
{ { L"Pashto" }, { L"ps" } },
|
|
|
|
|
{ { L"Persian" }, { L"fa" } },
|
|
|
|
|
{ { L"Polish" }, { L"pl" } },
|
|
|
|
|
{ { L"Portuguese (Brazil)" }, { L"pt" } },
|
|
|
|
|
{ { L"Portuguese (Portugal)" }, { L"pt-pt" } },
|
|
|
|
|
{ { L"Punjabi" }, { L"pa" } },
|
|
|
|
|
{ { L"Queretaro Otomi" }, { L"otq" } },
|
|
|
|
|
{ { L"Romanian" }, { L"ro" } },
|
|
|
|
|
{ { L"Russian" }, { L"ru" } },
|
|
|
|
|
{ { L"Samoan" }, { L"sm" } },
|
|
|
|
|
{ { L"Serbian (Cyrillic)" }, { L"sr-Cyrl" } },
|
|
|
|
|
{ { L"Serbian (Latin)" }, { L"sr-Latn" } },
|
|
|
|
|
{ { L"Slovak" }, { L"sk" } },
|
|
|
|
|
{ { L"Slovenian" }, { L"sl" } },
|
|
|
|
|
{ { L"Spanish" }, { L"es" } },
|
|
|
|
|
{ { L"Swahili" }, { L"sw" } },
|
|
|
|
|
{ { L"Swedish" }, { L"sv" } },
|
|
|
|
|
{ { L"Tahitian" }, { L"ty" } },
|
|
|
|
|
{ { L"Tamil" }, { L"ta" } },
|
|
|
|
|
{ { L"Telugu" }, { L"te" } },
|
|
|
|
|
{ { L"Thai" }, { L"th" } },
|
|
|
|
|
{ { L"Tigrinya" }, { L"ti" } },
|
|
|
|
|
{ { L"Tongan" }, { L"to" } },
|
|
|
|
|
{ { L"Turkish" }, { L"tr" } },
|
|
|
|
|
{ { L"Ukrainian" }, { L"uk" } },
|
|
|
|
|
{ { L"Urdu" }, { L"ur" } },
|
|
|
|
|
{ { L"Vietnamese" }, { L"vi" } },
|
|
|
|
|
{ { L"Welsh" }, { L"cy" } },
|
|
|
|
|
{ { L"Yucatec Maya" }, { L"yua" } },
|
|
|
|
|
{ { L"?" }, { L"auto-detect" } }
|
2018-10-18 05:36:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-10 12:32:56 +08:00
|
|
|
|
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true;
|
2021-07-01 07:52:52 +08:00
|
|
|
|
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000;
|
2020-03-27 18:07:05 +08:00
|
|
|
|
|
2021-06-29 12:24:59 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
|
2018-10-19 10:52:27 +08:00
|
|
|
|
{
|
2021-06-29 12:24:59 +08:00
|
|
|
|
if (!tlp.authKey.empty())
|
2021-01-21 22:07:09 +08:00
|
|
|
|
{
|
2021-06-29 12:24:59 +08:00
|
|
|
|
std::wstring translateFromComponent = tlp.translateFrom == L"?" ? L"" : L"&from=" + codes.at(tlp.translateFrom);
|
2020-03-30 10:55:12 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"api.cognitive.microsofttranslator.com",
|
|
|
|
|
L"POST",
|
2021-06-29 12:24:59 +08:00
|
|
|
|
FormatString(L"/translate?api-version=3.0&to=%s%s", codes.at(tlp.translateTo), translateFromComponent).c_str(),
|
2020-12-14 21:26:01 +08:00
|
|
|
|
FormatString(R"([{"text":"%s"}])", JSON::Escape(WideStringToString(text))),
|
2021-06-29 12:24:59 +08:00
|
|
|
|
FormatString(L"Content-Type: application/json; charset=UTF-8\r\nOcp-Apim-Subscription-Key:%s", tlp.authKey).c_str()
|
2020-03-30 10:55:12 +08:00
|
|
|
|
})
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (auto translation = Copy(JSON::Parse(httpRequest.response)[0][L"translations"][0][L"text"].String())) return { true, translation.value() };
|
2020-03-30 10:55:12 +08:00
|
|
|
|
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
|
|
|
|
|
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
2021-01-21 22:07:09 +08:00
|
|
|
|
}
|
2020-03-30 10:55:12 +08:00
|
|
|
|
|
2021-06-05 22:15:19 +08:00
|
|
|
|
static Synchronized<std::wstring> token;
|
2021-06-29 12:24:59 +08:00
|
|
|
|
if (token->empty())
|
|
|
|
|
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"www.bing.com", L"GET", L"translator" })
|
|
|
|
|
if (auto tokenPos = httpRequest.response.find(L"[" + std::to_wstring(time(nullptr) / 100)); tokenPos != std::string::npos)
|
|
|
|
|
token->assign(FormatString(L"&key=%s&token=%s", httpRequest.response.substr(tokenPos + 1, 13), httpRequest.response.substr(tokenPos + 16, 32)));
|
|
|
|
|
else return { false, FormatString(L"%s: %s\ntoken not found", TRANSLATION_ERROR, httpRequest.response) };
|
|
|
|
|
else return { false, FormatString(L"%s: could not acquire token", TRANSLATION_ERROR) };
|
2019-06-13 16:01:29 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"www.bing.com",
|
|
|
|
|
L"POST",
|
2021-06-29 12:24:59 +08:00
|
|
|
|
FormatString(L"/ttranslatev3?fromLang=%s&to=%s&text=%s%s", codes.at(tlp.translateFrom), codes.at(tlp.translateTo), Escape(text), token.Copy()).c_str()
|
2019-06-13 16:01:29 +08:00
|
|
|
|
})
|
2020-12-14 21:26:01 +08:00
|
|
|
|
if (auto translation = Copy(JSON::Parse(httpRequest.response)[0][L"translations"][0][L"text"].String())) return { true, translation.value() };
|
2021-06-07 14:05:45 +08:00
|
|
|
|
else return { false, FormatString(L"%s (token=%s): %s", TRANSLATION_ERROR, std::exchange(token.Acquire().contents, L""), httpRequest.response) };
|
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
|
|
|
|
}
|