From 93734f75f6bdee82db613087dd43ef215732cf44 Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Fri, 2 Feb 2024 16:03:01 +0800 Subject: [PATCH] refactor: refactoring i18n --- src/ll/api/i18n/{I18nAPI.cpp => I18n.cpp} | 7 +- src/ll/api/i18n/{I18nAPI.h => I18n.h} | 82 +++-------------------- src/ll/api/utils/WinUtils.cpp | 2 +- src/ll/core/Config.cpp | 10 +-- src/ll/core/CrashLogger.cpp | 2 +- src/ll/core/LeviLamina.cpp | 2 +- src/ll/core/plugin/PluginRegistrar.cpp | 4 +- src/mc/server/commands/CommandOutput.h | 2 +- 8 files changed, 23 insertions(+), 88 deletions(-) rename src/ll/api/i18n/{I18nAPI.cpp => I18n.cpp} (96%) rename src/ll/api/i18n/{I18nAPI.h => I18n.h} (60%) diff --git a/src/ll/api/i18n/I18nAPI.cpp b/src/ll/api/i18n/I18n.cpp similarity index 96% rename from src/ll/api/i18n/I18nAPI.cpp rename to src/ll/api/i18n/I18n.cpp index bc4de52cec..d78346d70d 100644 --- a/src/ll/api/i18n/I18nAPI.cpp +++ b/src/ll/api/i18n/I18n.cpp @@ -1,10 +1,8 @@ -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/io/FileUtils.h" #include "ll/api/utils/StringUtils.h" #include "ll/api/utils/WinUtils.h" -#include "mc/world/actor/player/Player.h" - namespace fs = std::filesystem; using ll::file_utils::u8path; @@ -210,7 +208,4 @@ void MultiFileI18N::save(bool nested) { I18N::Type MultiFileI18N::getType() const { return Type::MultiFile; } #pragma endregion -namespace detail { -LLNDAPI std::string getPlayerLocale(Player const& player) { return player.getLocaleName(); } -} // namespace detail } // namespace ll::i18n diff --git a/src/ll/api/i18n/I18nAPI.h b/src/ll/api/i18n/I18n.h similarity index 60% rename from src/ll/api/i18n/I18nAPI.h rename to src/ll/api/i18n/I18n.h index e51866cac4..6c38b0a76c 100644 --- a/src/ll/api/i18n/I18nAPI.h +++ b/src/ll/api/i18n/I18n.h @@ -2,6 +2,7 @@ #include "ll/api/base/Concepts.h" #include "ll/api/base/Containers.h" +#include "ll/api/base/FixedString.h" #include "ll/api/io/FileUtils.h" #include "ll/api/utils/StringUtils.h" @@ -19,8 +20,6 @@ // #define LL_I18N_COLLECT_STRINGS -class Player; - namespace ll::i18n { class I18N { @@ -140,61 +139,9 @@ inline void load(std::filesystem::path const& path) { } catch (...) {} getInstance() = nullptr; } -namespace detail { -template -inline auto tr(S const& fmt, Args&&... args) - -> std::conditional_t { - auto res = getInstance()->get(fmt); - if constexpr (sizeof...(args) != 0) { - return fmt::format(fmt::runtime(res), std::forward(args)...); - } - return res; -} -template -[[nodiscard]] inline auto trl(std::string_view localeName, S const& fmt, Args&&... args) - -> std::conditional_t { - auto res = getInstance()->get(fmt, localeName); - if constexpr (sizeof...(args) != 0) { - return fmt::format(fmt::runtime(res), std::forward(args)...); - } - return res; -} -LLNDAPI std::string getPlayerLocale(Player const&); -} // namespace detail - -struct TranslateFunctor { - std::string_view view; - [[nodiscard]] constexpr TranslateFunctor(std::string_view view) : view(view) {} // NOLINT - [[nodiscard]] inline operator std::string_view() const { return detail::tr(view); } // NOLINT - [[nodiscard]] inline operator fmt::string_view() const { return detail::tr(view); } // NOLINT - [[nodiscard]] explicit operator std::string() const { return std::string{detail::tr(view)}; } - - template - [[nodiscard]] inline std::string operator()(Args&&... args) { - return fmt::format(fmt::runtime(detail::tr(view)), std::forward(args)...); - } -}; - -struct TranslateFunctorWithLocale : TranslateFunctor { - using TranslateFunctor::TranslateFunctor; - - template - [[nodiscard]] inline std::string operator()(std::string_view locale, Args&&... args) { - return fmt::format(fmt::runtime(detail::trl(locale, view)), std::forward(args)...); - } - template - [[nodiscard]] inline std::string operator()(Player const& player, Args&&... args) { - return fmt::format( - fmt::runtime(detail::trl(detail::getPlayerLocale(player), view)), - std::forward(args)... - ); - } -}; - } // namespace ll::i18n #ifdef LL_I18N_COLLECT_STRINGS -#include "ll/api/base/FixedString.h" namespace ll::i18n_literals { namespace detail { template @@ -205,24 +152,17 @@ struct TrString { }(); }; } // namespace detail -template -[[nodiscard]] inline i18n::TranslateFunctor operator""_tr() { - static detail::TrString e{}; - return (std::string_view)str; -} -template -[[nodiscard]] inline i18n::TranslateFunctorWithLocale operator""_trl() { - static detail::TrString e{}; - return (std::string_view)str; -} } // namespace ll::i18n_literals -#else +#endif namespace ll::i18n_literals { -[[nodiscard]] inline i18n::TranslateFunctor operator""_tr(char const* x, size_t len) { - return std::string_view{x, len}; -} -[[nodiscard]] inline i18n::TranslateFunctorWithLocale operator""_trl(char const* x, size_t len) { - return std::string_view{x, len}; +template +[[nodiscard]] constexpr auto operator""_tr() { + return [=](Args&&... args) { +#ifdef LL_I18N_COLLECT_STRINGS + static detail::TrString e{}; +#endif + [[maybe_unused]] static constexpr auto checker = fmt::format_string(std::string_view{Fmt}); + return fmt::vformat(::ll::i18n::getInstance()->get(Fmt), fmt::make_format_args(args...)); + }; } } // namespace ll::i18n_literals -#endif diff --git a/src/ll/api/utils/WinUtils.cpp b/src/ll/api/utils/WinUtils.cpp index fa1e7b27e1..abbe7a152e 100644 --- a/src/ll/api/utils/WinUtils.cpp +++ b/src/ll/api/utils/WinUtils.cpp @@ -2,7 +2,7 @@ #include -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/memory/Memory.h" #include "ll/api/utils/StringUtils.h" diff --git a/src/ll/core/Config.cpp b/src/ll/core/Config.cpp index ab344369a9..e8830a35ce 100644 --- a/src/ll/core/Config.cpp +++ b/src/ll/core/Config.cpp @@ -1,7 +1,7 @@ #include "ll/core/Config.h" #include "ll/api/Config.h" -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/utils/ErrorUtils.h" #include "ll/core/LeviLamina.h" @@ -16,15 +16,15 @@ bool loadLeviConfig() { try { if (!ll::config::loadConfig(globalConfig, leviConfigPath)) { if (ll::config::saveConfig(globalConfig, leviConfigPath)) { - logger.warn("LeviConfig rewrite successfully"_tr); + logger.warn("LeviConfig rewrite successfully"_tr()); } else { - logger.error("LeviConfig rewrite failed"_tr); + logger.error("LeviConfig rewrite failed"_tr()); return false; } } return true; } catch (...) { - logger.error("LeviConfig load failed"_tr); + logger.error("LeviConfig load failed"_tr()); ll::error_utils::printCurrentException(logger); return false; } @@ -39,7 +39,7 @@ bool saveLeviConfig() { ll::error_utils::printCurrentException(logger); } if (!res) { - logger.error("LeviConfig failed to save"_tr); + logger.error("LeviConfig failed to save"_tr()); return false; } return true; diff --git a/src/ll/core/CrashLogger.cpp b/src/ll/core/CrashLogger.cpp index 89e21e3f14..9309aeff21 100644 --- a/src/ll/core/CrashLogger.cpp +++ b/src/ll/core/CrashLogger.cpp @@ -4,7 +4,7 @@ #include "ll/api/Logger.h" #include "ll/api/data/Version.h" -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/service/ServerInfo.h" #include "ll/api/utils/ErrorUtils.h" #include "ll/api/utils/StacktraceUtils.h" diff --git a/src/ll/core/LeviLamina.cpp b/src/ll/core/LeviLamina.cpp index 6dac6251cd..ab598429e6 100644 --- a/src/ll/core/LeviLamina.cpp +++ b/src/ll/core/LeviLamina.cpp @@ -4,7 +4,7 @@ #include #include "ll/api/Logger.h" -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/memory/Hook.h" #include "ll/api/service/Bedrock.h" #include "ll/api/service/PlayerInfo.h" diff --git a/src/ll/core/plugin/PluginRegistrar.cpp b/src/ll/core/plugin/PluginRegistrar.cpp index 08ee648eee..c58192dc61 100644 --- a/src/ll/core/plugin/PluginRegistrar.cpp +++ b/src/ll/core/plugin/PluginRegistrar.cpp @@ -18,7 +18,7 @@ #include "ll/api/base/Concepts.h" // IWYU pragma: keep #include "ll/api/data/DependencyGraph.h" -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "ll/api/io/FileUtils.h" #include "ll/api/memory/Hook.h" #include "ll/api/plugin/Manifest.h" @@ -351,7 +351,7 @@ bool PluginRegistrar::enablePlugin(std::string_view name) { if (ptr->getState() == Plugin::State::Enabled) { continue; } - logger.error("Dependency {} of {} is not enabled"_tr(depName)); + logger.error("Dependency {} of {} is not enabled"_tr(depName, name)); return false; } } diff --git a/src/mc/server/commands/CommandOutput.h b/src/mc/server/commands/CommandOutput.h index c2922190c1..9e36932efb 100644 --- a/src/mc/server/commands/CommandOutput.h +++ b/src/mc/server/commands/CommandOutput.h @@ -1,6 +1,6 @@ #pragma once -#include "ll/api/i18n/I18nAPI.h" +#include "ll/api/i18n/I18n.h" #include "mc/_HeaderOutputPredefine.h" #include "mc/server/commands/CommandOutputMessage.h" #include "mc/server/commands/CommandOutputParameter.h"