From e4ccb795b5fc61d0ea020ab7ec40fd5ded6639bf Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Sun, 19 Jun 2022 23:17:54 +0800 Subject: [PATCH 01/10] Add i18n methods for Player class Add trl --- LiteLoader/Header/I18nAPI.h | 163 +++++++++++++++++++---------- LiteLoader/Header/MC/Player.hpp | 21 +++- LiteLoader/Kernel/MC/PlayerAPI.cpp | 6 +- 3 files changed, 129 insertions(+), 61 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index a55cd82201..f1b0f255e7 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -44,7 +44,7 @@ /** * @brief I18N API class. - * + * */ class I18N { @@ -62,12 +62,11 @@ class I18N { void save(); public: - static const constexpr char* POD_KEY = "_ll_plugin_i18n"; ///< PluginOwnData key /** * @brief Construct a I18N object. - * + * * @param filePath The path to the i18n file(json) * @param defaultLangCode The default language code(if no lang code is specified, it will use this) * @param defaultLangData The default translation data @@ -79,7 +78,7 @@ class I18N { : filePath(filePath) , defaultLangCode(defaultLangCode) , defaultLangData(defaultLangData) { - if (hModule) + if (hModule) curModule = hModule; else curModule = GetCurrentModule(); @@ -89,14 +88,13 @@ class I18N { /** * @brief Get the translation of the specified key. - * + * * @param key The language key * @param langCode The language code like en_US,zh_CN("" => this->defaultLangCode) * @return The translation * @see I18N::defaultLangCode */ LIAPI std::string get(const std::string& key, const std::string& langCode = ""); - }; #ifdef UNICODE @@ -109,56 +107,74 @@ class I18N { namespace Translation { -template using enable_if_t = typename std::enable_if::type; + template + using enable_if_t = typename std::enable_if::type; -///////////////// tr Impl ///////////////// -template ::value), int> = 0> -inline std::string trImpl(HMODULE hPlugin, const S& formatStr, const Args&... args) { - auto& i18n = PluginOwnData::getImpl(hPlugin, I18N::POD_KEY); - - std::string realFormatStr = i18n.get(formatStr); - // realFormatStr = FixCurlyBracket(realFormatStr); - if constexpr (0 == sizeof...(args)) { - // Avoid fmt if only one argument - return realFormatStr; - } else { - return fmt::format(realFormatStr, args...); + ///////////////// tr Impl ///////////////// + template ::value), int> = 0> + inline std::string trImpl(HMODULE hPlugin, const S& formatStr, const Args&... args) { + std::string realFormatStr = formatStr; + if (PluginOwnData::hasImpl(hPlugin, I18N::POD_KEY)) { + auto& i18n = PluginOwnData::getImpl(hPlugin, I18N::POD_KEY); + realFormatStr = i18n.get(formatStr); + } + // realFormatStr = FixCurlyBracket(realFormatStr); + if constexpr (0 == sizeof...(args)) { + // Avoid fmt if only one argument + return realFormatStr; + } else { + return fmt::format(realFormatStr, args...); + } + } + template ::value), int> = 0> + inline std::string trlImpl(HMODULE hPlugin, const std::string& langCode, const S& formatStr, const Args&... args) { + std::string realFormatStr = formatStr; + if (PluginOwnData::hasImpl(hPlugin, I18N::POD_KEY)) { + auto& i18n = PluginOwnData::getImpl(hPlugin, I18N::POD_KEY); + realFormatStr = i18n.get(formatStr); + } + // realFormatStr = FixCurlyBracket(realFormatStr); + if constexpr (0 == sizeof...(args)) { + // Avoid fmt if only one argument + return realFormatStr; + } else { + return fmt::format(realFormatStr, args...); + } } -} -///////////////// trc Impl ///////////////// -template ::value), int> = 0> -[[deprecated("Please use trImpl(...).c_str()")]] -inline const char* trcImpl(HMODULE hPlugin, const S& formatStr, const Args&... args) { - std::string res = trImpl(hPlugin, formatStr, args...); - std::string name = std::string(I18N::POD_KEY) + "_translation_" + fmt::v8::detail::to_string_view(formatStr).data(); - auto& str = PluginOwnData::setImpl(hPlugin, name, res); - return str.c_str(); -} + ///////////////// trc Impl ///////////////// + template ::value), int> = 0> + [[deprecated("Use trImpl(...).c_str() instead")]] inline const char* trcImpl(HMODULE hPlugin, const S& formatStr, + const Args&... args) { + std::string res = trImpl(hPlugin, formatStr, args...); + std::string name = + std::string(I18N::POD_KEY) + "_translation_" + fmt::v8::detail::to_string_view(formatStr).data(); + auto& str = PluginOwnData::setImpl(hPlugin, name, res); + return str.c_str(); + } -[[deprecated]] -LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath); // For compatibility -LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, - const I18N::LangData& defaultLangData); + [[deprecated]] LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath); // For compatibility + LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, + const I18N::LangData& defaultLangData); -/** - * @brief Load i18n from a file. - * - * @param filePath The path to the i18n file(json) - * @param defaultLangCode The default language code(if no lang code is specified, it will use this) - * @param defaultLangData The default translation data - * @return True if load successfully, false otherwise. - */ -inline bool load(const std::string& filePath, const std::string& defaultLangCode = "en_US", - const I18N::LangData& defaultLangData = {}) { - return loadImpl(GetCurrentModule(), filePath, defaultLangCode, defaultLangData); -} + /** + * @brief Load i18n from a file. + * + * @param filePath The path to the i18n file(json) + * @param defaultLangCode The default language code(if no lang code is specified, it will use this) + * @param defaultLangData The default translation data + * @return True if load successfully, false otherwise. + */ + inline bool load(const std::string& filePath, const std::string& defaultLangCode = "en_US", + const I18N::LangData& defaultLangData = {}) { + return loadImpl(GetCurrentModule(), filePath, defaultLangCode, defaultLangData); + } }; // namespace Translation /** * @brief Translate a str. - * + * * @tparam S The string type * @tparam Args ... * @param formatStr The str to translate and format @@ -234,15 +250,56 @@ inline const char* trc(const char* formatStr, const Args&... args) { return trc(std::string(formatStr), args...); } +/** + * @brief Translate a str to the specified language. + * + * @tparam S The string type + * @tparam Args ... + * @param langCode The language code like en_US + * @param formatStr The str to translate and format + * @param args The format arguments + * @return The translated str + * @see fmt::format + * @see https://fmt.dev/latest/index.html + * @par Example + * @code + * trl("zh_CN", "There are {0} days before {1} to come back", 3, "alex"); + * @endcode + */ +template +inline std::string trl(const std::string& langCode, const std::string& formatStr, const Args&... args) { + return Translation::trlImpl(GetCurrentModule(), langCode, formatStr, args...); +} + +/** + * @brief Translate a str to the specified language. + * + * @tparam Args ... + * @param langCode The language code like en_US + * @param formatStr The str to translate and format + * @param args The format arguments + * @return The translated str + * @see fmt::format + * @see https://fmt.dev/latest/index.html + * @par Example + * @code + * trl("zh_CN", "There are {0} days before {1} to come back", 3, "alex"); + * @endcode + */ +template +inline std::string trl(const std::string& langCode, const char* formatStr, const Args&... args) { + return trl(langCode, std::string(formatStr), args...); +} + // For text encoding namespace TextEncoding { -LIAPI Encoding getLocalEncoding(); -LIAPI Encoding detectEncoding(const std::string& text, bool* isReliable = nullptr); + LIAPI Encoding getLocalEncoding(); + LIAPI Encoding detectEncoding(const std::string& text, bool* isReliable = nullptr); -LIAPI std::string fromUnicode(const std::wstring& text, Encoding to = Encoding::UTF8); -LIAPI std::wstring toUnicode(const std::string& text, Encoding from = Encoding::UTF8); -LIAPI std::string toUTF8(const std::string& text); -LIAPI std::string toUTF8(const std::string& text, Encoding from); + LIAPI std::string fromUnicode(const std::wstring& text, Encoding to = Encoding::UTF8); + LIAPI std::wstring toUnicode(const std::string& text, Encoding from = Encoding::UTF8); + LIAPI std::string toUTF8(const std::string& text); + LIAPI std::string toUTF8(const std::string& text, Encoding from); -LIAPI std::string convert(const std::string& text, Encoding from, Encoding to); + LIAPI std::string convert(const std::string& text, Encoding from, Encoding to); } // namespace TextEncoding \ No newline at end of file diff --git a/LiteLoader/Header/MC/Player.hpp b/LiteLoader/Header/MC/Player.hpp index a69d0b3598..bf3ca7fbc7 100644 --- a/LiteLoader/Header/MC/Player.hpp +++ b/LiteLoader/Header/MC/Player.hpp @@ -16,6 +16,7 @@ #include "UserEntityIdentifierComponent.hpp" #include "ScorePacketInfo.hpp" #include "DataItem.hpp" + #include "../I18nAPI.h" #undef BEFORE_EXTRA class Player : public Mob { @@ -50,13 +51,23 @@ class Player : public Mob { LIAPI bool isOperator(); LIAPI bool isOP(); + template + inline std::string trImpl(HMODULE hPlugin, const std::string& format, const Args&... args) { + return Translation::trlImpl(hPlugin, this->getLanguageCode(), format, args...); + } + template + inline std::string tr(const std::string& format, const Args&... args) { + return trImpl(GetCurrentModule(), format, args...); + } - LIAPI bool sendText(string text, TextType type = TextType::RAW); + LIAPI bool sendText(const std::string& text, TextType type = TextType::RAW); + template + inline bool sendText(const std::string& text, const Args&... args) { + return sendText(this->tr(text, args...), ttype); + } template - bool sendFormattedText(string text, const Args&... args) - { - if constexpr (0 == sizeof...(args)) - { + inline bool sendFormattedText(const std::string& text, const Args&... args) { + if constexpr (0 == sizeof...(args)) { // Avoid fmt if only one argument return sendText(text); } diff --git a/LiteLoader/Kernel/MC/PlayerAPI.cpp b/LiteLoader/Kernel/MC/PlayerAPI.cpp index f4ad74918a..481bc5d130 100644 --- a/LiteLoader/Kernel/MC/PlayerAPI.cpp +++ b/LiteLoader/Kernel/MC/PlayerAPI.cpp @@ -161,19 +161,19 @@ string Player::getDeviceTypeName() } } -bool Player::kick(const string& msg) +bool Player::kick(const std::string& msg) { NetworkIdentifier* pNetworkIdentifier = getNetworkIdentifier(); Global->disconnectClient(*pNetworkIdentifier, msg, 0); return true; } -bool Player::sendText(string text, TextType type) +bool Player::sendText(const std::string& text, TextType type) { return sendTextPacket(text, type); } -bool Player::talkAs(const string& msg) +bool Player::talkAs(const std::string& msg) { return sendTextTalkPacket(msg); } From e86c4e7cf9d0b43ae4a951901ce26847d2c84237 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Wed, 22 Jun 2022 22:24:42 +0800 Subject: [PATCH 02/10] Add i18n API for CommandOutput Update comment --- LiteLoader/Header/I18nAPI.h | 71 +++++++++++---------- LiteLoader/Header/MC/CommandOutput.hpp | 85 +++++++++++++++++++++++++- LiteLoader/Header/MC/Player.hpp | 25 ++++++++ LiteLoader/Kernel/I18nAPI.cpp | 4 ++ 4 files changed, 150 insertions(+), 35 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index f1b0f255e7..1d61f2456f 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -89,12 +89,19 @@ class I18N { /** * @brief Get the translation of the specified key. * - * @param key The language key - * @param langCode The language code like en_US,zh_CN("" => this->defaultLangCode) - * @return The translation + * @param key The language key + * @param langCode The language code like en_US,zh_CN("" => this->defaultLangCode) + * @return std::string The translation * @see I18N::defaultLangCode */ LIAPI std::string get(const std::string& key, const std::string& langCode = ""); + + /** + * @brief Get the default language code + * + * @return std::string The code + */ + LIAPI std::string getDefaultLangCode(); }; #ifdef UNICODE @@ -175,11 +182,11 @@ namespace Translation { /** * @brief Translate a str. * - * @tparam S The string type - * @tparam Args ... - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str + * @tparam S The string type + * @tparam Args ... + * @param formatStr The str to translate and format + * @param args The format arguments + * @return std::string The translated str * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example @@ -195,10 +202,10 @@ inline std::string tr(const S& formatStr, const Args&... args) { /** * @brief Translate a str. * - * @tparam Args ... - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str + * @tparam Args ... + * @param formatStr The str to translate and format + * @param args The format arguments + * @return std::string The translated str * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example @@ -214,11 +221,11 @@ inline std::string tr(const char* formatStr, const Args&... args) { /** * @brief Translate a str(c-style str). * - * @tparam S The string type - * @tparam Args ... - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str(c-style str) + * @tparam S The string type + * @tparam Args ... + * @param formatStr The str to translate and format + * @param args The format arguments + * @return const char* The translated str(c-style str) * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example @@ -234,10 +241,10 @@ inline const char* trc(const S& formatStr, const Args&... args) { /** * @brief Translate a str(c-style str). * - * @tparam Args ... - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str(c-style str) + * @tparam Args ... + * @param formatStr The str to translate and format + * @param args The format arguments + * @return const char* The translated str(c-style str) * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example @@ -253,12 +260,12 @@ inline const char* trc(const char* formatStr, const Args&... args) { /** * @brief Translate a str to the specified language. * - * @tparam S The string type - * @tparam Args ... - * @param langCode The language code like en_US - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str + * @tparam S The string type + * @tparam Args ... + * @param langCode The language code like en_US + * @param formatStr The str to translate and format + * @param args The format arguments + * @return std::string The translated str * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example @@ -274,11 +281,11 @@ inline std::string trl(const std::string& langCode, const std::string& formatStr /** * @brief Translate a str to the specified language. * - * @tparam Args ... - * @param langCode The language code like en_US - * @param formatStr The str to translate and format - * @param args The format arguments - * @return The translated str + * @tparam Args ... + * @param langCode The language code like en_US + * @param formatStr The str to translate and format + * @param args The format arguments + * @return std::string The translated str * @see fmt::format * @see https://fmt.dev/latest/index.html * @par Example diff --git a/LiteLoader/Header/MC/CommandOutput.hpp b/LiteLoader/Header/MC/CommandOutput.hpp index f208c737a2..b3f52655bc 100644 --- a/LiteLoader/Header/MC/CommandOutput.hpp +++ b/LiteLoader/Header/MC/CommandOutput.hpp @@ -6,7 +6,12 @@ #define BEFORE_EXTRA // Include Headers or Declare Types Here #include "CommandOutputParameter.hpp" +#include "CommandOrigin.hpp" +#include "ServerPlayer.hpp" +#include "../Utils/PluginOwnData.h" +#include "../I18nAPI.h" +#define POD_COMMANDOUTPUT_LANGCODE "_ll_plugin_cmdoutp_langcode_" + std::to_string((uint64_t)this) #undef BEFORE_EXTRA class CommandOutput { @@ -14,9 +19,83 @@ class CommandOutput { #define AFTER_EXTRA // Add Member There public: - LIAPI void addMessage(std::string str); - LIAPI void success(const string& str); - LIAPI void error(const string& str); + LIAPI void addMessage(std::string str); + LIAPI void success(const string& str); + LIAPI void error(const string& str); + + /** + * @brief Set the output language code of this CommandOutput object. + * + * @param code The language code + */ + inline void setLanguageCode(const std::string& code = "") { + PluginOwnData::set(POD_COMMANDOUTPUT_LANGCODE, code); + } + /** + * @brief Set the output language code of this CommandOutput object(convenience func). + * + * @param ori The command origin object for the CommandOutput object + */ + inline void setLanguageCode(const CommandOrigin& ori) { + std::string code = "en_US"; + if (PluginOwnData::has(I18N::POD_KEY)) { + auto& i18n = PluginOwnData::get(I18N::POD_KEY); + switch ((OriginType)ori.getOriginType()) { + case OriginType::Player: + code = ori.getPlayer()->getLanguageCode(); + break; + default: + code = i18n.getDefaultLangCode(); + break; + } + } + PluginOwnData::set(POD_COMMANDOUTPUT_LANGCODE, code); + } + + /** + * @brief Output a message(I18N, convenience func). + * + * @tparam Args ... + * @param format The str to translate and format + * @param args The format arguments + * @see tr + */ + template + inline void addMessage(const std::string& format, const Args&... args) { + if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) + this->addMessage(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + else this->addMessage(tr(format, args...)); + } + + /** + * @brief Output a success message(I18N, convenience func). + * + * @tparam Args ... + * @param format The str to translate and format + * @param args The format arguments + * @see tr + */ + template + inline void success(const std::string& format, const Args&... args) { + if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) + this->success(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + else this->success(tr(format, args...)); + } + + /** + * @brief Output a error message(I18N, convenience func). + * + * @tparam Args ... + * @param format The str to translate and format + * @param args The format arguments + * @see tr + */ + template + inline void error(const std::string& format, const Args&... args) { + if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) + this->error(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + else this->error(tr(format, args...)); + } #undef AFTER_EXTRA diff --git a/LiteLoader/Header/MC/Player.hpp b/LiteLoader/Header/MC/Player.hpp index bf3ca7fbc7..9b139f27ea 100644 --- a/LiteLoader/Header/MC/Player.hpp +++ b/LiteLoader/Header/MC/Player.hpp @@ -51,16 +51,41 @@ class Player : public Mob { LIAPI bool isOperator(); LIAPI bool isOP(); + /** + * @brief Translate(localize) a text for the player with provided plugin handle. + * + * @param hPlugin The plugin handle + * @param format The str to translate and format + * @param args The format arguments + * @return std::string The translated str + */ template inline std::string trImpl(HMODULE hPlugin, const std::string& format, const Args&... args) { return Translation::trlImpl(hPlugin, this->getLanguageCode(), format, args...); } + + /** + * @brief Translate(localize) a text for the player(convenience func). + * + * @param format The str to translate and format + * @param args The format arguments + * @return std::string The translated str + */ template inline std::string tr(const std::string& format, const Args&... args) { return trImpl(GetCurrentModule(), format, args...); } LIAPI bool sendText(const std::string& text, TextType type = TextType::RAW); + /** + * @brief Translate(localize) and send a text to the player(convenience func). + * + * @tparam ttype The text type(default RAW) + * @tparam Args ... + * @param text The str to translate and format + * @param args The format arguments + * @return bool Success or not + */ template inline bool sendText(const std::string& text, const Args&... args) { return sendText(this->tr(text, args...), ttype); diff --git a/LiteLoader/Kernel/I18nAPI.cpp b/LiteLoader/Kernel/I18nAPI.cpp index 2f1c86487b..23f30a7a57 100644 --- a/LiteLoader/Kernel/I18nAPI.cpp +++ b/LiteLoader/Kernel/I18nAPI.cpp @@ -68,6 +68,10 @@ std::string I18N::get(const std::string& key, const std::string& langCode) { return key; } +std::string I18N::getDefaultLangCode() { + return this->defaultLangCode; +} + ///////////////////////////// Encoding-CodePage Map ///////////////////////////// #undef UNICODE namespace TextEncoding { From 511b8da01578476d39912e4d961272ecdcc4b3d8 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Thu, 23 Jun 2022 19:59:06 +0800 Subject: [PATCH 03/10] Fix build --- LiteLoader/Header/MC/CommandOutput.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/LiteLoader/Header/MC/CommandOutput.hpp b/LiteLoader/Header/MC/CommandOutput.hpp index b3f52655bc..3494f42910 100644 --- a/LiteLoader/Header/MC/CommandOutput.hpp +++ b/LiteLoader/Header/MC/CommandOutput.hpp @@ -7,11 +7,12 @@ // Include Headers or Declare Types Here #include "CommandOutputParameter.hpp" #include "CommandOrigin.hpp" +#include "Command.hpp" #include "ServerPlayer.hpp" #include "../Utils/PluginOwnData.h" #include "../I18nAPI.h" -#define POD_COMMANDOUTPUT_LANGCODE "_ll_plugin_cmdoutp_langcode_" + std::to_string((uint64_t)this) +#define POD_COMMANDOUTPUT_LANGCODE ("_ll_plugin_cmdoutp_langcode_" + std::to_string((uint64_t)this)) #undef BEFORE_EXTRA class CommandOutput { @@ -63,7 +64,7 @@ class CommandOutput { template inline void addMessage(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) - this->addMessage(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + this->addMessage(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->addMessage(tr(format, args...)); } @@ -78,7 +79,7 @@ class CommandOutput { template inline void success(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) - this->success(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + this->success(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->success(tr(format, args...)); } @@ -93,7 +94,7 @@ class CommandOutput { template inline void error(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) - this->error(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...); + this->error(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->error(tr(format, args...)); } From 92788f9e57401d3ccf1f3412c6c58a6821aae3f7 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Thu, 23 Jun 2022 20:40:27 +0800 Subject: [PATCH 04/10] Add Translation::getI18N Make I18N::defaultLangCode public --- LiteLoader/Header/I18nAPI.h | 26 ++++++++++++++++++-------- LiteLoader/Kernel/I18nAPI.cpp | 4 ---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index 1d61f2456f..2c623ef6be 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -54,7 +54,6 @@ class I18N { private: HMODULE curModule; std::string filePath; - std::string defaultLangCode = "en_US"; LangData langData; LangData defaultLangData; @@ -62,7 +61,8 @@ class I18N { void save(); public: - static const constexpr char* POD_KEY = "_ll_plugin_i18n"; ///< PluginOwnData key + + std::string defaultLangCode = "en_US"; /** * @brief Construct a I18N object. @@ -96,12 +96,8 @@ class I18N { */ LIAPI std::string get(const std::string& key, const std::string& langCode = ""); - /** - * @brief Get the default language code - * - * @return std::string The code - */ - LIAPI std::string getDefaultLangCode(); + static const constexpr char* POD_KEY = "_ll_plugin_i18n"; ///< PluginOwnData key + }; #ifdef UNICODE @@ -177,6 +173,20 @@ namespace Translation { return loadImpl(GetCurrentModule(), filePath, defaultLangCode, defaultLangData); } + /** + * @brief Get the I18N object of a certain plugin. + * + * @param hPlugin The plugin handle(nullptr -> GetCurrentModule()) + * @return std::optional The I18N object + */ + inline std::optional getI18N(HMODULE hPlugin = nullptr) { + auto handle = (hPlugin == nullptr ? GetCurrentModule() : hPlugin); + if (handle && PluginOwnData::hasImpl(handle, I18N::POD_KEY)) { + return PluginOwnData::getImpl(handle, I18N::POD_KEY); + } + return std::optional(); + } + }; // namespace Translation /** diff --git a/LiteLoader/Kernel/I18nAPI.cpp b/LiteLoader/Kernel/I18nAPI.cpp index 23f30a7a57..2f1c86487b 100644 --- a/LiteLoader/Kernel/I18nAPI.cpp +++ b/LiteLoader/Kernel/I18nAPI.cpp @@ -68,10 +68,6 @@ std::string I18N::get(const std::string& key, const std::string& langCode) { return key; } -std::string I18N::getDefaultLangCode() { - return this->defaultLangCode; -} - ///////////////////////////// Encoding-CodePage Map ///////////////////////////// #undef UNICODE namespace TextEncoding { From da4932ea1c883e1d22b739e5355d29838b6518ca Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Fri, 24 Jun 2022 20:40:58 +0800 Subject: [PATCH 05/10] Improve Translation::loadImpl --- LiteLoader/Header/I18nAPI.h | 12 ++++++++---- LiteLoader/Kernel/I18nAPI.cpp | 11 ++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index 2c623ef6be..a08595fd75 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -85,6 +85,10 @@ class I18N { load(filePath); PluginOwnData::setImpl(curModule, POD_KEY, *this); } + /// Copy constructor + I18N(const I18N& other) { + *this = other; + } /** * @brief Get the translation of the specified key. @@ -157,8 +161,8 @@ namespace Translation { } [[deprecated]] LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath); // For compatibility - LIAPI bool loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, - const I18N::LangData& defaultLangData); + LIAPI I18N* loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, + const I18N::LangData& defaultLangData); /** * @brief Load i18n from a file. @@ -166,9 +170,9 @@ namespace Translation { * @param filePath The path to the i18n file(json) * @param defaultLangCode The default language code(if no lang code is specified, it will use this) * @param defaultLangData The default translation data - * @return True if load successfully, false otherwise. + * @return I18N* The pointer to the I18N object in PluginOwnData, null if failed */ - inline bool load(const std::string& filePath, const std::string& defaultLangCode = "en_US", + inline I18N* load(const std::string& filePath, const std::string& defaultLangCode = "en_US", const I18N::LangData& defaultLangData = {}) { return loadImpl(GetCurrentModule(), filePath, defaultLangCode, defaultLangData); } diff --git a/LiteLoader/Kernel/I18nAPI.cpp b/LiteLoader/Kernel/I18nAPI.cpp index 2f1c86487b..e7ddd3a926 100644 --- a/LiteLoader/Kernel/I18nAPI.cpp +++ b/LiteLoader/Kernel/I18nAPI.cpp @@ -154,20 +154,21 @@ namespace Translation { return loadImpl(hPlugin, filePath, "en_US", {}); } - bool loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, + I18N* loadImpl(HMODULE hPlugin, const std::string& filePath, const std::string& defaultLangCode, const I18N::LangData& defaultLangData) { try { I18N i18n(filePath, defaultLangCode, defaultLangData, hPlugin); + return &PluginOwnData::getImpl(hPlugin, I18N::POD_KEY); } catch (const std::exception& e) { logger.error("Fail to load translation file <{}> !", filePath); logger.error("{}", TextEncoding::toUTF8(e.what())); - return false; + return nullptr; } catch (...) { logger.error("Fail to load translation file <{}> !", filePath); - return false; + return nullptr; } - return true; - } + return nullptr; + } }; // namespace Translation namespace TextEncoding { From 6f55317776e7daf74478f3a9e55e9c2d778ea690 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Sun, 26 Jun 2022 18:30:05 +0800 Subject: [PATCH 06/10] Update CommandOutput --- LiteLoader/Header/MC/CommandOutput.hpp | 6 +----- LiteLoader/Kernel/Command/CommandOutputAPI.cpp | 16 +--------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/LiteLoader/Header/MC/CommandOutput.hpp b/LiteLoader/Header/MC/CommandOutput.hpp index 3494f42910..d0457766df 100644 --- a/LiteLoader/Header/MC/CommandOutput.hpp +++ b/LiteLoader/Header/MC/CommandOutput.hpp @@ -20,9 +20,6 @@ class CommandOutput { #define AFTER_EXTRA // Add Member There public: - LIAPI void addMessage(std::string str); - LIAPI void success(const string& str); - LIAPI void error(const string& str); /** * @brief Set the output language code of this CommandOutput object. @@ -45,8 +42,7 @@ class CommandOutput { case OriginType::Player: code = ori.getPlayer()->getLanguageCode(); break; - default: - code = i18n.getDefaultLangCode(); + default: code = i18n.defaultLangCode; break; } } diff --git a/LiteLoader/Kernel/Command/CommandOutputAPI.cpp b/LiteLoader/Kernel/Command/CommandOutputAPI.cpp index 2b7bafff15..0f0fe2962a 100644 --- a/LiteLoader/Kernel/Command/CommandOutputAPI.cpp +++ b/LiteLoader/Kernel/Command/CommandOutputAPI.cpp @@ -1,15 +1 @@ -#include -#include -using namespace std; - -void CommandOutput::addMessage(std::string str) { - addMessage(str, {}, (CommandOutputMessageType)0); -} - -void CommandOutput::success(const string& str) { - return success(str, {}); -} - -void CommandOutput::error(const string& str) { - return error(str, {}); -} \ No newline at end of file +// Nothing, reserved \ No newline at end of file From 76013302a08261367a01cb6938700bd644e1e492 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Sun, 26 Jun 2022 19:30:28 +0800 Subject: [PATCH 07/10] Fix build --- LiteLoader/Header/I18nAPI.h | 6 ++--- LiteLoader/Header/MC/CommandOutput.hpp | 25 ++++++++++++++++--- .../Kernel/Command/CommandOutputAPI.cpp | 14 ++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index a08595fd75..f47273b07e 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -288,7 +288,7 @@ inline const char* trc(const char* formatStr, const Args&... args) { * @endcode */ template -inline std::string trl(const std::string& langCode, const std::string& formatStr, const Args&... args) { +inline std::string trl(const std::string& langCode, const S& formatStr, const Args&... args) { return Translation::trlImpl(GetCurrentModule(), langCode, formatStr, args...); } @@ -297,7 +297,7 @@ inline std::string trl(const std::string& langCode, const std::string& formatStr * * @tparam Args ... * @param langCode The language code like en_US - * @param formatStr The str to translate and format + * @param formatStr The str to translate and format(c-style) * @param args The format arguments * @return std::string The translated str * @see fmt::format @@ -307,7 +307,7 @@ inline std::string trl(const std::string& langCode, const std::string& formatStr * trl("zh_CN", "There are {0} days before {1} to come back", 3, "alex"); * @endcode */ -template +template inline std::string trl(const std::string& langCode, const char* formatStr, const Args&... args) { return trl(langCode, std::string(formatStr), args...); } diff --git a/LiteLoader/Header/MC/CommandOutput.hpp b/LiteLoader/Header/MC/CommandOutput.hpp index d0457766df..a0f0151c8f 100644 --- a/LiteLoader/Header/MC/CommandOutput.hpp +++ b/LiteLoader/Header/MC/CommandOutput.hpp @@ -20,6 +20,25 @@ class CommandOutput { #define AFTER_EXTRA // Add Member There public: + + /** + * @brief Output a message(without I18N). + * + * @param str The message + */ + LIAPI void addMessage(const std::string& str); + /** + * @brief Output a success message(without I18N). + * + * @param str The message + */ + LIAPI void success(const string& str); + /** + * @brief Output a success message(without I18N). + * + * @param str The message + */ + LIAPI void error(const string& str); /** * @brief Set the output language code of this CommandOutput object. @@ -58,7 +77,7 @@ class CommandOutput { * @see tr */ template - inline void addMessage(const std::string& format, const Args&... args) { + inline void trAddMessage(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) this->addMessage(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->addMessage(tr(format, args...)); @@ -73,7 +92,7 @@ class CommandOutput { * @see tr */ template - inline void success(const std::string& format, const Args&... args) { + inline void trSuccess(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) this->success(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->success(tr(format, args...)); @@ -88,7 +107,7 @@ class CommandOutput { * @see tr */ template - inline void error(const std::string& format, const Args&... args) { + inline void trError(const std::string& format, const Args&... args) { if (PluginOwnData::has(POD_COMMANDOUTPUT_LANGCODE)) this->error(trl(PluginOwnData::get(POD_COMMANDOUTPUT_LANGCODE), format, args...)); else this->error(tr(format, args...)); diff --git a/LiteLoader/Kernel/Command/CommandOutputAPI.cpp b/LiteLoader/Kernel/Command/CommandOutputAPI.cpp index 0f0fe2962a..020e1a014d 100644 --- a/LiteLoader/Kernel/Command/CommandOutputAPI.cpp +++ b/LiteLoader/Kernel/Command/CommandOutputAPI.cpp @@ -1 +1,13 @@ -// Nothing, reserved \ No newline at end of file +#include + +void CommandOutput::addMessage(const std::string& str) { + this->addMessage(str, {}, (CommandOutputMessageType)0); +} + +void CommandOutput::success(const std::string& str) { + this->success(str, {}); +} + +void CommandOutput::error(const std::string& str) { + this->error(str, {}); +} \ No newline at end of file From 0c95e3f1eb09850654c00f6024dcfe2b6f083cec Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Tue, 5 Jul 2022 18:48:22 +0800 Subject: [PATCH 08/10] Fix trlImpl bug --- LiteLoader/Header/I18nAPI.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index f47273b07e..78c6ff78d2 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -138,7 +138,7 @@ namespace Translation { std::string realFormatStr = formatStr; if (PluginOwnData::hasImpl(hPlugin, I18N::POD_KEY)) { auto& i18n = PluginOwnData::getImpl(hPlugin, I18N::POD_KEY); - realFormatStr = i18n.get(formatStr); + realFormatStr = i18n.get(formatStr, langCode); } // realFormatStr = FixCurlyBracket(realFormatStr); if constexpr (0 == sizeof...(args)) { @@ -323,4 +323,4 @@ namespace TextEncoding { LIAPI std::string toUTF8(const std::string& text, Encoding from); LIAPI std::string convert(const std::string& text, Encoding from, Encoding to); -} // namespace TextEncoding \ No newline at end of file +} // namespace TextEncoding From 938eabe33c379a61376f1b2208e5a5d2f42decfc Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Fri, 8 Jul 2022 20:16:51 +0800 Subject: [PATCH 09/10] Remove language packs Add I18N support for Logger --- LiteLoader/Header/I18nAPI.h | 13 +-- LiteLoader/Header/LoggerAPI.h | 9 +++ LiteLoader/Kernel/I18nAPI.cpp | 2 + LiteLoader/LiteLoader.vcxproj | 1 + LiteLoader/LiteLoader.vcxproj.filters | 3 + LiteLoader/Main/Config.h | 2 +- LiteLoader/Main/DefaultLangData.h | 80 +++++++++++++++++++ LiteLoader/Main/LiteLoader.cpp | 18 ++--- RELEASE/plugins/LiteLoader/LangPack/en.json | 23 ------ .../plugins/LiteLoader/LangPack/zh_CN.json | 23 ------ .../plugins/LiteLoader/LangPack/zh_TW.json | 23 ------ 11 files changed, 112 insertions(+), 85 deletions(-) create mode 100644 LiteLoader/Main/DefaultLangData.h delete mode 100644 RELEASE/plugins/LiteLoader/LangPack/en.json delete mode 100644 RELEASE/plugins/LiteLoader/LangPack/zh_CN.json delete mode 100644 RELEASE/plugins/LiteLoader/LangPack/zh_TW.json diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index 78c6ff78d2..04f5de9076 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -4,16 +4,18 @@ // // [Usage - Translation] // -// Translation::load("plugins/xxx/lang/zh_CN.json"); +// Translation::load("plugins/xxx/lang.json"); // ... // tr("There are {0} days before {1} to come back", 3, "alex"); // return translated string [std::string] // trc("There are {0} days before {1} to come back", 3, "alex"); // return translated string [const char*] // -// ** In Translation File: plugins/xxx/lang/zh_CN.json +// ** In Translation File: plugins/xxx/lang.json // { -// "There are {0} days before {1} to come back": "在{1}回来前还剩{0}天", -// "...": "...", -// "...": "..." +// "zh_CN": { +// "There are {0} days before {1} to come back": "在{1}回来前还剩{0}天", +// "...": "...", +// "...": "..." +// } // } // // @@ -33,7 +35,6 @@ #include "Global.h" #include "LLAPI.h" -#include "LoggerAPI.h" #include "Utils/FileHelper.h" #include "Utils/PluginOwnData.h" #include "third-party/Nlohmann/json.hpp" diff --git a/LiteLoader/Header/LoggerAPI.h b/LiteLoader/Header/LoggerAPI.h index c0d797cce8..d0778d2b03 100644 --- a/LiteLoader/Header/LoggerAPI.h +++ b/LiteLoader/Header/LoggerAPI.h @@ -40,6 +40,7 @@ #include "Utils/FileHelper.h" #include "Utils/PluginOwnData.h" #include "Utils/StringHelper.h" +#include "I18nAPI.h" #include #include #include @@ -119,6 +120,10 @@ class Logger { template ::value), int> = 0> void operator()(const S& formatStr, const Args&... args) { + if (PluginOwnData::has(I18N::POD_KEY)) { + *this << tr(formatStr, args...) << endl; + return; + } if constexpr (0 == sizeof...(args)) { // Avoid fmt if only one argument @@ -134,6 +139,10 @@ class Logger { template void operator()(const char* formatStr, const Args&... args) { + if (PluginOwnData::has(I18N::POD_KEY)) { + *this << tr(formatStr, args...) << endl; + return; + } if constexpr (0 == sizeof...(args)) { // Avoid fmt if only one argument diff --git a/LiteLoader/Kernel/I18nAPI.cpp b/LiteLoader/Kernel/I18nAPI.cpp index e7ddd3a926..a7fd2847dd 100644 --- a/LiteLoader/Kernel/I18nAPI.cpp +++ b/LiteLoader/Kernel/I18nAPI.cpp @@ -49,6 +49,7 @@ std::string I18N::get(const std::string& key, const std::string& langCode) { if (lang.count(key)) return lang[key]; // Search for the similar language in langData for (auto& [lc, ld] : langData) { + if (lc.length() < 2) continue; if (lc.substr(0, 2) == langType) { if (ld.count(key)) { return ld[key]; } } @@ -61,6 +62,7 @@ std::string I18N::get(const std::string& key, const std::string& langCode) { // Search for the similar language for (auto& [lc, ld] : defaultLangData) { if (lc.substr(0, 2) == langType) { + if (lc.length() < 2) continue; if (ld.count(key)) { return ld[key]; } } } diff --git a/LiteLoader/LiteLoader.vcxproj b/LiteLoader/LiteLoader.vcxproj index 4e771dffdb..6fc0a1aa7c 100644 --- a/LiteLoader/LiteLoader.vcxproj +++ b/LiteLoader/LiteLoader.vcxproj @@ -344,6 +344,7 @@ LibraryBuilder.exe + diff --git a/LiteLoader/LiteLoader.vcxproj.filters b/LiteLoader/LiteLoader.vcxproj.filters index 7dc170c2f4..f6f3804f56 100644 --- a/LiteLoader/LiteLoader.vcxproj.filters +++ b/LiteLoader/LiteLoader.vcxproj.filters @@ -812,6 +812,9 @@ Header\MC + + Main + diff --git a/LiteLoader/Main/Config.h b/LiteLoader/Main/Config.h index ce1f4676a9..77e2e8294f 100644 --- a/LiteLoader/Main/Config.h +++ b/LiteLoader/Main/Config.h @@ -33,7 +33,7 @@ struct LLConfig bool debugMode = false; bool colorLog = true; int logLevel = 4; - std::string language = "en"; + std::string language = "en_US"; bool enableScriptEngine = true; bool alwaysLaunchScriptEngine = false; diff --git a/LiteLoader/Main/DefaultLangData.h b/LiteLoader/Main/DefaultLangData.h new file mode 100644 index 0000000000..09fc999d36 --- /dev/null +++ b/LiteLoader/Main/DefaultLangData.h @@ -0,0 +1,80 @@ +#pragma once +#include + +static const I18N::LangData defaultLangData = { + {"en", { + {"init.loadConfig.fail", "Loading ScriptEngine config failed"}, + {"init.fileMapping.fail", "Failed to load global shared data"}, + {"init.mapFile.fail", "Global shared data mapping failed"}, + {"init.llMoney.noFound", "LLMoney.dll not found, ScriptEngine Economy System won't work"}, + {"init.idFile.fail", "Failed to initialize unique identification file! Code: "}, + {"api.parseJson.fail", "Json parse error"}, + {"remoteCall.createMessageThread.fail", "Creating Message Thread failed"}, + {"remoteCall.createEventObject.fail", "Creating Event Object failed"}, + {"remoteCall.postMessage.fail", "Posting remote call information failed"}, + {"remoteCall.timeout.fail", "Remote call timeout, call failed"}, + {"remoteCall.waitFail.fail", "Remote call waiting failed, call failed"}, + {"remoteCall.postMessageReturn.fail", "Remote call post return value failed"}, + {"remoteCall.inRemoteEngine.fail", "Remote call error in remote engine"}, + {"base.getDimName.0", "Overworld"}, + {"base.getDimName.1", "Nether"}, + {"base.getDimName.2", "End"}, + {"base.getDimName.unknown", "Other dimension"}, + {"llseapi.require.success", " - Plugin require loaded successfully. Loaded: "}, + {"llseapi.require.fail", " - Plugin require load failed"}, + {"llseapi.require.download.success", " - Successfully downloaded require! Path: "}, + {"llseapi.require.network.fail", " - Download plugin require failed! Code: "}, + }}, + {"zh_CN", { + {"init.loadConfig.fail", "LLSE配置文件加载失败!"}, + {"init.fileMapping.fail", "全局共享数据加载失败!"}, + {"init.mapFile.fail", "全局共享数据映射失败!"}, + {"init.llMoney.noFound", "LLMoney.dll 未找到!LLSE经济系统将不会工作"}, + {"init.idFile.fail", "初始化唯一识别文件失败!错误码:"}, + {"api.parseJson.fail", "Json解析发生错误!"}, + {"remoteCall.createMessageThread.fail", "创建消息循环失败!"}, + {"remoteCall.createEventObject.fail", "创建事件对象失败!"}, + {"remoteCall.postMessage.fail", "递送远程调用信息失败!"}, + {"remoteCall.timeout.fail", "远程调用等待超时!调用失败"}, + {"remoteCall.waitFail.fail", "远程调用等待失败!调用失败"}, + {"remoteCall.postMessageReturn.fail", "远程调用传递返回值失败!"}, + {"remoteCall.inRemoteEngine.fail", "在远程引擎中发生错误!"}, + {"base.getDimName.0", "主世界"}, + {"base.getDimName.1", "下界"}, + {"base.getDimName.2", "末地"}, + {"base.getDimName.unknown", "其他维度"}, + {"llseapi.require.success", " - 插件依赖包加载成功。已加载:"}, + {"llseapi.require.fail", " - 插件依赖包加载失败!"}, + {"llseapi.require.download.success", " - 插件依赖包拉取成功!已安装到路径:"}, + {"llseapi.require.network.fail", " - 插件依赖包拉取网络请求失败!错误码:"}, + {"Detected the existence of another BDS process with the same path!", "检测到另一个相同的BDS进程存在!"}, + {"This may cause the network port and the level to be occupied", "这可能会导致存档和端口占用"}, + {"Do you want to terminate the process with PID {}? (y=Yes, n=No)", "您是否要强制终止该PID为 {} 的进程? (y=是, n=否)"}, + }}, + {"zh_TW", { + {"init.loadConfig.fail", "加載ScriptEngine配置文件失敗!"}, + {"init.fileMapping.fail", "加載全局共享數據失敗!"}, + {"init.mapFile.fail", "全局共享數據映射失敗!"}, + {"init.llMoney.noFound", "LLMoney.dll 未找到!ScriptEngine經濟系統將無法工作"}, + {"init.idFile.fail", "初始化唯一標識文件失敗! 錯誤代碼::"}, + {"api.parseJson.fail", "解析Json時發生錯誤!"}, + {"remoteCall.createMessageThread.fail", "創建消息線程失敗"}, + {"remoteCall.createEventObject.fail", "創建事件線程失敗"}, + {"remoteCall.postMessage.fail", "發送遠程調用信息失敗!"}, + {"remoteCall.timeout.fail", "遠程調用超時,調用失敗!"}, + {"remoteCall.waitFail.fail", "遠程調用等待失敗,調用失敗!"}, + {"remoteCall.postMessageReturn.fail", "遠程調用後返回值失敗!"}, + {"remoteCall.inRemoteEngine.fail", "在遠程引擎中的遠程調用錯誤!"}, + {"base.getDimName.0", "主世界"}, + {"base.getDimName.1", "地獄"}, + {"base.getDimName.2", "終界"}, + {"base.getDimName.unknown", "其他維度(世界)"}, + {"llseapi.require.success", " - 插件所需拓展包加載成功。已加載:"}, + {"llseapi.require.fail", " - 插件所需拓展包加載失敗!"}, + {"llseapi.require.download.success", " - 插件所需拓展包下載成功!已安裝到至:"}, + {"llseapi.require.network.fail", " - 插件所需拓展包下載請求失敗!錯誤代碼:"}, + {"Detected the existence of another BDS process with the same path!", "檢測到另一個相同的BDS進程存在!"}, + {"This may cause the network port and the level to be occupied", "這可能會導致存檔和端口占用"}, + {"Do you want to terminate the process with PID {}? (y=Yes, n=No)", "您是否要強製終止該PID為 {} 的進程? (y=是, n=否)"}, + }} +}; \ No newline at end of file diff --git a/LiteLoader/Main/LiteLoader.cpp b/LiteLoader/Main/LiteLoader.cpp index 7901cf0f85..e462a4bed8 100644 --- a/LiteLoader/Main/LiteLoader.cpp +++ b/LiteLoader/Main/LiteLoader.cpp @@ -13,7 +13,7 @@ #include "Loader.h" #include "AutoUpgrade.h" #include "CrashLogger.h" -#include +#include "DefaultLangData.h" #include "AddonsHelper.h" #include #include "Version.h" @@ -84,7 +84,7 @@ void CheckRunningBDS() std::wstring path{buf, sz}; if (current == path) { - logger.error("Detected the existence of another bds process with the same path!"); + logger.error("Detected the existence of another BDS process with the same path!"); logger.error("This may cause the network port and the level to be occupied"); logger.error("Do you want to terminate the process with PID {}? (y=Yes, n=No)", pid); char ch; @@ -194,6 +194,12 @@ void LLMain() std::error_code ec; std::filesystem::create_directories("plugins", ec); + // Load Config + LL::LoadLLConfig(); + + // I18n + Translation::load("plugins/LiteLoader/language.json", LL::globalConfig.language, defaultLangData); + // Check Protocol Version CheckProtocolVersion(); @@ -207,15 +213,9 @@ void LLMain() // Init LL Logger Logger::setDefaultFile("logs/LiteLoader-latest.log", false); - // Load Config - LL::LoadLLConfig(); - // Check Running BDS(Requires Config) CheckRunningBDS(); - // I18n - Translation::load("plugins/LiteLoader/LangPack/" + LL::globalConfig.language + ".json"); - // Builtin CrashLogger LL::InitCrashLogger(LL::globalConfig.enableCrashLogger); @@ -279,4 +279,4 @@ THook(int, "main", int a, void* b) } LLMain(); return original(a, b); -} \ No newline at end of file +} diff --git a/RELEASE/plugins/LiteLoader/LangPack/en.json b/RELEASE/plugins/LiteLoader/LangPack/en.json deleted file mode 100644 index 86d3ba623c..0000000000 --- a/RELEASE/plugins/LiteLoader/LangPack/en.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "init.loadConfig.fail": "Loading ScriptEngine config failed", - "init.fileMapping.fail": "Failed to load global shared data", - "init.mapFile.fail": "Global shared data mapping failed", - "init.llMoney.noFound": "LLMoney.dll not found, ScriptEngine Economy System won't work", - "init.idFile.fail": "Failed to initialize unique identification file! Code: ", - "api.parseJson.fail": "Json parse error", - "remoteCall.createMessageThread.fail": "Creating Message Thread failed", - "remoteCall.createEventObject.fail": "Creating Event Object failed", - "remoteCall.postMessage.fail": "Posting remote call information failed", - "remoteCall.timeout.fail": "Remote call timeout, call failed", - "remoteCall.waitFail.fail": "Remote call waiting failed, call failed", - "remoteCall.postMessageReturn.fail": "Remote call post return value failed", - "remoteCall.inRemoteEngine.fail": "Remote call error in remote engine", - "base.getDimName.0": "Overworld", - "base.getDimName.1": "Nether", - "base.getDimName.2": "End", - "base.getDimName.unknown": "Other dimension", - "llseapi.require.success": " - Plugin require loaded successfully. Loaded: ", - "llseapi.require.fail": " - Plugin require load failed", - "llseapi.require.download.success": " - Successfully downloaded require! Path: ", - "llseapi.require.network.fail": " - Download plugin require failed! Code: " -} \ No newline at end of file diff --git a/RELEASE/plugins/LiteLoader/LangPack/zh_CN.json b/RELEASE/plugins/LiteLoader/LangPack/zh_CN.json deleted file mode 100644 index 5d0e9c3162..0000000000 --- a/RELEASE/plugins/LiteLoader/LangPack/zh_CN.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "init.loadConfig.fail": "LLSE配置文件加载失败!", - "init.fileMapping.fail": "全局共享数据加载失败!", - "init.mapFile.fail": "全局共享数据映射失败!", - "init.llMoney.noFound": "LLMoney.dll 未找到!LLSE经济系统将不会工作", - "init.idFile.fail": "初始化唯一识别文件失败!错误码:", - "api.parseJson.fail": "Json解析发生错误!", - "remoteCall.createMessageThread.fail": "创建消息循环失败!", - "remoteCall.createEventObject.fail": "创建事件对象失败!", - "remoteCall.postMessage.fail": "递送远程调用信息失败!", - "remoteCall.timeout.fail": "远程调用等待超时!调用失败", - "remoteCall.waitFail.fail": "远程调用等待失败!调用失败", - "remoteCall.postMessageReturn.fail": "远程调用传递返回值失败!", - "remoteCall.inRemoteEngine.fail": "在远程引擎中发生错误!", - "base.getDimName.0": "主世界", - "base.getDimName.1": "下界", - "base.getDimName.2": "末地", - "base.getDimName.unknown": "其他维度", - "llseapi.require.success": " - 插件依赖包加载成功。已加载:", - "llseapi.require.fail": " - 插件依赖包加载失败!", - "llseapi.require.download.success": " - 插件依赖包拉取成功!已安装到路径:", - "llseapi.require.network.fail": " - 插件依赖包拉取网络请求失败!错误码:" -} \ No newline at end of file diff --git a/RELEASE/plugins/LiteLoader/LangPack/zh_TW.json b/RELEASE/plugins/LiteLoader/LangPack/zh_TW.json deleted file mode 100644 index 68043411f6..0000000000 --- a/RELEASE/plugins/LiteLoader/LangPack/zh_TW.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "init.loadConfig.fail": "加載ScriptEngine配置文件失敗!", - "init.fileMapping.fail": "加載全局共享數據失敗!", - "init.mapFile.fail": "全局共享數據映射失敗!", - "init.llMoney.noFound": "LLMoney.dll 未找到!ScriptEngine經濟系統將無法工作", - "init.idFile.fail": "初始化唯一標識文件失敗! 錯誤代碼::", - "api.parseJson.fail": "解析Json時發生錯誤!", - "remoteCall.createMessageThread.fail": "創建消息線程失敗", - "remoteCall.createEventObject.fail": "創建事件線程失敗", - "remoteCall.postMessage.fail": "發送遠程呼叫信息失敗!", - "remoteCall.timeout.fail": "遠程調用超時,調用失敗!", - "remoteCall.waitFail.fail": "遠程呼叫等待失敗,呼叫失敗!", - "remoteCall.postMessageReturn.fail": "遠程調用後返回值失敗!", - "remoteCall.inRemoteEngine.fail": "在遠程引擎中的遠程調用錯誤!", - "base.getDimName.0": "主世界", - "base.getDimName.1": "地獄", - "base.getDimName.2": "終界", - "base.getDimName.unknown": "其他維度(世界)", - "llseapi.require.success": " - 插件所需拓展包加載成功。已加載:", - "llseapi.require.fail": " - 插件所需拓展包加載失敗!", - "llseapi.require.download.success": " - 插件所需拓展包下載成功!已安裝到至:", - "llseapi.require.network.fail": " - 插件所需拓展包下載請求失敗!錯誤代碼:" -} \ No newline at end of file From 623373ea7db4f6971610b5606e1468dd09f7a886 Mon Sep 17 00:00:00 2001 From: Jasonzyt <2126931871@qq.com> Date: Sat, 9 Jul 2022 18:16:11 +0800 Subject: [PATCH 10/10] Fix build --- LiteLoader/Header/GlobalServiceAPI.h | 1 - LiteLoader/Header/I18nAPI.h | 2 +- ScriptEngine/API/APIHelp.cpp | 2 +- ScriptEngine/API/CommandAPI.cpp | 4 +--- ScriptEngine/EconomySystem.cpp | 1 - ScriptEngine/Global.hpp | 1 - ScriptEngine/ScriptEngine.cpp | 1 - 7 files changed, 3 insertions(+), 9 deletions(-) diff --git a/LiteLoader/Header/GlobalServiceAPI.h b/LiteLoader/Header/GlobalServiceAPI.h index 2cdc1c7d78..7d659d280c 100644 --- a/LiteLoader/Header/GlobalServiceAPI.h +++ b/LiteLoader/Header/GlobalServiceAPI.h @@ -1,5 +1,4 @@ #pragma once -#include "Global.h" //Types namespace RakNet { diff --git a/LiteLoader/Header/I18nAPI.h b/LiteLoader/Header/I18nAPI.h index 04f5de9076..fbcc4b39b2 100644 --- a/LiteLoader/Header/I18nAPI.h +++ b/LiteLoader/Header/I18nAPI.h @@ -101,7 +101,7 @@ class I18N { */ LIAPI std::string get(const std::string& key, const std::string& langCode = ""); - static const constexpr char* POD_KEY = "_ll_plugin_i18n"; ///< PluginOwnData key + static const constexpr char* POD_KEY = "ll_plugin_i18n"; ///< PluginOwnData key }; diff --git a/ScriptEngine/API/APIHelp.cpp b/ScriptEngine/API/APIHelp.cpp index 52bbf9a41a..162290c2d1 100644 --- a/ScriptEngine/API/APIHelp.cpp +++ b/ScriptEngine/API/APIHelp.cpp @@ -1,4 +1,3 @@ -#include "APIHelp.h" #include #include #include @@ -23,6 +22,7 @@ #include #include #include +#include "APIHelp.h" using namespace std; diff --git a/ScriptEngine/API/CommandAPI.cpp b/ScriptEngine/API/CommandAPI.cpp index 97b301368b..63d286edba 100644 --- a/ScriptEngine/API/CommandAPI.cpp +++ b/ScriptEngine/API/CommandAPI.cpp @@ -1,6 +1,5 @@ -#include "DynamicCommandAPI.h" +//#include "DynamicCommandAPI.h" #include "CommandAPI.h" -#include "APIHelp.h" #include "McAPI.h" #include "ItemAPI.h" #include "PlayerAPI.h" @@ -14,7 +13,6 @@ #include #include #include -#include #include #include #include diff --git a/ScriptEngine/EconomySystem.cpp b/ScriptEngine/EconomySystem.cpp index a0430a24af..b71fc438c5 100644 --- a/ScriptEngine/EconomySystem.cpp +++ b/ScriptEngine/EconomySystem.cpp @@ -2,7 +2,6 @@ #include "API/EventAPI.h" #include #include -#include #include Logger economicLogger("EconomicSystem"); diff --git a/ScriptEngine/Global.hpp b/ScriptEngine/Global.hpp index 11fc2c0173..93027028f7 100644 --- a/ScriptEngine/Global.hpp +++ b/ScriptEngine/Global.hpp @@ -7,7 +7,6 @@ #include #include -#include #include #include #include diff --git a/ScriptEngine/ScriptEngine.cpp b/ScriptEngine/ScriptEngine.cpp index a190cdf36c..572fc2cb60 100644 --- a/ScriptEngine/ScriptEngine.cpp +++ b/ScriptEngine/ScriptEngine.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include using namespace std;