2020-03-26 06:12:20 -06:00
|
|
|
|
#include "qtcommon.h"
|
|
|
|
|
#include "extension.h"
|
2019-02-10 21:46:39 -05:00
|
|
|
|
#include "network.h"
|
2018-11-03 23:26:53 -04:00
|
|
|
|
#include <ctime>
|
|
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
|
2020-03-29 20:55:12 -06:00
|
|
|
|
extern Synchronized<std::wstring> translateTo, apiKey;
|
2019-07-03 12:23:10 +05:30
|
|
|
|
|
2020-03-16 02:56:04 -06:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "Google Translate";
|
2020-03-29 20:55:12 -06:00
|
|
|
|
const char* GET_API_KEY_FROM = "https://codelabs.developers.google.com/codelabs/cloud-translation-intro";
|
2018-11-03 23:26:53 -04:00
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"Afrikaans: af",
|
2020-03-26 06:12:20 -06:00
|
|
|
|
"Albanian: sq",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Amharic: am",
|
|
|
|
|
"Arabic: ar",
|
|
|
|
|
"Armenian: hy",
|
|
|
|
|
"Azerbaijani: az",
|
|
|
|
|
"Basque: eu",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Belarusian: be",
|
|
|
|
|
"Bengali: bn",
|
|
|
|
|
"Bosnian: bs",
|
|
|
|
|
"Bulgarian: bg",
|
|
|
|
|
"Catalan: ca",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Cebuano: ceb",
|
|
|
|
|
"Chichewa: ny",
|
|
|
|
|
"Chinese (simplified): zh",
|
2020-03-27 04:07:05 -06:00
|
|
|
|
"Chinese (traditional): zh-TW",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Corsican: co",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
2020-02-25 04:39:27 -07:00
|
|
|
|
"English: en",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Esperanto: eo",
|
|
|
|
|
"Estonian: et",
|
|
|
|
|
"Filipino: tl",
|
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Frisian: fy",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Galician: gl",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Georgian: ka",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Gujarati: gu",
|
|
|
|
|
"Haitian Creole: ht",
|
|
|
|
|
"Hausa: ha",
|
|
|
|
|
"Hawaiian: haw",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Hebrew: iw",
|
|
|
|
|
"Hindi: hi",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Hmong: hmn",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Hungarian: hu",
|
|
|
|
|
"Icelandic: is",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Igbo: ig",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Indonesian: id",
|
|
|
|
|
"Irish: ga",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Javanese: jw",
|
|
|
|
|
"Kannada: kn",
|
|
|
|
|
"Kazakh: kk",
|
|
|
|
|
"Khmer: km",
|
|
|
|
|
"Kinyarwanda: rw",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Korean: ko",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Kurdish (Kurmanji): ku",
|
|
|
|
|
"Kyrgyz: ky",
|
|
|
|
|
"Lao: lo",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Latin: la",
|
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Luxembourgish: lb",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Macedonian: mk",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Malagasy: mg",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Malay: ms",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Malayalam: ml",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Maltese: mt",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Maori: mi",
|
|
|
|
|
"Marathi: mr",
|
|
|
|
|
"Mongolian: mn",
|
|
|
|
|
"Myanmar (Burmese): my",
|
|
|
|
|
"Nepali: ne",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Norwegian: no",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Odia (Oriya): or",
|
|
|
|
|
"Pashto: ps",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
|
|
|
|
"Portuguese: pt",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Punjabi: pa",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Samoan: sm",
|
|
|
|
|
"Scots Gaelic: gd",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Serbian: sr",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Sesotho: st",
|
|
|
|
|
"Shona: sn",
|
|
|
|
|
"Sindhi: sd",
|
|
|
|
|
"Sinhala: si",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Somali: so",
|
|
|
|
|
"Spanish: es",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Sundanese: su",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Swahili: sw",
|
|
|
|
|
"Swedish: sv",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Tajik: tg",
|
|
|
|
|
"Tamil: ta",
|
|
|
|
|
"Tatar: tt",
|
|
|
|
|
"Telugu: te",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Thai: th",
|
|
|
|
|
"Turkish: tr",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Turkmen: tk",
|
|
|
|
|
"Ukrainian: uk",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Urdu: ur",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Uyghur: ug",
|
|
|
|
|
"Uzbek: uz",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Vietnamese: vi",
|
|
|
|
|
"Welsh: cy",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Xhosa: xh",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Yiddish: yi",
|
2020-03-27 05:55:11 -06:00
|
|
|
|
"Yoruba: yo",
|
2018-11-03 23:26:53 -04:00
|
|
|
|
"Zulu: zu"
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-27 04:07:05 -06:00
|
|
|
|
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true;
|
2020-03-29 20:55:12 -06:00
|
|
|
|
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 500;
|
2020-03-26 06:12:20 -06:00
|
|
|
|
|
2019-09-10 18:53:55 -04:00
|
|
|
|
unsigned TKK = 0;
|
|
|
|
|
|
|
|
|
|
std::wstring GetTranslationUri(const std::wstring& text)
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2018-11-08 05:51:20 -05:00
|
|
|
|
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access
|
2020-03-29 20:55:12 -06:00
|
|
|
|
if (!TKK) return FormatString(L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=t&tl=%s&q=%s", translateTo.Copy(), Escape(text));
|
2018-11-03 23:26:53 -04:00
|
|
|
|
|
2020-03-29 20:55:12 -06:00
|
|
|
|
// reverse engineered from translate.google.com
|
2019-02-10 21:46:39 -05:00
|
|
|
|
std::wstring escapedText;
|
2019-09-10 18:53:55 -04:00
|
|
|
|
unsigned a = time(NULL) / 3600, b = a; // the first part of TKK
|
2019-02-10 21:46:39 -05:00
|
|
|
|
for (unsigned char ch : WideStringToString(text))
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2019-06-13 04:01:29 -04:00
|
|
|
|
escapedText += FormatString(L"%%%02X", (int)ch);
|
2019-02-10 21:46:39 -05:00
|
|
|
|
a += ch;
|
2018-11-03 23:26:53 -04:00
|
|
|
|
a += a << 10;
|
|
|
|
|
a ^= a >> 6;
|
|
|
|
|
}
|
|
|
|
|
a += a << 3;
|
|
|
|
|
a ^= a >> 11;
|
|
|
|
|
a += a << 15;
|
|
|
|
|
a ^= TKK;
|
|
|
|
|
a %= 1000000;
|
|
|
|
|
|
2020-03-29 20:55:12 -06:00
|
|
|
|
return FormatString(L"/translate_a/single?client=webapp&dt=ld&dt=rm&dt=t&sl=auto&tl=%s&tk=%u.%u&q=%s", translateTo.Copy(), a, a ^ b, escapedText);
|
2018-11-03 23:26:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 23:16:23 -04:00
|
|
|
|
bool IsHash(const std::wstring& result)
|
|
|
|
|
{
|
2020-01-18 23:25:57 -07:00
|
|
|
|
return result.size() == 32 && std::all_of(result.begin(), result.end(), [](char ch) { return (ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'z'); });
|
2019-06-21 23:16:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 05:41:21 -06:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2020-03-29 20:55:12 -06:00
|
|
|
|
if (!apiKey->empty())
|
2020-03-26 06:12:20 -06:00
|
|
|
|
if (HttpRequest httpRequest{
|
|
|
|
|
L"Mozilla/5.0 Textractor",
|
|
|
|
|
L"translation.googleapis.com",
|
2020-03-29 20:55:12 -06:00
|
|
|
|
L"POST",
|
|
|
|
|
FormatString(L"/language/translate/v2?format=text&target=%s&key=%s", translateTo.Copy(), apiKey.Copy()).c_str(),
|
|
|
|
|
FormatString(R"({"q":["%s"]})", JSON::Escape(text))
|
2020-03-26 06:12:20 -06:00
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
// Response formatted as JSON: starts with "translatedText": " and translation is enclosed in quotes followed by a comma
|
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\"translatedText\": \"(.+?)\","))) return { true, results[1] };
|
|
|
|
|
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 (!TKK)
|
|
|
|
|
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.google.com", L"GET", L"/" })
|
|
|
|
|
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"(\\d{7,})'")))
|
|
|
|
|
_InterlockedCompareExchange(&TKK, stoll(results[1]), 0);
|
2018-11-03 23:26:53 -04:00
|
|
|
|
|
2019-09-10 18:53:55 -04:00
|
|
|
|
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.googleapis.com", L"GET", GetTranslationUri(text).c_str() })
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2019-06-13 04:01:29 -04:00
|
|
|
|
// Response formatted as JSON: starts with "[[[" and translation is enclosed in quotes followed by a comma
|
|
|
|
|
if (httpRequest.response[0] == L'[')
|
|
|
|
|
{
|
|
|
|
|
std::wstring translation;
|
|
|
|
|
for (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\\[\"(.*?)\",[n\"]")); httpRequest.response = results.suffix())
|
2019-06-21 23:16:23 -04:00
|
|
|
|
if (!IsHash(results[1])) translation += std::wstring(results[1]) + L" ";
|
2019-06-13 04:01:29 -04:00
|
|
|
|
if (!translation.empty()) return { true, translation };
|
|
|
|
|
}
|
2020-03-17 22:01:16 -06:00
|
|
|
|
return { false, FormatString(L"%s (TKK=%u): %s", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0), httpRequest.response) };
|
2018-11-03 23:26:53 -04:00
|
|
|
|
}
|
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
|
|
|
|
}
|