From db31e19997188f6c9b13f2f33cff2a9c0899aa97 Mon Sep 17 00:00:00 2001 From: Blu3train Date: Thu, 1 Jul 2021 19:37:00 +0200 Subject: [PATCH] cache filename extension translators: added target language name --- extensions/translatewrapper.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index 719c150b..2406d168 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -25,11 +25,12 @@ 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 const std::unordered_map codes; extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter; extern int tokenCount, rateLimitTimespan, maxSentenceSize; std::pair Translate(const std::wstring& text, TranslationParam tlp); -const std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER); +std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER); QFormLayout* display; Settings settings; @@ -47,6 +48,19 @@ namespace std::ofstream(TRANSLATION_CACHE_FILE, std::ios::binary | std::ios::trunc).write((const char*)allTranslations.c_str(), allTranslations.size() * sizeof(wchar_t)); savedSize = translationCache->size(); } + void LoadCache() + { + translationCache->clear(); + std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary); + BlockMarkupIterator savedTranslations(stream, Array{ L"|SENTENCE|", L"|TRANSLATION|" }); + auto translationCache = ::translationCache.Acquire(); + while (auto read = savedTranslations.Next()) + { + auto& [sentence, translation] = read.value(); + translationCache->try_emplace(std::move(sentence), std::move(translation)); + } + savedSize = translationCache->size(); + } } class Window : public QDialog, Localizer @@ -117,16 +131,6 @@ class Window : public QDialog, Localizer setWindowTitle(TRANSLATION_PROVIDER); QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection); - - std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary); - BlockMarkupIterator savedTranslations(stream, Array{ L"|SENTENCE|", L"|TRANSLATION|" }); - auto translationCache = ::translationCache.Acquire(); - while (auto read = savedTranslations.Next()) - { - auto& [sentence, translation] = read.value(); - translationCache->try_emplace(std::move(sentence), std::move(translation)); - } - savedSize = translationCache->size(); } ~Window() @@ -138,6 +142,9 @@ class Window : public QDialog, Localizer void SaveTranslateTo(QString language) { settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language))); + if (translationCache->size() > savedSize) SaveCache(); + TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache (%ls).txt", TRANSLATION_PROVIDER, codes.at(S(language))); + LoadCache(); } void SaveTranslateFrom(QString language) {