From c074bbf5060ca2891c18a6fcdf4d125b0bf7fa3e Mon Sep 17 00:00:00 2001
From: Akash Mozumdar <akashmozumdar@gmail.com>
Date: Wed, 3 Jul 2019 12:23:10 +0530
Subject: [PATCH] only cache translation on current thread

---
 extensions/bingtranslate.cpp    |  3 ++-
 extensions/googletranslate.cpp  |  3 ++-
 extensions/translatewrapper.cpp | 10 +++-------
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp
index 3770b21..86fabe3 100644
--- a/extensions/bingtranslate.cpp
+++ b/extensions/bingtranslate.cpp
@@ -4,6 +4,8 @@
 
 extern const wchar_t* TRANSLATION_ERROR;
 
+extern Synchronized<std::wstring> translateTo;
+
 const char* TRANSLATION_PROVIDER = "Bing";
 QStringList languages
 {
@@ -53,7 +55,6 @@ QStringList languages
 	"Vietnamese: vi",
 	"Welsh: cy"
 };
-Synchronized<std::wstring> translateTo = L"en";
 
 std::pair<bool, std::wstring> Translate(const std::wstring& text)
 {
diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp
index b9c9a22..125a1a3 100644
--- a/extensions/googletranslate.cpp
+++ b/extensions/googletranslate.cpp
@@ -6,6 +6,8 @@
 
 extern const wchar_t* TRANSLATION_ERROR;
 
+extern Synchronized<std::wstring> translateTo;
+
 const char* TRANSLATION_PROVIDER = "Google";
 QStringList languages
 {
@@ -70,7 +72,6 @@ QStringList languages
 	"Yiddish: yi",
 	"Zulu: zu"
 };
-Synchronized<std::wstring> translateTo = L"en";
 
 std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
 {
diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp
index d428b06..b7cc6c7 100644
--- a/extensions/translatewrapper.cpp
+++ b/extensions/translatewrapper.cpp
@@ -9,9 +9,9 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
 
 extern const char* TRANSLATION_PROVIDER;
 extern QStringList languages;
-extern Synchronized<std::wstring> translateTo;
 std::pair<bool, std::wstring> Translate(const std::wstring& text);
 
+Synchronized<std::wstring> translateTo = L"en";
 int savedSize;
 Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
 
@@ -86,7 +86,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
 	if (translationCache->count(sentence) != 0) translation = translationCache->at(sentence);
 	else if (!(rateLimiter.Request() || sentenceInfo["current select"])) translation = TOO_MANY_TRANS_REQUESTS;
 	else std::tie(cache, translation) = Translate(sentence);
-	if (cache)
+	if (cache && sentenceInfo["current select"])
 	{
 		translationCache->insert({ sentence, translation });
 		if (translationCache->size() > savedSize + 50) SaveCache();
@@ -98,9 +98,5 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
 }
 
 TEST(
-	{
-		std::wstring test = L"こんにちは";
-		ProcessSentence(test, { SentenceInfo::DUMMY });
-		assert(test.find(L"Hello") != std::wstring::npos);
-	}
+	assert(Translate(L"こんにちは").second.find(L"ello") != std::wstring::npos)
 );