diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index 719c150..2406d16 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 @@ public: 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 @@ private: 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) {