summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgarder500 <jeremy27.clara22@gmail.com>2025-05-01 17:25:14 +0200
committergarder500 <jeremy27.clara22@gmail.com>2025-05-01 17:25:14 +0200
commit1aa7ec4539170739986bcbb4d542f5432f98bea5 (patch)
treed66322220c7a585a22be4261c96633ac6e2b4f8c
parent6c4647611317e27e38d70bfbc55eb180bc377809 (diff)
Ajout de la gestion des traductions et des messages d'erreur en plusieurs langues, suppression des fichiers de traduction obsolètes.
-rw-r--r--bot/CMakeLists.txt1
-rw-r--r--bot/include/utils.hpp22
-rw-r--r--bot/locales/.gitignore0
-rw-r--r--bot/locales/en/translation.mobin660 -> 0 bytes
-rw-r--r--bot/locales/en/translation.po25
-rw-r--r--bot/src/actions/delete.cpp31
-rw-r--r--bot/src/handle_actions.cpp4
-rw-r--r--bot/src/main.cpp2
-rw-r--r--bot/src/utils.cpp43
9 files changed, 86 insertions, 42 deletions
diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt
index 048faaf..de5d68c 100644
--- a/bot/CMakeLists.txt
+++ b/bot/CMakeLists.txt
@@ -10,6 +10,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
find_package(dpp REQUIRED
HINTS ${DPP_INCLUDE_DIR} # Add path if needed: /nix/store/.../include
)
+find_package(Gettext REQUIRED)
file(GLOB_RECURSE SRC_FILES
src/*.cpp
include/*.hpp
diff --git a/bot/include/utils.hpp b/bot/include/utils.hpp
index 820fd01..801006a 100644
--- a/bot/include/utils.hpp
+++ b/bot/include/utils.hpp
@@ -11,6 +11,9 @@
#include <sstream>
#include <algorithm>
using namespace dpp;
+
+enum class Lang { en, fr };
+
namespace app
{
@@ -81,6 +84,25 @@ namespace app
*/
std::string string_from_json(const nlohmann::json &j);
+ /**
+ * @brief Gets the available locale for a given locale string
+ *
+ * @param locale The locale string to check
+ * @return std::string The available locale or "en" if not found
+ */
+ std::string get_available_locale(std::string locale);
+
+ /**
+ * @brienf translate a string from a locale, optionnal parameters and a default value
+ * @param str The string to translate to found in an Array of translations
+ * @param locale The locale to use for translation
+ * @param array_translations The array of translations
+ * @param args The optional parameters to replace in the string
+ * @return std::string The translated string
+ */
+ std::string translate(const std::string &str, const std::string &locale, const std::map<Lang, std::map<std::string, std::string>> &array_translations, const std::unordered_map<std::string, std::string> &args = {});
+
+
} // namespace dpp
#endif // UTILS_HPP
diff --git a/bot/locales/.gitignore b/bot/locales/.gitignore
deleted file mode 100644
index e69de29..0000000
--- a/bot/locales/.gitignore
+++ /dev/null
diff --git a/bot/locales/en/translation.mo b/bot/locales/en/translation.mo
deleted file mode 100644
index 9b220bd..0000000
--- a/bot/locales/en/translation.mo
+++ /dev/null
Binary files differ
diff --git a/bot/locales/en/translation.po b/bot/locales/en/translation.po
deleted file mode 100644
index ed075aa..0000000
--- a/bot/locales/en/translation.po
+++ /dev/null
@@ -1,25 +0,0 @@
-msgid ""
-msgstr ""
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=UTF-8\n"
-"Content-Transfer-Encoding: 8bit\n"
-"X-Generator: POEditor.com\n"
-"Project-Id-Version: Bot-Creator\n"
-"Language: en\n"
-
-#: (emplacement original dans le code)
-msgid "No messages to delete."
-msgstr "No message to delete."
-
-#:
-msgid "You need to wait a bit before deleting messages."
-msgstr "You need to wait a big before deleting messages."
-
-#:
-msgid "The amount of messages to delete must be between 1 and 100."
-msgstr "The amount of messages to delete must be between 1 and 100."
-
-#:
-msgid "You do not have permission to delete messages in this channel."
-msgstr "You do not have permission to delete messages in this channel."
-
diff --git a/bot/src/actions/delete.cpp b/bot/src/actions/delete.cpp
index 27855cc..22dbc80 100644
--- a/bot/src/actions/delete.cpp
+++ b/bot/src/actions/delete.cpp
@@ -1,9 +1,27 @@
-#include <dpp/dpp.h>
+#include "../../include/utils.hpp"
+
+
+const std::map<Lang, std::map<std::string, std::string>> error_messages_map = {
+ {Lang::en, {
+ {"error_no_messages", "No message to delete."},
+ {"error", "You need to wait a bit before deleting messages."},
+ {"error_amount", "The amount of messages to delete must be between 1 and 100."},
+ {"error_perm_channel", "You do not have permission to delete messages in this channel."}
+ }},
+ {Lang::fr, {
+ {"error_no_messages", "Aucun message à supprimer."},
+ {"error", "Vous devez attendre un peu avant de supprimer des messages."},
+ {"error_amount", "Le nombre de messages à supprimer doit être compris entre 1 et 100."},
+ {"error_perm_channel", "Vous n'avez pas la permission de supprimer des messages dans ce canal."}
+ }}
+};
dpp::task<bool> delete_action(const dpp::slashcommand_t &event, const nlohmann::json &action,
const std::unordered_map<std::string, std::string> &key_values,
dpp::user &user_ptr, dpp::cluster *cluster)
{
+ // setup locale for gettext
+ std::string locale = app::get_available_locale(event.command.locale);
const dpp::channel *channel_ptr = &event.command.get_channel();
const auto &guild_ptr = event.command.get_guild();
@@ -13,13 +31,14 @@ dpp::task<bool> delete_action(const dpp::slashcommand_t &event, const nlohmann::
const auto bot_member_it = guild_ptr.members.find(cluster->me.id);
const auto *bot_member_ptr = (bot_member_it != guild_ptr.members.end()) ? &bot_member_it->second : nullptr;
- const std::unordered_map<std::string, std::string> error_messages = [&action]()
+ const std::unordered_map<std::string, std::string> error_messages = [&action,&locale]()
{
std::unordered_map<std::string, std::string> defaults = {
- {"error_no_messages", "No messages to delete."},
- {"error", "You need to wait a bit before deleting messages."},
- {"error_amount", "The amount of messages to delete must be between 1 and 100."},
- {"error_perm_channel", "You do not have permission to delete messages in this channel."}};
+ {"error_no_messages", app::translate("error_no_messages", locale, error_messages_map)},
+ {"error", app::translate("error", locale, error_messages_map)},
+ {"error_amount", app::translate("error_amount", locale, error_messages_map)},
+ {"error_perm_channel", app::translate("error_perm_channel", locale, error_messages_map)},
+ };
if (action.contains("error_amount"))
defaults["error_amount"] = action["error_amount"];
if (action.contains("error_perm_channel"))
diff --git a/bot/src/handle_actions.cpp b/bot/src/handle_actions.cpp
index e469ec7..0b3f2ff 100644
--- a/bot/src/handle_actions.cpp
+++ b/bot/src/handle_actions.cpp
@@ -1,8 +1,6 @@
-#include <dpp/dpp.h>
#include "../include/actions/delete.hpp"
dpp::task<bool> handle_actions(const dpp::slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values)
{
-
dpp::cluster *cluster = event.owner;
dpp::user user_ptr = event.command.get_issuing_user();
dpp::async thinking = event.co_thinking(false);
@@ -36,4 +34,6 @@ dpp::task<bool> handle_actions(const dpp::slashcommand_t &event, const nlohmann:
}
}
}
+ co_await thinking;
+ co_return true;
}
diff --git a/bot/src/main.cpp b/bot/src/main.cpp
index 42e84bd..8007998 100644
--- a/bot/src/main.cpp
+++ b/bot/src/main.cpp
@@ -1,5 +1,3 @@
-#include <dpp/dpp.h>
-#include <string>
#include "../include/utils.hpp"
#include "../include/http_webhook_server.hpp"
#include "../include/handle_actions.hpp"
diff --git a/bot/src/utils.cpp b/bot/src/utils.cpp
index 349f584..18c43b5 100644
--- a/bot/src/utils.cpp
+++ b/bot/src/utils.cpp
@@ -1,10 +1,4 @@
-#include <dpp/dpp.h>
-#include <dpp/nlohmann/json.hpp>
-#include <map>
-#include <string>
-#include <regex>
-#include <sstream>
-#include <algorithm>
+#include "../include/utils.hpp"
using namespace dpp;
namespace app
@@ -212,4 +206,39 @@ namespace app
}
return str;
}
+
+ std::string get_available_locale(std::string locale)
+ {
+ std::vector<std::string> available_locales = {"en", "fr"};
+ std::string lang = locale.substr(0, 2);
+ std::transform(lang.begin(), lang.end(), lang.begin(), ::tolower);
+ if (std::find(available_locales.begin(), available_locales.end(), lang) != available_locales.end())
+ {
+ return lang;
+ }
+ else
+ {
+ return "en"; // Default to English if not found
+ }
+ }
+
+ std::string translate(const std::string &str, const std::string &locale, const std::map<Lang, std::map<std::string, std::string>> &array_translations, const std::unordered_map<std::string, std::string> &args)
+ {
+ std::string lang = get_available_locale(locale);
+ auto it = array_translations.find(Lang::en);
+ if (it != array_translations.end())
+ {
+ auto it2 = it->second.find(str);
+ if (it2 != it->second.end())
+ {
+ std::string translation = it2->second;
+ for (const auto &arg : args)
+ {
+ translation = update_string(translation, args);
+ }
+ return translation;
+ }
+ }
+ return str; // Return the original string if no translation is found
+ }
}