Skip to content

Commit

Permalink
refactor: refactoring i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Feb 2, 2024
1 parent 85f07dd commit 93734f7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 88 deletions.
7 changes: 1 addition & 6 deletions src/ll/api/i18n/I18nAPI.cpp → src/ll/api/i18n/I18n.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
82 changes: 11 additions & 71 deletions src/ll/api/i18n/I18nAPI.h → src/ll/api/i18n/I18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -19,8 +20,6 @@

// #define LL_I18N_COLLECT_STRINGS

class Player;

namespace ll::i18n {

class I18N {
Expand Down Expand Up @@ -140,61 +139,9 @@ inline void load(std::filesystem::path const& path) {
} catch (...) {}
getInstance() = nullptr;
}
namespace detail {
template <ll::concepts::IsString S, class... Args>
inline auto tr(S const& fmt, Args&&... args)
-> std::conditional_t<sizeof...(args) == 0, std::string_view, std::string> {
auto res = getInstance()->get(fmt);
if constexpr (sizeof...(args) != 0) {
return fmt::format(fmt::runtime(res), std::forward<Args>(args)...);
}
return res;
}
template <ll::concepts::IsString S, class... Args>
[[nodiscard]] inline auto trl(std::string_view localeName, S const& fmt, Args&&... args)
-> std::conditional_t<sizeof...(args) == 0, std::string_view, std::string> {
auto res = getInstance()->get(fmt, localeName);
if constexpr (sizeof...(args) != 0) {
return fmt::format(fmt::runtime(res), std::forward<Args>(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 <class... Args>
[[nodiscard]] inline std::string operator()(Args&&... args) {
return fmt::format(fmt::runtime(detail::tr(view)), std::forward<Args>(args)...);
}
};

struct TranslateFunctorWithLocale : TranslateFunctor {
using TranslateFunctor::TranslateFunctor;

template <class... Args>
[[nodiscard]] inline std::string operator()(std::string_view locale, Args&&... args) {
return fmt::format(fmt::runtime(detail::trl(locale, view)), std::forward<Args>(args)...);
}
template <class... Args>
[[nodiscard]] inline std::string operator()(Player const& player, Args&&... args) {
return fmt::format(
fmt::runtime(detail::trl(detail::getPlayerLocale(player), view)),
std::forward<Args>(args)...
);
}
};

} // namespace ll::i18n

#ifdef LL_I18N_COLLECT_STRINGS
#include "ll/api/base/FixedString.h"
namespace ll::i18n_literals {
namespace detail {
template <FixedString str>
Expand All @@ -205,24 +152,17 @@ struct TrString {
}();
};
} // namespace detail
template <FixedString str>
[[nodiscard]] inline i18n::TranslateFunctor operator""_tr() {
static detail::TrString<str> e{};
return (std::string_view)str;
}
template <FixedString str>
[[nodiscard]] inline i18n::TranslateFunctorWithLocale operator""_trl() {
static detail::TrString<str> 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 <FixedString Fmt>
[[nodiscard]] constexpr auto operator""_tr() {
return [=]<typename... Args>(Args&&... args) {
#ifdef LL_I18N_COLLECT_STRINGS
static detail::TrString<str> e{};
#endif
[[maybe_unused]] static constexpr auto checker = fmt::format_string<Args...>(std::string_view{Fmt});
return fmt::vformat(::ll::i18n::getInstance()->get(Fmt), fmt::make_format_args(args...));
};
}
} // namespace ll::i18n_literals
#endif
2 changes: 1 addition & 1 deletion src/ll/api/utils/WinUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#include "ll/api/i18n/I18nAPI.h"
#include "ll/api/i18n/I18n.h"
#include "ll/api/memory/Memory.h"
#include "ll/api/utils/StringUtils.h"

Expand Down
10 changes: 5 additions & 5 deletions src/ll/core/Config.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/ll/core/CrashLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/ll/core/LeviLamina.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string>

#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"
Expand Down
4 changes: 2 additions & 2 deletions src/ll/core/plugin/PluginRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mc/server/commands/CommandOutput.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 93734f7

Please sign in to comment.