cache filename extension translators: added target language name

This commit is contained in:
Blu3train 2021-07-01 19:37:00 +02:00 committed by Akash Mozumdar
parent d64a2c05b4
commit db31e19997

View File

@ -25,11 +25,12 @@ 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);
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; QFormLayout* display;
Settings settings; 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)); 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(); savedSize = translationCache->size();
} }
void LoadCache()
{
translationCache->clear();
std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary);
BlockMarkupIterator savedTranslations(stream, Array<std::wstring_view>{ 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 class Window : public QDialog, Localizer
@ -117,16 +131,6 @@ public:
setWindowTitle(TRANSLATION_PROVIDER); setWindowTitle(TRANSLATION_PROVIDER);
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection); QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary);
BlockMarkupIterator savedTranslations(stream, Array<std::wstring_view>{ 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() ~Window()
@ -138,6 +142,9 @@ private:
void SaveTranslateTo(QString language) void SaveTranslateTo(QString language)
{ {
settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(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) void SaveTranslateFrom(QString language)
{ {