From accd231c64356942d6ab6757a9b951a527a7744d Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 27 Mar 2020 04:07:05 -0600 Subject: [PATCH] add option to only translate selected thread --- extensions/bingtranslate.cpp | 9 ++++++--- extensions/deepltranslate.cpp | 9 ++++++--- extensions/googletranslate.cpp | 7 +++++-- extensions/translatewrapper.cpp | 10 ++++++---- include/common.h | 9 +++++---- text.cpp | 5 +++-- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index 5cd5d36..ae0ce3c 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -14,10 +14,10 @@ QStringList languages "Bangla: bn", "Bosnian: bs", "Bulgarian: bg", - "Cantonese (Traditional): yue", + "Cantonese (traditional): yue", "Catalan: ca", - "Chinese (Simplified): zh-Hans", - "Chinese (Traditional): zh-Hant", + "Chinese (simplified): zh-Hans", + "Chinese (traditional): zh-Hant", "Croatian: hr", "Czech: cs", "Danish: da", @@ -79,6 +79,9 @@ QStringList languages "Yucatec Maya: yua" }; +bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true; +int tokenCount = 30, tokenRestoreDelay = 60000; + std::pair Translate(const std::wstring& text, SentenceInfo) { if (HttpRequest httpRequest{ diff --git a/extensions/deepltranslate.cpp b/extensions/deepltranslate.cpp index 9ad9369..41b802b 100644 --- a/extensions/deepltranslate.cpp +++ b/extensions/deepltranslate.cpp @@ -13,7 +13,7 @@ extern Synchronized translateTo; const char* TRANSLATION_PROVIDER = "DeepL Translate"; QStringList languages { - "Chinese: ZH", + "Chinese (simplified): ZH", "Dutch: NL", "English: EN", "French: FR", @@ -26,6 +26,9 @@ QStringList languages "Spanish: ES", }; +bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = false; +int tokenCount = 10, tokenRestoreDelay = 60000; + const wchar_t* accept[] = { L"*/*", nullptr }; Synchronized LMTBID; @@ -87,8 +90,8 @@ std::pair Translate(const std::wstring& text, SentenceInfo s } )", ++id, r + (n - r % n), WideStringToString(translateTo->c_str()), jsonText, useContext ? WideStringToString(context->operator[](sentenceInfo["text number"])) : ""); context->insert_or_assign(sentenceInfo["text number"], L'"' + text + L'"'); - std::wstring headers = L"Host: www2.deepl.com\r\nAccept-Language: en-US,en;q=0.5\r\nContent-type: text/plain\r\nOrigin: https://www.deepl.com\r\nTE: Trailers" - + LMTBID.Acquire().contents; + // missing accept-encoding header since it fucks up HttpRequest + std::wstring headers = L"Host: www2.deepl.com\r\nAccept-Language: en-US,en;q=0.5\r\nContent-type: text/plain\r\nOrigin: https://www.deepl.com\r\nTE: Trailers" + LMTBID.Acquire().contents; if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"www2.deepl.com", diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp index 65e2b63..a617823 100644 --- a/extensions/googletranslate.cpp +++ b/extensions/googletranslate.cpp @@ -21,8 +21,8 @@ QStringList languages "Bosnian: bs", "Bulgarian: bg", "Catalan: ca", - "Chinese(Simplified): zh-CH", - "Chinese(Traditional): zh-TW", + "Chinese (simplified): zh-CH", + "Chinese (traditional): zh-TW", "Croatian: hr", "Czech: cs", "Danish: da", @@ -75,6 +75,9 @@ QStringList languages "Zulu: zu" }; +bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true; +int tokenCount = 30, tokenRestoreDelay = 60000; + Synchronized key; unsigned TKK = 0; diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index c046cff..5a3868c 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -8,6 +8,7 @@ extern const char* NATIVE_LANGUAGE; extern const char* TRANSLATE_TO; +extern const char* TRANSLATE_SELECTED_THREAD_ONLY; extern const char* RATE_LIMIT_ALL_THREADS; extern const char* RATE_LIMIT_SELECTED_THREAD; extern const char* USE_TRANS_CACHE; @@ -17,6 +18,8 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS; extern const char* TRANSLATION_PROVIDER; extern QStringList languages; +extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache; +extern int tokenCount, tokenRestoreDelay; std::pair Translate(const std::wstring& text, SentenceInfo sentenceInfo); const char* LANGUAGE = u8"Language"; @@ -26,8 +29,6 @@ QFormLayout* display; QSettings settings = openSettings(); Synchronized translateTo = L"en"; -bool rateLimitAll = true, rateLimitSelected = false, useCache = true; -int tokenCount = 30, tokenRestoreDelay = 60000; Synchronized> translationCache; int savedSize; @@ -61,6 +62,7 @@ public: display->addRow(TRANSLATE_TO, languageBox); connect(languageBox, &QComboBox::currentTextChanged, this, &Window::saveLanguage); for (auto [value, label] : Array{ + { translateSelectedOnly, TRANSLATE_SELECTED_THREAD_ONLY }, { rateLimitAll, RATE_LIMIT_ALL_THREADS }, { rateLimitSelected, RATE_LIMIT_SELECTED_THREAD }, { useCache, USE_TRANS_CACHE }, @@ -136,9 +138,9 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) if (useCache) { auto translationCache = ::translationCache.Acquire(); - if (auto it = translationCache->find(sentence); it != translationCache->end()) translation = it->second + L"\x200b"; + if (auto it = translationCache->find(sentence); it != translationCache->end()) translation = it->second + L"\x200b"; // dumb hack to not try to translate if stored empty translation } - if (translation.empty()) + if (translation.empty() && (!translateSelectedOnly || sentenceInfo["current select"])) if (rateLimiter.Request() || !rateLimitAll || (!rateLimitSelected && sentenceInfo["current select"])) std::tie(cache, translation) = Translate(sentence, sentenceInfo); else translation = TOO_MANY_TRANS_REQUESTS; if (cache) translationCache->try_emplace(sentence, translation); diff --git a/include/common.h b/include/common.h index 1eeb2c0..f62f28e 100644 --- a/include/common.h +++ b/include/common.h @@ -28,13 +28,14 @@ constexpr bool x64 = false; #endif template -struct ArrayImpl { using type = std::tuple[]; }; +struct ArrayImpl { using Type = std::tuple[]; }; template -struct ArrayImpl { using type = T[]; }; +struct ArrayImpl { using Type = T[]; }; template -using Array = typename ArrayImpl::type; +using Array = typename ArrayImpl::Type; -template using Functor = std::integral_constant, F>; +template +using Functor = std::integral_constant, F>; template struct Identity { V operator()(V v) const { return v; } }; diff --git a/text.cpp b/text.cpp index 06a5cc7..a42a5d1 100644 --- a/text.cpp +++ b/text.cpp @@ -130,8 +130,9 @@ const char* READ_ERROR = u8"Textractor: Reader ERROR (likely an incorrect R-code const char* HIJACK_ERROR = u8"Textractor: Hijack ERROR"; const char* COULD_NOT_FIND = u8"Textractor: could not find text"; const char* TRANSLATE_TO = u8"Translate to"; +const char* TRANSLATE_SELECTED_THREAD_ONLY = u8"Translate selected text thread only"; const char* RATE_LIMIT_ALL_THREADS = u8"Rate limit all text threads"; -const char* RATE_LIMIT_SELECTED_THREAD = u8"Rate limit currently selected text thread"; +const char* RATE_LIMIT_SELECTED_THREAD = u8"Rate limit selected text thread"; const char* USE_TRANS_CACHE = u8"Use translation cache"; const char* RATE_LIMIT_TOKEN_COUNT = u8"Rate limiter token count"; const char* RATE_LIMIT_TOKEN_RESTORE_DELAY = u8"Rate limiter token restore delay (ms)"; @@ -308,7 +309,7 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior #endif // SPANISH #ifdef SIMPLIFIED_CHINESE - NATIVE_LANGUAGE = "Chinese (Simplified)"; + NATIVE_LANGUAGE = "Chinese (simplified)"; ATTACH = u8"附加到游戏"; LAUNCH = u8"启动游戏"; DETACH = u8"从游戏分离";