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