forked from Public-Mirror/Textractor
cache translations in file
This commit is contained in:
parent
28d14bcc32
commit
3a103890ad
@ -2,6 +2,7 @@
|
|||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
extern const char* SELECT_LANGUAGE;
|
extern const char* SELECT_LANGUAGE;
|
||||||
extern const char* SELECT_LANGUAGE_MESSAGE;
|
extern const char* SELECT_LANGUAGE_MESSAGE;
|
||||||
@ -12,6 +13,8 @@ extern QStringList languages;
|
|||||||
extern Synchronized<std::wstring> translateTo;
|
extern Synchronized<std::wstring> translateTo;
|
||||||
std::pair<bool, std::wstring> Translate(const std::wstring& text);
|
std::pair<bool, std::wstring> Translate(const std::wstring& text);
|
||||||
|
|
||||||
|
Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
|
||||||
|
|
||||||
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||||
{
|
{
|
||||||
switch (ul_reason_for_call)
|
switch (ul_reason_for_call)
|
||||||
@ -33,10 +36,21 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
|
|||||||
.toStdWString()
|
.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;
|
break;
|
||||||
case DLL_PROCESS_DETACH:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@ -63,7 +77,6 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|||||||
const int tokenCount = 30, delay = 60 * 1000;
|
const int tokenCount = 30, delay = 60 * 1000;
|
||||||
Synchronized<std::vector<DWORD>> tokens;
|
Synchronized<std::vector<DWORD>> tokens;
|
||||||
} rateLimiter;
|
} rateLimiter;
|
||||||
static Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
|
|
||||||
|
|
||||||
bool cache = false;
|
bool cache = false;
|
||||||
std::wstring translation;
|
std::wstring translation;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user