2019-02-10 21:46:39 -05:00
|
|
|
|
#include "extension.h"
|
|
|
|
|
#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;
|
|
|
|
|
|
2019-07-03 12:23:10 +05:30
|
|
|
|
extern Synchronized<std::wstring> translateTo;
|
|
|
|
|
|
2020-03-16 02:56:04 -06:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "Bing Translate";
|
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-16 02:56:04 -06:00
|
|
|
|
"Cantonese (Traditional): yue",
|
2018-10-17 17:36:56 -04:00
|
|
|
|
"Catalan: ca",
|
2020-03-16 02:56:04 -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-26 05:41:21 -06:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
|
2018-10-18 22:52:27 -04:00
|
|
|
|
{
|
2019-06-13 04:01:29 -04:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"www.bing.com",
|
|
|
|
|
L"POST",
|
2019-07-16 14:03:46 +05:30
|
|
|
|
FormatString(L"/ttranslatev3?fromLang=auto-detect&to=%s&text=%s", translateTo->c_str(), Escape(text)).c_str()
|
2019-06-13 04:01:29 -04:00
|
|
|
|
})
|
2020-01-31 23:24:49 -07:00
|
|
|
|
// Response formatted as JSON: translation starts with text":" and ends with ","to
|
2020-03-26 05:41:21 -06:00
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+?)\",\""))) return { true, results[1] };
|
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
|
|
|
|
}
|