From bda08d500eeb5a8417a54e9d9206b1bc7a88a43f Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 17 Aug 2021 22:48:16 -0600 Subject: [PATCH] fix minor bugs with rate limiter, update urls --- extensions/bingtranslate.cpp | 2 +- extensions/deepltranslate.cpp | 4 ++-- extensions/devtoolsdeepltranslate.cpp | 2 +- extensions/devtoolspapagotranslate.cpp | 2 +- extensions/devtoolssystrantranslate.cpp | 2 +- extensions/googletranslate.cpp | 4 ++-- extensions/translatewrapper.cpp | 8 ++++---- text.cpp | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index f82d5a3..04aac88 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -192,7 +192,7 @@ extern const std::unordered_map codes { { L"?" }, { L"auto-detect" } } }; -bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true; +bool translateSelectedOnly = false, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000; std::pair Translate(const std::wstring& text, TranslationParam tlp) diff --git a/extensions/deepltranslate.cpp b/extensions/deepltranslate.cpp index 7e917bb..5af759b 100644 --- a/extensions/deepltranslate.cpp +++ b/extensions/deepltranslate.cpp @@ -6,7 +6,7 @@ extern const wchar_t* TRANSLATION_ERROR; const char* TRANSLATION_PROVIDER = "DeepL Translate"; -const char* GET_API_KEY_FROM = "https://www.deepl.com/pro.html"; +const char* GET_API_KEY_FROM = "https://www.deepl.com/pro.html#developer"; extern const QStringList languagesTo { "Bulgarian", @@ -97,7 +97,7 @@ extern const std::unordered_map codes { { L"?" }, { L"auto" } } }; -bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = true, useFilter = true; +bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = true, useCache = true, useFilter = true; int tokenCount = 10, rateLimitTimespan = 60000, maxSentenceSize = 1000; enum KeyType { CAT, REST }; diff --git a/extensions/devtoolsdeepltranslate.cpp b/extensions/devtoolsdeepltranslate.cpp index 2461f22..8a90652 100644 --- a/extensions/devtoolsdeepltranslate.cpp +++ b/extensions/devtoolsdeepltranslate.cpp @@ -97,7 +97,7 @@ extern const std::unordered_map codes { { L"?" }, { L"Any language (detect)" } } }; -bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true; +bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500; BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) diff --git a/extensions/devtoolspapagotranslate.cpp b/extensions/devtoolspapagotranslate.cpp index 0a7f13f..0934008 100644 --- a/extensions/devtoolspapagotranslate.cpp +++ b/extensions/devtoolspapagotranslate.cpp @@ -46,7 +46,7 @@ extern const std::unordered_map codes { { L"?" }, { L"auto" } } }; -bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true; +bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500; BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) diff --git a/extensions/devtoolssystrantranslate.cpp b/extensions/devtoolssystrantranslate.cpp index d4db36c..ab7184b 100644 --- a/extensions/devtoolssystrantranslate.cpp +++ b/extensions/devtoolssystrantranslate.cpp @@ -112,7 +112,7 @@ extern const std::unordered_map codes { { L"?" }, { L"autodetect" } } }; -bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true; +bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500; BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp index 5fae5eb..26d8d41 100644 --- a/extensions/googletranslate.cpp +++ b/extensions/googletranslate.cpp @@ -5,7 +5,7 @@ extern const wchar_t* TRANSLATION_ERROR; const char* TRANSLATION_PROVIDER = "Google Translate"; -const char* GET_API_KEY_FROM = "https://codelabs.developers.google.com/codelabs/cloud-translation-intro"; +const char* GET_API_KEY_FROM = "https://console.cloud.google.com/marketplace/product/google/translate.googleapis.com"; extern const QStringList languagesTo { "Afrikaans", @@ -232,7 +232,7 @@ extern const std::unordered_map codes { { L"?" }, { L"auto" } } }; -bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true, useFilter = true; +bool translateSelectedOnly = false, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true; int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 1000; std::pair Translate(const std::wstring& text, TranslationParam tlp) diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index 8b95f18..9631dc2 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -23,7 +23,7 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS; extern const char* TRANSLATION_PROVIDER; extern const char* GET_API_KEY_FROM; extern const QStringList languagesTo, languagesFrom; -extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter; +extern bool translateSelectedOnly, useRateLimiter, rateLimitSelected, useCache, useFilter; extern int tokenCount, rateLimitTimespan, maxSentenceSize; std::pair Translate(const std::wstring& text, TranslationParam tlp); @@ -94,7 +94,7 @@ public: connect(translateFromCombo, &QComboBox::currentTextChanged, this, &Window::SaveTranslateFrom); for (auto [value, label] : Array{ { translateSelectedOnly, TRANSLATE_SELECTED_THREAD_ONLY }, - { rateLimitAll, RATE_LIMIT_ALL_THREADS }, + { useRateLimiter, RATE_LIMIT_ALL_THREADS }, { rateLimitSelected, RATE_LIMIT_SELECTED_THREAD }, { useCache, USE_TRANS_CACHE }, { useFilter, FILTER_GARBAGE } @@ -167,7 +167,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) break; } tokens.push(current); - return tokens.size() < tokenCount; + return tokens.size() <= tokenCount; } private: @@ -193,7 +193,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) 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() && (!translateSelectedOnly || sentenceInfo["current select"])) - if (rateLimiter.Request() || !rateLimitAll || (!rateLimitSelected && sentenceInfo["current select"])) std::tie(cache, translation) = Translate(sentence, tlp.Copy()); + if (rateLimiter.Request() || !useRateLimiter || (!rateLimitSelected && sentenceInfo["current select"])) std::tie(cache, translation) = Translate(sentence, tlp.Copy()); else translation = TOO_MANY_TRANS_REQUESTS; if (useFilter) Trim(translation); if (cache) translationCache->try_emplace(sentence, translation); diff --git a/text.cpp b/text.cpp index ef9e8cf..c624979 100644 --- a/text.cpp +++ b/text.cpp @@ -138,7 +138,7 @@ const char* TRANSLATE_TO = u8"Translate to"; const char* TRANSLATE_FROM = u8"Translate from"; const char* FILTER_GARBAGE = u8"Filter garbage characters"; 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_ALL_THREADS = u8"Use rate limiter"; const char* RATE_LIMIT_SELECTED_THREAD = u8"Rate limit selected text thread"; const char* USE_TRANS_CACHE = u8"Use translation cache"; const char* MAX_TRANSLATIONS_IN_TIMESPAN = u8"Max translation requests in timespan";