feat: add custom lang file
/ build (push) Failing after 3m23s

This commit is contained in:
2026-06-22 09:33:42 +08:00
Unverified
parent 8aaef54548
commit 51f48b8820
+33 -9
View File
@@ -12,6 +12,8 @@
#include "ll/api/mod/ModManagerRegistry.h"
#include "ll/api/mod/RegisterHelper.h"
#include "ll/api/service/Bedrock.h"
#include "ll/api/io/FileUtils.h"
#include <filesystem>
#include "gmlib/gm/i18n/LangI18n.h"
#include "gmlib/gm/i18n/ResourceI18n.h"
@@ -34,7 +36,12 @@ Entry& Entry::getInstance() {
bool Entry::load() {
isResourceI18nLoaded=false;
if (!ll::file_utils::readFile(getSelf().getLangDir() / u8"en_US.lang").has_value()) {
ll::file_utils::writeFile(getSelf().getLangDir() / u8"en_US.lang", en_US);
}
if (!ll::file_utils::readFile(getSelf().getLangDir() / u8"zh_CN.lang").has_value()) {
ll::file_utils::writeFile(getSelf().getLangDir() / u8"zh_CN.lang", zh_CN);
}
mConfig.emplace();
if (!ll::config::loadConfig(*mConfig, getSelf().getConfigDir() / u8"config.json")) {
ll::config::saveConfig(*mConfig, getSelf().getConfigDir() / u8"config.json");
@@ -82,19 +89,38 @@ gmlib::i18n::LangI18n& Entry::getI18n() {
}
void Entry::loadI18n() {
const std::string custom_zh_CN = ll::file_utils::readFile(
getSelf().getLangDir() / u8"zh_CN.lang",
false
)
.value_or(zh_CN);
const std::string custom_en_US = ll::file_utils::readFile(
getSelf().getLangDir() / u8"en_US.lang",
false
)
.value_or(en_US);
mI18n= std::make_unique<gmlib::i18n::LangI18n>(getSelf().getLangDir(), getConfig().ServerSideTranslation.Language);
mI18n->updateOrCreateLanguage("en_US", en_US);
mI18n->updateOrCreateLanguage("zh_CN", zh_CN);
mI18n->updateOrCreateLanguage("en_US", custom_en_US);
mI18n->updateOrCreateLanguage("zh_CN", custom_zh_CN);
mI18n->loadAllLanguages();
mI18n->setDefaultLanguage("zh_CN");
mI18n->setDefaultLanguage(getConfig().ServerSideTranslation.Language);
}
void Entry::loadResourcePack() {
auto mResource = std::make_unique<gmlib::i18n::ResourceI18n>(getSelf().getModDir() / u8"lang", MOD_NAME, 0, 16, 1);
mResource->addLanguage("en_US", en_US);
mResource->addLanguage("zh_CN", zh_CN);
if (
ll::file_utils::readFile(getSelf().getLangDir() / u8"en_US.lang")!=
ll::file_utils::readFile(getSelf().getModDir() / u8"resource/language_pack/texts/en_US.lang")
||ll::file_utils::readFile(getSelf().getLangDir() / u8"zh_CN.lang")!=
ll::file_utils::readFile(getSelf().getModDir() / u8"resource/language_pack/texts/zh_CN.lang")
)
std::filesystem::remove_all(getSelf().getModDir() / u8"resource");
auto mResource = std::make_unique<gmlib::i18n::ResourceI18n>(getSelf().getModDir() / u8"resource", MOD_NAME, 0, 16, 1);
mResource->addLanguageFromPath("zh_CN",getSelf().getLangDir() / u8"zh_CN.lang");
mResource->addLanguageFromPath("en_US",getSelf().getLangDir() / u8"en_US.lang");
mResource->loadAllLanguages();
isResourceI18nLoaded=true;
}
@@ -110,8 +136,6 @@ LL_REGISTER_MOD(DeathMessages::Entry, DeathMessages::Entry::getInstance());
std::string tr(std::string const& key, std::vector<std::string> const& params) {
auto& i18n = DeathMessages::Entry::getInstance().getI18n();
return i18n.get(key, params);
return key;
}