diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
| commit | d1793df1eda463b10107d41785ad1d7f055ed476 (patch) | |
| tree | fd3e41c3b4a05924748ae4b762e1ae55a0bc815c /bot/src/localization | |
| parent | d7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff) | |
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'bot/src/localization')
| -rw-r--r-- | bot/src/localization/line_id.cpp | 100 | ||||
| -rw-r--r-- | bot/src/localization/line_id.hpp | 56 | ||||
| -rw-r--r-- | bot/src/localization/localization.cpp | 132 | ||||
| -rw-r--r-- | bot/src/localization/localization.hpp | 37 |
4 files changed, 325 insertions, 0 deletions
diff --git a/bot/src/localization/line_id.cpp b/bot/src/localization/line_id.cpp new file mode 100644 index 0000000..567a3ba --- /dev/null +++ b/bot/src/localization/line_id.cpp @@ -0,0 +1,100 @@ +#include "line_id.hpp" + +#include <optional> +#include <string> + +namespace bot { + namespace loc { + std::optional<LineId> string_to_line_id(const std::string &str) { + if (str == "ping.response") { + return LineId::PingResponse; + } + + else if (str == "msg.owner") { + return LineId::MsgOwner; + } + + else if (str == "argument.subcommand") { + return LineId::ArgumentSubcommand; + } else if (str == "argument.message") { + return LineId::ArgumentMessage; + } else if (str == "argument.interval") { + return LineId::ArgumentInterval; + } else if (str == "argument.name") { + return LineId::ArgumentName; + } else if (str == "argument.target") { + return LineId::ArgumentTarget; + } else if (str == "argument.value") { + return LineId::ArgumentValue; + } else if (str == "argument.amount") { + return LineId::ArgumentAmount; + } + + else if (str == "error.template") { + return LineId::ErrorTemplate; + } else if (str == "error.not_enough_arguments") { + return LineId::ErrorNotEnoughArguments; + } else if (str == "error.incorrect_argument") { + return LineId::ErrorIncorrectArgument; + } else if (str == "error.incompatible_name") { + return LineId::ErrorIncompatibleName; + } else if (str == "error.namesake_creation") { + return LineId::ErrorNamesakeCreation; + } else if (str == "error.not_found") { + return LineId::ErrorNotFound; + } else if (str == "error.something_went_wrong") { + return LineId::ErrorSomethingWentWrong; + } else if (str == "error.insufficient_rights") { + return LineId::ErrorInsufficientRights; + } else if (str == "error.illegal_command") { + return LineId::ErrorIllegalCommand; + } + + else if (str == "event.on") { + return LineId::EventOn; + } else if (str == "event.off") { + return LineId::EventOff; + } + + else if (str == "notify.sub") { + return LineId::NotifySub; + } else if (str == "notify.unsub") { + return LineId::NotifyUnsub; + } + + else if (str == "join.response") { + return LineId::JoinResponse; + } else if (str == "join.response_in_chat") { + return LineId::JoinResponseInChat; + } else if (str == "join.already_in") { + return LineId::JoinAlreadyIn; + } else if (str == "join.rejoined") { + return LineId::JoinRejoined; + } else if (str == "join.from_other_chat") { + return LineId::JoinFromOtherChat; + } else if (str == "join.not_allowed") { + return LineId::JoinNotAllowed; + } + + else if (str == "custom_command.new") { + return LineId::CustomcommandNew; + } else if (str == "custom_command.delete") { + return LineId::CustomcommandDelete; + } + + else if (str == "timer.new") { + return LineId::TimerNew; + } else if (str == "timer.delete") { + return LineId::TimerDelete; + } + + else if (str == "help.response") { + return LineId::HelpResponse; + } + + else { + return std::nullopt; + } + } + } +} diff --git a/bot/src/localization/line_id.hpp b/bot/src/localization/line_id.hpp new file mode 100644 index 0000000..41ceec6 --- /dev/null +++ b/bot/src/localization/line_id.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include <optional> +#include <string> + +namespace bot { + namespace loc { + enum LineId { + MsgOwner, + + ArgumentSubcommand, + ArgumentMessage, + ArgumentInterval, + ArgumentName, + ArgumentTarget, + ArgumentValue, + ArgumentAmount, + + ErrorTemplate, + ErrorNotEnoughArguments, + ErrorIncorrectArgument, + ErrorIncompatibleName, + ErrorNamesakeCreation, + ErrorNotFound, + ErrorSomethingWentWrong, + ErrorExternalAPIError, + ErrorInsufficientRights, + ErrorIllegalCommand, + + PingResponse, + + EventOn, + EventOff, + + NotifySub, + NotifyUnsub, + + JoinResponse, + JoinResponseInChat, + JoinAlreadyIn, + JoinRejoined, + JoinFromOtherChat, + JoinNotAllowed, + + CustomcommandNew, + CustomcommandDelete, + + TimerNew, + TimerDelete, + + HelpResponse + }; + + std::optional<LineId> string_to_line_id(const std::string &str); + } +} diff --git a/bot/src/localization/localization.cpp b/bot/src/localization/localization.cpp new file mode 100644 index 0000000..2742602 --- /dev/null +++ b/bot/src/localization/localization.cpp @@ -0,0 +1,132 @@ +#include "localization.hpp" + +#include <algorithm> +#include <filesystem> +#include <fstream> +#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}}; + + 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; + } + } +} diff --git a/bot/src/localization/localization.hpp b/bot/src/localization/localization.hpp new file mode 100644 index 0000000..4626c68 --- /dev/null +++ b/bot/src/localization/localization.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include <optional> +#include <string> +#include <unordered_map> +#include <vector> + +#include "../commands/request.hpp" +#include "line_id.hpp" + +namespace bot { + namespace loc { + class Localization { + public: + Localization(const std::string &folder_path); + ~Localization() = default; + + std::optional<std::string> get_localized_line( + const std::string &locale_id, const LineId &line_id) const; + + std::optional<std::string> get_formatted_line( + const std::string &locale_id, const LineId &line_id, + const std::vector<std::string> &args) const; + + std::optional<std::string> get_formatted_line( + const command::Request &request, const LineId &line_id, + const std::vector<std::string> &args) const; + + private: + std::unordered_map<LineId, std::string> load_from_file( + const std::string &file_path); + std::unordered_map<std::string, std::unordered_map<LineId, std::string>> + localizations; + }; + } + +} |
