diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index e695fd8..a02baf7 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -2,6 +2,7 @@ #include "network.h" #include #include +#include extern const char* SELECT_LANGUAGE; extern const char* SELECT_LANGUAGE_MESSAGE; @@ -12,6 +13,8 @@ extern QStringList languages; extern Synchronized translateTo; std::pair Translate(const std::wstring& text); +Synchronized> translationCache; + BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) @@ -33,10 +36,21 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved .toStdWString() ); }); + + QFile file(QString("%1 Cache.txt").arg(TRANSLATION_PROVIDER)); + file.open(QIODevice::ReadOnly | QIODevice::Text); + QStringList savedCache = QString(file.readAll()).split("|T|\n", QString::SkipEmptyParts); + for (int i = 0; i < savedCache.size() - 1; i += 2) + translationCache->insert({ savedCache[i].toStdWString(), savedCache[i + 1].toStdWString() }); } break; case DLL_PROCESS_DETACH: { + QFile file(QString("%1 Cache.txt").arg(TRANSLATION_PROVIDER)); + file.open(QIODevice::WriteOnly | QIODevice::Text); + auto translationCache = ::translationCache.Acquire(); + for (const auto& [original, translation] : translationCache.contents) + file.write(QString::fromStdWString(FormatString(L"%s|T|\n%s|T|\n", original, translation)).toUtf8()); } break; } @@ -63,7 +77,6 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) const int tokenCount = 30, delay = 60 * 1000; Synchronized> tokens; } rateLimiter; - static Synchronized> translationCache; bool cache = false; std::wstring translation;