2019-02-11 10:46:39 +08:00
|
|
|
|
#include "extension.h"
|
|
|
|
|
#include "network.h"
|
2019-06-13 16:01:29 +08:00
|
|
|
|
#include <QStringList>
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
2019-02-28 00:33:17 +08:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
|
2020-03-30 10:55:12 +08:00
|
|
|
|
extern Synchronized<std::wstring> translateTo, apiKey;
|
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
|
|
|
|
};
|
|
|
|
|
|
2020-03-27 18:07:05 +08:00
|
|
|
|
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true;
|
2020-03-30 10:55:12 +08:00
|
|
|
|
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 500;
|
2020-03-27 18:07:05 +08:00
|
|
|
|
|
2020-03-26 19:41:21 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
|
2018-10-19 10:52:27 +08:00
|
|
|
|
{
|
2020-03-30 10:55:12 +08: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(),
|
|
|
|
|
FormatString(R"([{"text":"%s"}])", JSON::Escape(text)),
|
|
|
|
|
FormatString(L"Content-Type: application/json; charset=UTF-8\r\nOcp-Apim-Subscription-Key:%s", apiKey.Copy()).c_str()
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
// Response formatted as JSON: translation starts with text":" and ends with ","to
|
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+?)\",\""))) return { true, results[1] };
|
|
|
|
|
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 16:01:29 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"www.bing.com",
|
|
|
|
|
L"POST",
|
2020-03-30 10:55:12 +08:00
|
|
|
|
FormatString(L"/ttranslatev3?fromLang=auto-detect&to=%s&text=%s", translateTo.Copy(), Escape(text)).c_str()
|
2019-06-13 16:01:29 +08:00
|
|
|
|
})
|
2020-02-01 14:24:49 +08:00
|
|
|
|
// Response formatted as JSON: translation starts with text":" and ends with ","to
|
2020-03-26 19:41:21 +08:00
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+?)\",\""))) return { true, results[1] };
|
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
|
|
|
|
}
|