Files
EchoionCore/include/echoion/core/commands/LanguageCommand.hpp
T
faumaray 5c036d91fb making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:45:17 +03:00

112 lines
2.9 KiB
C++

#pragma once
#include "../MusicManager.hpp"
#include "../i18n/Localization.hpp"
#include "Command.hpp"
#include <Database.hpp>
#include <algorithm>
#include <cctype>
#include <format>
#include <string>
namespace Echoion::Core
{
// !language [en|ru] — show or set the caller's reply language. The choice
// is stored per user (keyed by the stable TS3 unique identifier) in
// kv_store so it survives reconnects and restarts. With no argument it
// reports the current language and the available options.
class LanguageCommand : public Command
{
public:
void execute(const CommandContext& ctx) override
{
std::string arg = trimmedLower(ctx.args);
if (arg.empty()) {
ctx.reply(
ctx.trf("Current language: {}. Available: en, ru. Use "
"{}language <en|ru>.",
I18n::languageName(ctx.language), ctx.prefix));
return;
}
const bool isEn =
arg == "en" || arg == "eng" || arg == "english";
const bool isRu = arg == "ru" || arg == "rus" ||
arg == "russian" ||
arg == "\xd1\x80\xd1\x83\xd1\x81\xd1\x81"
"\xd0\xba\xd0\xb8\xd0\xb9";
if (!isEn && !isRu) {
ctx.reply(ctx.tr("Unknown language. Choose 'en' or 'ru'."));
return;
}
const I18n::Language newLang =
isRu ? I18n::Language::Russian : I18n::Language::English;
auto manager = ctx.musicManager.lock();
auto db = ctx.service<Api::Database>();
if (!manager || !db) {
ctx.reply(
ctx.tr("Language settings are unavailable right now."));
return;
}
auto uid = manager->clientUid(ctx.clientId);
if (!uid || uid->empty()) {
ctx.reply(ctx.tr("Couldn't identify you on the server."));
return;
}
auto saved = db->setKv(
"lang:" + *uid, std::string(I18n::languageCode(newLang)));
if (!saved) {
ctx.reply(ctx.tr(
"Couldn't save your language. Please try again."));
return;
}
// Confirm in the newly selected language.
std::string name(I18n::languageName(newLang));
ctx.reply(std::vformat(
I18n::translate("\xe2\x9c\x85 Language set to {}.",
newLang),
std::make_format_args(name)));
}
std::string_view getDescription() const override
{
return "Sets your reply language (English or Russian).";
}
std::string_view getUsage() const override { return "[en|ru]"; }
CommandCategory getCategory() const override
{
return CommandCategory::Utility;
}
std::vector<std::string_view> getAliases() const override
{
return {"lang"};
}
private:
static std::string trimmedLower(std::string_view in)
{
size_t b = in.find_first_not_of(" \t");
if (b == std::string_view::npos) {
return {};
}
size_t e = in.find_last_not_of(" \t");
std::string out(in.substr(b, e - b + 1));
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return std::tolower(c); });
return out;
}
};
} // namespace Echoion::Core