2018-10-18 05:36:56 +08:00
|
|
|
#include "extension.h"
|
|
|
|
#include <winhttp.h>
|
|
|
|
#include <regex>
|
|
|
|
#include <QInputDialog>
|
2018-10-18 22:13:33 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QThread>
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
|
|
QStringList languages
|
|
|
|
{
|
|
|
|
"English: en",
|
|
|
|
"Japanese: ja",
|
|
|
|
"Hebrew: he",
|
|
|
|
"Arabic: ar",
|
|
|
|
"Hindi: hi",
|
|
|
|
"Romanian: ro",
|
|
|
|
"Bosnian: bs-Latn",
|
|
|
|
"Russian: ru",
|
|
|
|
"Bulgarian: bg",
|
|
|
|
"Hungarian: hu",
|
|
|
|
"Serbian: sr-Cyrl",
|
|
|
|
"Catalan: ca",
|
|
|
|
"Indonesian: id",
|
|
|
|
"Chinese(Simplified): zh-CHS",
|
|
|
|
"Italian: it",
|
|
|
|
"Slovak: sk",
|
|
|
|
"Chinese(Traditional): zh-CHT",
|
|
|
|
"Slovenian: sl",
|
|
|
|
"Croatian: hr",
|
|
|
|
"Klingon: tlh", // Wait what? Apparently Bing supports this???????
|
|
|
|
"Spanish: es",
|
|
|
|
"Czech: cs",
|
|
|
|
"Swedish: sv",
|
|
|
|
"Danish: da",
|
|
|
|
"Korean: ko",
|
|
|
|
"Thai: th",
|
|
|
|
"Dutch: nl",
|
|
|
|
"Latvian: lv",
|
|
|
|
"Turkish: tr",
|
|
|
|
"Lithuanian: lt",
|
|
|
|
"Ukranian: uk",
|
|
|
|
"Estonian: et",
|
|
|
|
"Malay: ms",
|
|
|
|
"Urdu: ur",
|
|
|
|
"Finnish: fi",
|
|
|
|
"Maltese: mt",
|
|
|
|
"Vietnamese: vi",
|
|
|
|
"French: fr",
|
|
|
|
"Norwegian: no",
|
|
|
|
"Welsh: cy",
|
|
|
|
"German: de",
|
|
|
|
"Persian: fa",
|
|
|
|
"Greek: el",
|
|
|
|
"Polish: pl",
|
|
|
|
"Portuguese: pt"
|
|
|
|
};
|
|
|
|
|
2018-10-19 10:52:27 +08:00
|
|
|
// This function detects language and puts it in translateFrom if it's empty
|
|
|
|
std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstring translateTo)
|
2018-10-18 05:36:56 +08:00
|
|
|
{
|
2018-10-18 22:13:33 +08:00
|
|
|
static HINTERNET internet = NULL;
|
|
|
|
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
|
|
|
|
2018-10-18 05:36:56 +08:00
|
|
|
std::wstring translation;
|
|
|
|
if (internet)
|
|
|
|
{
|
2018-10-19 10:52:27 +08:00
|
|
|
std::wstring location = translateFrom.empty()
|
|
|
|
? L"/tdetect?text=" + text
|
|
|
|
: L"/ttranslate?from=" + translateFrom + L"&to=" + translateTo + L"&text=" + text;
|
2018-10-18 05:36:56 +08:00
|
|
|
if (HINTERNET connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
{
|
2018-10-19 10:52:27 +08:00
|
|
|
if (HINTERNET request = WinHttpOpenRequest(connection, L"POST", location.c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
2018-10-18 05:36:56 +08:00
|
|
|
{
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
|
|
|
{
|
|
|
|
DWORD bytesRead;
|
|
|
|
char buffer[10000] = {};
|
|
|
|
WinHttpReceiveResponse(request, NULL);
|
|
|
|
WinHttpReadData(request, buffer, 10000, &bytesRead);
|
2018-10-19 10:52:27 +08:00
|
|
|
wchar_t wbuffer[10000] = {};
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
|
|
|
|
if (translateFrom.empty()) translateFrom = wbuffer;
|
|
|
|
// Response formatted as JSON: translation starts with :" and ends with "}
|
|
|
|
if (std::wcmatch results; std::regex_search(wbuffer, results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
|
2018-10-18 05:36:56 +08:00
|
|
|
}
|
|
|
|
WinHttpCloseHandle(request);
|
|
|
|
}
|
|
|
|
WinHttpCloseHandle(connection);
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 10:52:27 +08:00
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
|
|
|
static std::wstring translateTo;
|
|
|
|
if (translateTo.empty() && QApplication::instance()->thread() == QThread::currentThread())
|
|
|
|
{
|
|
|
|
languages.sort();
|
|
|
|
bool ok;
|
|
|
|
QString language = QInputDialog::getItem(nullptr, "Select Language", "What language should Bing translate to?", languages, 0, false, &ok);
|
|
|
|
if (!ok) language = "English: en";
|
|
|
|
translateTo = language.split(" ")[1].toStdWString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentenceInfo["hook address"] == -1 || sentenceInfo["current select"] != 1) return false;
|
2018-10-18 05:36:56 +08:00
|
|
|
|
2018-10-19 10:52:27 +08:00
|
|
|
std::wstring translation;
|
|
|
|
std::wstring translateFrom;
|
|
|
|
Translate(sentence, translateFrom, translateTo);
|
|
|
|
translation = Translate(sentence, translateFrom, translateTo);
|
|
|
|
for (auto& c : translation) if (c == L'\\') c = 0x200b;
|
|
|
|
if (translation.empty()) translation = L"Error while translating.";
|
2018-10-18 05:36:56 +08:00
|
|
|
sentence += L"\r\n" + translation;
|
|
|
|
return true;
|
|
|
|
}
|