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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include "localization.hpp"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <map>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "../utils/string.hpp"
#include "line_id.hpp"
namespace bot {
namespace loc {
Localization::Localization(const std::string &folder_path) {
for (const auto &entry :
std::filesystem::directory_iterator(folder_path)) {
std::vector<std::string> file_name_parts =
utils::string::split_text(entry.path(), '/');
std::string file_name = file_name_parts[file_name_parts.size() - 1];
file_name = file_name.substr(0, file_name.length() - 5);
std::unordered_map<LineId, std::string> lines =
this->load_from_file(entry.path());
this->localizations[file_name] = lines;
}
}
std::unordered_map<LineId, std::string> Localization::load_from_file(
const std::string &file_path) {
std::ifstream ifs(file_path);
std::unordered_map<LineId, std::string> map;
nlohmann::json json;
ifs >> json;
for (auto it = json.begin(); it != json.end(); ++it) {
std::optional<LineId> line_id = string_to_line_id(it.key());
if (line_id.has_value()) {
map[line_id.value()] = it.value();
}
}
ifs.close();
return map;
}
std::optional<std::string> Localization::get_localized_line(
const std::string &locale_id, const LineId &line_id) const {
auto locale_it =
std::find_if(this->localizations.begin(), this->localizations.end(),
[&](const auto &x) { return x.first == locale_id; });
if (locale_it == this->localizations.end()) {
return std::nullopt;
}
auto line_it =
std::find_if(locale_it->second.begin(), locale_it->second.end(),
[&](const auto &x) { return x.first == line_id; });
if (line_it == locale_it->second.end()) {
return std::nullopt;
}
return line_it->second;
}
std::optional<std::string> Localization::get_formatted_line(
const std::string &locale_id, const LineId &line_id,
const std::vector<std::string> &args) const {
std::optional<std::string> o_line =
this->get_localized_line(locale_id, line_id);
if (!o_line.has_value()) {
return std::nullopt;
}
std::string line = o_line.value();
int pos = 0;
int index = 0;
while ((pos = line.find("%s", pos)) != std::string::npos) {
line.replace(pos, 2, args[index]);
pos += args[index].size();
++index;
if (index >= args.size()) {
break;
}
}
return line;
}
std::optional<std::string> Localization::get_formatted_line(
const command::Request &request, const LineId &line_id,
const std::vector<std::string> &args) const {
std::optional<std::string> o_line = this->get_formatted_line(
request.channel_preferences.get_locale(), line_id, args);
if (!o_line.has_value()) {
return std::nullopt;
}
std::string line = o_line.value();
std::map<std::string, std::string> token_map = {
{"{sender.alias_name}", request.user.get_alias_name()},
{"{source.alias_name}", request.channel.get_alias_name()},
{"{default.prefix}", DEFAULT_PREFIX},
{"{channel.prefix}", request.channel_preferences.get_prefix()}};
for (const auto &pair : token_map) {
int pos = line.find(pair.first);
while (pos != std::string::npos) {
line.replace(pos, pair.first.length(), pair.second);
pos = line.find(pair.first, pos + pair.second.length());
}
}
return line;
}
const std::vector<std::string> Localization::get_loaded_localizations()
const {
std::vector<std::string> output;
std::transform(this->localizations.begin(), this->localizations.end(),
std::back_inserter(output),
[](const auto &x) { return x.first; });
return output;
}
}
}
|