From 3a103890addd84502c1ec06198a0305b42daa491 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sun, 16 Jun 2019 19:43:59 -0400 Subject: [PATCH] cache translations in file --- extensions/translatewrapper.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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;