forked from Public-Mirror/Textractor
change file name and remove global to fix threading issue
This commit is contained in:
parent
db31e19997
commit
493e80e568
@ -2,9 +2,7 @@
|
|||||||
#include "extension.h"
|
#include "extension.h"
|
||||||
#include "translatewrapper.h"
|
#include "translatewrapper.h"
|
||||||
#include "blockmarkup.h"
|
#include "blockmarkup.h"
|
||||||
#include "network.h"
|
|
||||||
#include <concurrent_priority_queue.h>
|
#include <concurrent_priority_queue.h>
|
||||||
#include <map>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
|
|
||||||
@ -25,33 +23,35 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
|
|||||||
extern const char* TRANSLATION_PROVIDER;
|
extern const char* TRANSLATION_PROVIDER;
|
||||||
extern const char* GET_API_KEY_FROM;
|
extern const char* GET_API_KEY_FROM;
|
||||||
extern const QStringList languagesTo, languagesFrom;
|
extern const QStringList languagesTo, languagesFrom;
|
||||||
extern const std::unordered_map<std::wstring, std::wstring> codes;
|
|
||||||
extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter;
|
extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter;
|
||||||
extern int tokenCount, rateLimitTimespan, maxSentenceSize;
|
extern int tokenCount, rateLimitTimespan, maxSentenceSize;
|
||||||
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp);
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp);
|
||||||
|
|
||||||
std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER);
|
|
||||||
|
|
||||||
QFormLayout* display;
|
QFormLayout* display;
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
Synchronized<TranslationParam> tlp;
|
Synchronized<TranslationParam> tlp;
|
||||||
Synchronized<std::map<std::wstring, std::wstring>> translationCache;
|
Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
|
||||||
int savedSize;
|
int savedSize = 0;
|
||||||
|
|
||||||
|
std::string CacheFile()
|
||||||
|
{
|
||||||
|
return FormatString("%s Cache (%S).txt", TRANSLATION_PROVIDER, tlp->translateTo);
|
||||||
|
}
|
||||||
void SaveCache()
|
void SaveCache()
|
||||||
{
|
{
|
||||||
std::wstring allTranslations(L"\xfeff");
|
std::wstring allTranslations(L"\xfeff");
|
||||||
for (const auto& [sentence, translation] : translationCache.Acquire().contents)
|
for (const auto& [sentence, translation] : translationCache.Acquire().contents)
|
||||||
allTranslations.append(L"|SENTENCE|").append(sentence).append(L"|TRANSLATION|").append(translation).append(L"|END|\r\n");
|
allTranslations.append(L"|SENTENCE|").append(sentence).append(L"|TRANSLATION|").append(translation).append(L"|END|\r\n");
|
||||||
std::ofstream(TRANSLATION_CACHE_FILE, std::ios::binary | std::ios::trunc).write((const char*)allTranslations.c_str(), allTranslations.size() * sizeof(wchar_t));
|
std::ofstream(CacheFile(), std::ios::binary | std::ios::trunc).write((const char*)allTranslations.c_str(), allTranslations.size() * sizeof(wchar_t));
|
||||||
savedSize = translationCache->size();
|
savedSize = translationCache->size();
|
||||||
}
|
}
|
||||||
void LoadCache()
|
void LoadCache()
|
||||||
{
|
{
|
||||||
translationCache->clear();
|
translationCache->clear();
|
||||||
std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary);
|
std::ifstream stream(CacheFile(), std::ios::binary);
|
||||||
BlockMarkupIterator savedTranslations(stream, Array<std::wstring_view>{ L"|SENTENCE|", L"|TRANSLATION|" });
|
BlockMarkupIterator savedTranslations(stream, Array<std::wstring_view>{ L"|SENTENCE|", L"|TRANSLATION|" });
|
||||||
auto translationCache = ::translationCache.Acquire();
|
auto translationCache = ::translationCache.Acquire();
|
||||||
while (auto read = savedTranslations.Next())
|
while (auto read = savedTranslations.Next())
|
||||||
@ -141,9 +141,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
void SaveTranslateTo(QString language)
|
void SaveTranslateTo(QString language)
|
||||||
{
|
{
|
||||||
settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language)));
|
|
||||||
if (translationCache->size() > savedSize) SaveCache();
|
if (translationCache->size() > savedSize) SaveCache();
|
||||||
TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache (%ls).txt", TRANSLATION_PROVIDER, codes.at(S(language)));
|
settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language)));
|
||||||
LoadCache();
|
LoadCache();
|
||||||
}
|
}
|
||||||
void SaveTranslateFrom(QString language)
|
void SaveTranslateFrom(QString language)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user