DevTools DeepL Translate: sometimes the target language selection is wrong

This commit is contained in:
Blu3train 2022-02-16 21:49:08 +01:00
parent f732f488e6
commit 88de8b1f5c

View File

@ -7,6 +7,7 @@ extern const wchar_t* TRANSLATION_ERROR;
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate"; const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
const char* GET_API_KEY_FROM = nullptr; const char* GET_API_KEY_FROM = nullptr;
std::wstring currTranslateTo;
extern const QStringList languagesTo extern const QStringList languagesTo
{ {
@ -66,35 +67,35 @@ languagesFrom =
}; };
extern const std::unordered_map<std::wstring, std::wstring> codes extern const std::unordered_map<std::wstring, std::wstring> codes
{ {
{ { L"Bulgarian" }, { L"Bulgarian" } }, { { L"Bulgarian" }, { L"bg-BG" } },
{ { L"Chinese" }, { L"Chinese" } }, { { L"Chinese" }, { L"zh" } },
{ { L"Chinese (Simplified)" }, { L"Chinese (simplified)" } }, { { L"Chinese (Simplified)" }, { L"zh-ZH" } },
{ { L"Czech" }, { L"Czech" } }, { { L"Czech" }, { L"cs-CS" } },
{ { L"Danish" }, { L"Danish" } }, { { L"Danish" }, { L"da-DA" } },
{ { L"Dutch" }, { L"Dutch" } }, { { L"Dutch" }, { L"nl-NL" } },
{ { L"English" }, { L"English" } }, { { L"English" }, { L"en" } },
{ { L"English (American)" }, { L"English (American)" } }, { { L"English (American)" }, { L"en-US" } },
{ { L"English (British)" }, { L"English (British)" } }, { { L"English (British)" }, { L"en-GB" } },
{ { L"Estonian" }, { L"Estonian" } }, { { L"Estonian" }, { L"et-ET" } },
{ { L"Finnish" }, { L"Finnish" } }, { { L"Finnish" }, { L"fi-FI" } },
{ { L"French" }, { L"French" } }, { { L"French" }, { L"fr-FR" } },
{ { L"German" }, { L"German" } }, { { L"German" }, { L"de-DE" } },
{ { L"Greek" }, { L"Greek" } }, { { L"Greek" }, { L"el-EL" } },
{ { L"Hungarian" }, { L"Hungarian" } }, { { L"Hungarian" }, { L"hu-HU" } },
{ { L"Italian" }, { L"Italian" } }, { { L"Italian" }, { L"it-IT" } },
{ { L"Japanese" }, { L"Japanese" } }, { { L"Japanese" }, { L"ja-JA" } },
{ { L"Latvian" }, { L"Latvian" } }, { { L"Latvian" }, { L"lv-LV" } },
{ { L"Lithuanian" }, { L"Lithuanian" } }, { { L"Lithuanian" }, { L"lt-LT" } },
{ { L"Polish" }, { L"Polish" } }, { { L"Polish" }, { L"pl-PL" } },
{ { L"Portuguese" }, { L"Portuguese" } }, { { L"Portuguese" }, { L"pt-PT" } },
{ { L"Portuguese (Brazilian)" }, { L"Portuguese (Brazilian)" } }, { { L"Portuguese (Brazilian)" }, { L"pt-BR" } },
{ { L"Romanian" }, { L"Romanian" } }, { { L"Romanian" }, { L"ro-RO" } },
{ { L"Russian" }, { L"Russian" } }, { { L"Russian" }, { L"ru-RU" } },
{ { L"Slovak" }, { L"Slovak" } }, { { L"Slovak" }, { L"sk-SK" } },
{ { L"Slovenian" }, { L"Slovenian" } }, { { L"Slovenian" }, { L"sl-SL" } },
{ { L"Spanish" }, { L"Spanish" } }, { { L"Spanish" }, { L"es-ES" } },
{ { L"Swedish" }, { L"Swedish" } }, { { L"Swedish" }, { L"sv-SV" } },
{ { L"?" }, { L"Detect language" } } { { L"?" }, { L"auto" } }
}; };
bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true;
@ -126,16 +127,21 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationPar
std::scoped_lock lock(translationMutex); std::scoped_lock lock(translationMutex);
std::wstring escaped; // DeepL breaks with slash in input std::wstring escaped; // DeepL breaks with slash in input
for (auto ch : text) ch == '/' ? escaped += L"\\/" : escaped += ch; for (auto ch : text) ch == '/' ? escaped += L"\\/" : escaped += ch;
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#en/en/%s"})", Escape(escaped))); DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#%s/%s/%s"})", (tlp.translateFrom == L"?") ? codes.at(tlp.translateFrom) : codes.at(tlp.translateFrom).substr(0, 2), codes.at(tlp.translateTo).substr(0, 2), Escape(escaped)));
for (int retry = 0; ++retry < 20; Sleep(100)) if (currTranslateTo != tlp.translateTo)
if (Copy(DevTools::SendRequest("Runtime.evaluate", LR"({"expression":"document.readyState"})")[L"result"][L"value"].String()) == L"complete") break; {
currTranslateTo = tlp.translateTo;
for (int retry = 0; ++retry < 20; Sleep(100))
if (Copy(DevTools::SendRequest("Runtime.evaluate", LR"({"expression":"document.readyState"})")[L"result"][L"value"].String()) == L"complete") break;
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":" DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"
document.querySelector('.lmt__language_select--source').querySelector('button').click(); document.querySelector('.lmt__language_select--source').querySelector('button').click();
document.evaluate(`//*[text()='%s']`,document.querySelector('.lmt__language_select__menu'),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click(); document.querySelector('[dl-test=translator-lang-option-%s').click();
document.querySelector('.lmt__language_select--target').querySelector('button').click();
document.evaluate(`//*[text()='%s']`,document.querySelector('.lmt__language_select__menu'),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click(); document.querySelector('.lmt__language_select--target').querySelector('button').click();
"})", codes.at(tlp.translateFrom), codes.at(tlp.translateTo))); document.querySelector('[dl-test=translator-lang-option-%s]').click();
"})", (tlp.translateFrom == L"?") ? codes.at(tlp.translateFrom) : codes.at(tlp.translateFrom).substr(0, 2), codes.at(tlp.translateTo)));
}
for (int retry = 0; ++retry < 100; Sleep(100)) for (int retry = 0; ++retry < 100; Sleep(100))
if (auto translation = Copy(DevTools::SendRequest("Runtime.evaluate", if (auto translation = Copy(DevTools::SendRequest("Runtime.evaluate",