Textractor/extensions/bingtranslate.cpp

114 lines
3.3 KiB
C++
Raw Normal View History

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