add option to only translate selected thread

This commit is contained in:
Akash Mozumdar 2020-03-27 04:07:05 -06:00
parent 8a710659dc
commit accd231c64
6 changed files with 31 additions and 18 deletions

View File

@ -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<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
{
if (HttpRequest httpRequest{

View File

@ -13,7 +13,7 @@ extern Synchronized<std::wstring> 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<std::wstring> LMTBID;
@ -87,8 +90,8 @@ std::pair<bool, std::wstring> 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",

View File

@ -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<std::wstring> key;
unsigned TKK = 0;

View File

@ -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<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo sentenceInfo);
const char* LANGUAGE = u8"Language";
@ -26,8 +29,6 @@ QFormLayout* display;
QSettings settings = openSettings();
Synchronized<std::wstring> translateTo = L"en";
bool rateLimitAll = true, rateLimitSelected = false, useCache = true;
int tokenCount = 30, tokenRestoreDelay = 60000;
Synchronized<std::map<std::wstring, std::wstring>> 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<bool&, const char*>{
{ 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);

View File

@ -28,13 +28,14 @@ constexpr bool x64 = false;
#endif
template <typename T, typename... Xs>
struct ArrayImpl { using type = std::tuple<T, Xs...>[]; };
struct ArrayImpl { using Type = std::tuple<T, Xs...>[]; };
template <typename T>
struct ArrayImpl<T> { using type = T[]; };
struct ArrayImpl<T> { using Type = T[]; };
template <typename... Ts>
using Array = typename ArrayImpl<Ts...>::type;
using Array = typename ArrayImpl<Ts...>::Type;
template <auto F> using Functor = std::integral_constant<std::remove_reference_t<decltype(F)>, F>;
template <auto F>
using Functor = std::integral_constant<std::remove_reference_t<decltype(F)>, F>;
template <typename V>
struct Identity { V operator()(V v) const { return v; } };

View File

@ -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"从游戏分离";