summaryrefslogtreecommitdiff
path: root/src/localization/localization.cpp
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-04-21 15:31:00 +0500
committerilotterytea <iltsu@alright.party>2024-04-21 15:31:00 +0500
commitd3cad81b14f41d1345d39c8bc0a1cb556bca4f09 (patch)
tree10bc5836994be45ba3ca62f124a175a64ffbb13c /src/localization/localization.cpp
parent9f3b2ea3b4391f4cbbe28463b917936c639491ec (diff)
feat: localizator
Diffstat (limited to 'src/localization/localization.cpp')
-rw-r--r--src/localization/localization.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/localization/localization.cpp b/src/localization/localization.cpp
new file mode 100644
index 0000000..82473fd
--- /dev/null
+++ b/src/localization/localization.cpp
@@ -0,0 +1,103 @@
+#include "localization.hpp"
+
+#include <algorithm>
+#include <filesystem>
+#include <fstream>
+#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) {
+ 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) {
+ 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;
+ }
+
+ }
+}