Skip to content

Commit

Permalink
cache filename extension translators: added target language name
Browse files Browse the repository at this point in the history
  • Loading branch information
Blu3train authored and Artikash committed Jul 2, 2021
1 parent d64a2c0 commit db31e19
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions extensions/translatewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
extern const char* TRANSLATION_PROVIDER;
extern const char* GET_API_KEY_FROM;
extern const QStringList languagesTo, languagesFrom;
extern const std::unordered_map<std::wstring, std::wstring> codes;
extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter;
extern int tokenCount, rateLimitTimespan, maxSentenceSize;
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;
Settings settings;
Expand All @@ -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));
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
Expand Down Expand Up @@ -117,16 +131,6 @@ class Window : public QDialog, Localizer

setWindowTitle(TRANSLATION_PROVIDER);
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()
Expand All @@ -138,6 +142,9 @@ class Window : public QDialog, Localizer
void SaveTranslateTo(QString 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)
{
Expand Down

0 comments on commit db31e19

Please sign in to comment.