Skip to content

Commit e4a5c2f

Browse files
authored
Merge pull request #5 from lifenjoiner/translate
translate between any languages multiple times
2 parents ec4c06c + f54c9cf commit e4a5c2f

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

Language.cpp

+36-17
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,35 @@
3535

3636
#define MAX_STRING_LENGTH (64 * 1024)
3737

38+
std::map<std::wstring, std::wstring> CLanguage::langmapBack;
39+
3840
bool CLanguage::LoadFile(const std::wstring& path)
3941
{
4042
static std::wstring lastLangPath;
4143

42-
// revert to original language
44+
// revert to original language, special: L1 -> L2 (fail -> L0) -> L0 (En)
4345
if (_wcsicmp(lastLangPath.c_str(), path.c_str()))
4446
{
45-
std::map<std::wstring, std::wstring> langMap2;
47+
langmapBack.clear();
4648
for (auto it = langmap.cbegin(); it != langmap.cend(); ++it)
4749
{
48-
langMap2[it->second] = it->first;
50+
langmapBack[it->second] = it->first;
4951
}
50-
langmap = langMap2;
52+
langmap.clear();
5153
}
52-
53-
if (!PathFileExists(path.c_str()))
54-
return false;
55-
5654
lastLangPath = path;
5755

58-
// since stream classes still expect the filepath in char and not wchar_t
59-
// we need to convert the filepath to multibyte first
60-
61-
std::ifstream file;
62-
try
63-
{
64-
}
65-
catch (std::ios_base::failure&)
56+
if (!PathFileExists(path.c_str()))
6657
{
58+
lastLangPath = L"";
6759
return false;
6860
}
61+
62+
std::ifstream file;
6963
file.open(path);
7064
if (!file.good())
7165
{
66+
lastLangPath = L"";
7267
return false;
7368
}
7469
auto line = std::make_unique<char[]>(2 * MAX_STRING_LENGTH);
@@ -163,9 +158,33 @@ std::wstring CLanguage::GetTranslatedString(const std::wstring& s)
163158

164159
std::wstring CLanguage::GetTranslatedString(const std::wstring& s, std::map<std::wstring, std::wstring>* pLangMap)
165160
{
166-
auto foundIt = pLangMap->find(s);
161+
if (s.length() == 0)
162+
{
163+
return s;
164+
}
165+
166+
auto foundIt = langmapBack.find(s);
167+
if ((foundIt != langmapBack.end()) && (!foundIt->second.empty()))
168+
{
169+
if (!pLangMap->empty())
170+
{
171+
auto foundIt2 = pLangMap->find(foundIt->second);
172+
if ((foundIt2 != pLangMap->end()) && (!foundIt2->second.empty()))
173+
{
174+
return foundIt2->second;
175+
}
176+
}
177+
// msgId
178+
return foundIt->second;
179+
}
180+
181+
// windows initializing
182+
foundIt = pLangMap->find(s);
167183
if ((foundIt != pLangMap->end()) && (!foundIt->second.empty()))
184+
{
168185
return foundIt->second;
186+
}
187+
169188
return s;
170189
}
171190

Language.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ class CLanguage
5050
static std::wstring GetTranslatedString(const std::wstring& s, std::map<std::wstring, std::wstring>* pLangMap);
5151

5252
private:
53-
std::map<std::wstring, std::wstring> langmap;
53+
std::map<std::wstring, std::wstring> langmap;
54+
static std::map<std::wstring, std::wstring> langmapBack;
5455
};

0 commit comments

Comments
 (0)