2021-01-21 22:07:09 +08:00
|
|
|
|
#include "qtcommon.h"
|
|
|
|
|
#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;
|
|
|
|
|
|
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 = "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";
|
2018-10-18 05:36:56 +08:00
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Afrikaans: af",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Arabic: ar",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Bangla: bn",
|
|
|
|
|
"Bosnian: bs",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Bulgarian: bg",
|
2020-03-27 18:07:05 +08:00
|
|
|
|
"Cantonese (traditional): yue",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Catalan: ca",
|
2020-03-27 18:07:05 +08:00
|
|
|
|
"Chinese (simplified): zh-Hans",
|
|
|
|
|
"Chinese (traditional): zh-Hant",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
2020-02-25 19:39:27 +08:00
|
|
|
|
"English: en",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Estonian: et",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Fijian: fj",
|
|
|
|
|
"Filipino: fil",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Haitian Creole: ht",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Hebrew: he",
|
|
|
|
|
"Hindi: hi",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Hmong Daw: mww",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Hungarian: hu",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Icelandic: is",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Indonesian: id",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Irish: ga",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Kannada: kn",
|
2020-03-16 17:04:40 +08:00
|
|
|
|
"Klingon: tlh",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Korean: ko",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Malagasy: mg",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Malay: ms",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Malayalam: ml",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Maltese: mt",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Maori: mi",
|
|
|
|
|
"Norwegian: nb",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Portuguese (Brazil): pt",
|
|
|
|
|
"Portuguese (Portugal): pt-pt",
|
|
|
|
|
"Punjabi: pa",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Samoan: sm",
|
|
|
|
|
"Serbian (Cyrillic): sr-Cyrl",
|
|
|
|
|
"Serbian (Latin): sr-Latn",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Spanish: es",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Swahili: sw",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Swedish: sv",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Tahitian: ty",
|
|
|
|
|
"Tamil: ta",
|
|
|
|
|
"Telugu: te",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Thai: th",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Tongan: to",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Turkish: tr",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Ukrainian: uk",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Urdu: ur",
|
|
|
|
|
"Vietnamese: vi",
|
2020-03-16 16:56:04 +08:00
|
|
|
|
"Welsh: cy",
|
|
|
|
|
"Yucatec Maya: yua"
|
2018-10-18 05:36:56 +08:00
|
|
|
|
};
|
2021-01-21 22:07:09 +08:00
|
|
|
|
std::wstring autoDetectLanguage = L"auto-detect";
|
2018-10-18 05:36:56 +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-27 18:07:05 +08:00
|
|
|
|
|
2020-04-26 10:39:12 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
2018-10-19 10:52:27 +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"&from=" + translateFrom.Copy();
|
2020-03-30 10:55:12 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"api.cognitive.microsofttranslator.com",
|
|
|
|
|
L"POST",
|
2021-01-21 22:07:09 +08:00
|
|
|
|
FormatString(L"/translate?api-version=3.0&to=%s%s", translateTo.Copy(), translateFromComponent).c_str(),
|
2020-12-14 21:26:01 +08:00
|
|
|
|
FormatString(R"([{"text":"%s"}])", JSON::Escape(WideStringToString(text))),
|
2021-01-19 09:58:21 +08:00
|
|
|
|
FormatString(L"Content-Type: application/json; charset=UTF-8\r\nOcp-Apim-Subscription-Key:%s", authKey.Copy()).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
|
|
|
|
|
2019-06-13 16:01:29 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"www.bing.com",
|
|
|
|
|
L"POST",
|
2021-01-21 22:07:09 +08:00
|
|
|
|
FormatString(L"/ttranslatev3?fromLang=%s&to=%s&text=%s", translateFrom.Copy(), translateTo.Copy(), Escape(text)).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() };
|
2020-03-18 12:01:16 +08:00
|
|
|
|
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, 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
|
|
|
|
}
|