-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.cpp
96 lines (71 loc) · 2.08 KB
/
lang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "lang.h"
#include "strings_helper.h"
RZUF3_Lang* g_lang = nullptr;
RZUF3_Lang::RZUF3_Lang(std::string filepath)
{
if(g_lang != nullptr) throw std::logic_error("Only one instance of RZUF3_Lang can be present at a time");
g_lang = this;
m_filepath = filepath;
}
RZUF3_Lang::~RZUF3_Lang()
{
spdlog::info("Unloaded language file: {}", m_filepath);
g_lang = nullptr;
}
bool RZUF3_Lang::load()
{
m_loaded = false;
std::ifstream file(m_filepath);
if (!file.is_open()) {
spdlog::error("Failed to open the language file: {}", m_filepath);
return false;
}
m_texts.clear();
std::string line;
std::string accumulatedLine;
while (std::getline(file, line)) {
if (line.empty() || line.at(0) == '#') continue;
accumulatedLine += line;
if (accumulatedLine.back() == '\\') {
accumulatedLine.pop_back();
continue;
}
accumulatedLine.erase(std::remove(accumulatedLine.begin(), accumulatedLine.end(), '\r'), accumulatedLine.end());
size_t pos = accumulatedLine.find("\\n");
while (pos != std::string::npos) {
accumulatedLine.replace(pos, 2, "\n");
pos = accumulatedLine.find("\\n", pos + 1);
}
std::string key, value;
pos = accumulatedLine.find('=');
if (pos == std::string::npos) {
spdlog::error("Language file: Invalid line: {}", accumulatedLine);
accumulatedLine = "";
continue;
}
key = accumulatedLine.substr(0, pos);
value = accumulatedLine.substr(pos + 1);
RZUF3_StringsHelper::trim(key);
RZUF3_StringsHelper::trim(value);
if (m_texts.find(key) != m_texts.end()) {
spdlog::error("Language file: Duplicate key: {}", key);
accumulatedLine = "";
continue;
}
m_texts.insert(std::pair(key, value));
accumulatedLine = "";
}
m_loaded = true;
spdlog::info("Loaded language file: {} ({} entries)", m_filepath, m_texts.size());
return true;
}
std::string RZUF3_Lang::getText(std::string key)
{
if (this == nullptr || !m_loaded) return key;
auto it = m_texts.find(key);
if (it == m_texts.end()) {
spdlog::warn("Language file: No entry with a key {} found in the file {}", key, m_filepath);
return key;
}
return it->second;
}