update translation apis

This commit is contained in:
Akash Mozumdar 2019-07-16 14:03:46 +05:30
parent 6a015f04bc
commit f85e3a3841
2 changed files with 4 additions and 8 deletions

View File

@ -58,18 +58,14 @@ QStringList languages
std::pair<bool, std::wstring> Translate(const std::wstring& text)
{
std::wstring translateFrom;
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;
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"www.bing.com",
L"POST",
FormatString(L"/ttranslate?from=%s&to=%s&text=%s", translateFrom, translateTo->c_str(), Escape(text)).c_str()
FormatString(L"/ttranslatev3?fromLang=auto-detect&to=%s&text=%s", translateTo->c_str(), Escape(text)).c_str()
})
// 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] };
// Response formatted as JSON: translation starts with text":" and ends with ","
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+)\"\\,"))) return { true, results[1] };
else return { false, TRANSLATION_ERROR };
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}

View File

@ -95,7 +95,7 @@ std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
a %= 1000000;
b ^= a;
return FormatString(L"/translate_a/single?client=t&dt=ld&dt=rm&dt=t&tl=%s&tk=%u.%u&q=%s", translateTo->c_str(), a, b, escapedText);
return FormatString(L"/translate_a/single?client=webapp&dt=ld&dt=rm&dt=t&sl=auto&tl=%s&tk=%u.%u&q=%s", translateTo->c_str(), a, b, escapedText);
}
bool IsHash(const std::wstring& result)