Skip to content

Commit

Permalink
change file name and remove global to fix threading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Artikash committed Jul 2, 2021
1 parent db31e19 commit 493e80e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions extensions/translatewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include "extension.h"
#include "translatewrapper.h"
#include "blockmarkup.h"
#include "network.h"
#include <concurrent_priority_queue.h>
#include <map>
#include <fstream>
#include <QComboBox>

Expand All @@ -25,33 +23,35 @@ 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);

std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER);

QFormLayout* display;
Settings settings;

namespace
{
Synchronized<TranslationParam> tlp;
Synchronized<std::map<std::wstring, std::wstring>> translationCache;
int savedSize;
Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
int savedSize = 0;

std::string CacheFile()
{
return FormatString("%s Cache (%S).txt", TRANSLATION_PROVIDER, tlp->translateTo);
}
void SaveCache()
{
std::wstring allTranslations(L"\xfeff");
for (const auto& [sentence, translation] : translationCache.Acquire().contents)
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();
}
void LoadCache()
{
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|" });
auto translationCache = ::translationCache.Acquire();
while (auto read = savedTranslations.Next())
Expand Down Expand Up @@ -141,9 +141,8 @@ class Window : public QDialog, Localizer
private:
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)));
settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language)));
LoadCache();
}
void SaveTranslateFrom(QString language)
Expand Down

0 comments on commit 493e80e

Please sign in to comment.