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;
|
|
|
|
|
|
2019-06-13 16:01:29 +08:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "Bing";
|
2018-10-18 05:36:56 +08:00
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"English: en",
|
|
|
|
|
"Arabic: ar",
|
|
|
|
|
"Bosnian: bs-Latn",
|
|
|
|
|
"Bulgarian: bg",
|
|
|
|
|
"Catalan: ca",
|
|
|
|
|
"Chinese(Simplified): zh-CHS",
|
|
|
|
|
"Chinese(Traditional): zh-CHT",
|
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Estonian: et",
|
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
|
|
|
|
"Hebrew: he",
|
|
|
|
|
"Hindi: hi",
|
|
|
|
|
"Hungarian: hu",
|
|
|
|
|
"Indonesian: id",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
|
|
|
|
"Klingon: tlh",
|
|
|
|
|
"Korean: ko",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
|
|
|
|
"Malay: ms",
|
|
|
|
|
"Maltese: mt",
|
|
|
|
|
"Norwegian: no",
|
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Portuguese: pt",
|
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
|
|
|
|
"Serbian: sr-Cyrl",
|
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Spanish: es",
|
|
|
|
|
"Swedish: sv",
|
|
|
|
|
"Thai: th",
|
|
|
|
|
"Turkish: tr",
|
|
|
|
|
"Ukranian: uk",
|
|
|
|
|
"Urdu: ur",
|
|
|
|
|
"Vietnamese: vi",
|
|
|
|
|
"Welsh: cy"
|
2018-10-18 05:36:56 +08:00
|
|
|
|
};
|
2019-06-13 16:01:29 +08:00
|
|
|
|
Synchronized<std::wstring> translateTo = L"en";
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
2019-06-13 16:01:29 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
2018-10-19 10:52:27 +08:00
|
|
|
|
{
|
2019-06-13 16:01:29 +08:00
|
|
|
|
std::wstring translateFrom;
|
2019-06-14 00:06:15 +08:00
|
|
|
|
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"www.bing.com", L"POST", FormatString(L"/tdetect?text=%s", Escape(text)).c_str() }) translateFrom = httpRequest.response;
|
2019-06-13 16:01:29 +08:00
|
|
|
|
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
2019-01-22 03:22:47 +08:00
|
|
|
|
|
2019-06-13 16:01:29 +08:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"www.bing.com",
|
|
|
|
|
L"POST",
|
2019-06-14 00:06:15 +08:00
|
|
|
|
FormatString(L"/ttranslate?from=%s&to=%s&text=%s", translateFrom, translateTo->c_str(), Escape(text)).c_str()
|
2019-06-13 16:01:29 +08:00
|
|
|
|
})
|
|
|
|
|
// Response formatted as JSON: translation starts with :" and ends with "}
|
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L":\"(.+)\"\\}"))) return { true, results[1] };
|
|
|
|
|
else return { false, TRANSLATION_ERROR };
|
|
|
|
|
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
2018-12-19 05:32:28 +08:00
|
|
|
|
}
|