Detect and replace default language setting in config files during initialization

This commit is contained in:
Xujiayao 2026-01-19 13:58:18 +08:00
parent fae6d8b511
commit 65b019cfa6
6 changed files with 44 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package com.xujiayao.discord_mc_chat.utils.config;
import com.fasterxml.jackson.databind.JsonNode;
import com.xujiayao.discord_mc_chat.utils.StringUtils;
import com.xujiayao.discord_mc_chat.utils.YamlUtils;
import com.xujiayao.discord_mc_chat.utils.i18n.I18nManager;
@ -10,7 +11,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.function.Function;
import static com.xujiayao.discord_mc_chat.Constants.LOGGER;
@ -43,16 +43,24 @@ public class ConfigManager {
// If config.yml does not exist or is empty, create it from the appropriate template.
if (!Files.exists(CONFIG_FILE_PATH) || Files.size(CONFIG_FILE_PATH) == 0) {
LOGGER.warn(I18nManager.getDmccTranslation("utils.config.config.not_found"));
LOGGER.warn(I18nManager.getDmccTranslation("utils.config.config.creating", CONFIG_FILE_PATH));
LOGGER.warn(I18nManager.getDmccTranslation("utils.config.config.edit_prompt", CONFIG_FILE_PATH));
LOGGER.info(I18nManager.getDmccTranslation("utils.config.config.creating", CONFIG_FILE_PATH));
LOGGER.info(I18nManager.getDmccTranslation("utils.config.config.replacing_lang", I18nManager.getLanguage()));
LOGGER.info(I18nManager.getDmccTranslation("utils.config.config.edit_prompt", CONFIG_FILE_PATH));
try (InputStream inputStream = ConfigManager.class.getResourceAsStream(configTemplatePath)) {
if (inputStream == null) {
throw new IOException("Default config template not found: " + configTemplatePath);
}
// Copy the template config file as is
Files.copy(inputStream, CONFIG_FILE_PATH, StandardCopyOption.REPLACE_EXISTING);
// Read the template content
String template = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
// Replace the default language with the detected system language.
// This ensures the generated config file uses the user's system language if supported.
template = template.replace("\nlanguage: \"to_be_auto_replaced\"\n", StringUtils.format("\nlanguage: \"{}\"\n", I18nManager.getLanguage()));
// Write the config file with the replaced language setting
Files.writeString(CONFIG_FILE_PATH, template, StandardCharsets.UTF_8);
}
return false;

View file

@ -13,6 +13,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import static com.xujiayao.discord_mc_chat.Constants.LOGGER;
@ -30,9 +31,34 @@ public class I18nManager {
private static final Path CUSTOM_MESSAGES_DIR = Paths.get("./config/discord_mc_chat/custom_messages");
private static final Path CACHE_DIR = Paths.get("./config/discord_mc_chat/cache/lang");
private static final Map<String, String> minecraftTranslations = new HashMap<>();
private static String language = "en_us";
private static String language = detectLanguage();
private static JsonNode customMessages;
/**
* Gets the currently selected language code.
*
* @return The language code (e.g., "en_us").
*/
public static String getLanguage() {
return language;
}
/**
* Detects the system language and checks if it is supported by DMCC.
*
* @return The detected language code (e.g., "zh_cn") if supported, otherwise "en_us".
*/
public static String detectLanguage() {
String code = Locale.getDefault().toString().toLowerCase();
// Check if the internal translation file exists for the detected language
if (I18nManager.class.getResource("/lang/" + code + ".yml") != null) {
return code;
}
return "en_us";
}
/**
* Loads only DMCC's internal translations from resources.
*

View file

@ -12,7 +12,7 @@ mode: single_server
# ==================================================
# 语言设置
language: "en_us"
language: "to_be_auto_replaced"
# Discord 设置
discord:

View file

@ -12,7 +12,7 @@ mode: standalone
# ==================================================
# 语言设置
language: "en_us"
language: "to_be_auto_replaced"
# 多服务器功能
multi_server:

View file

@ -75,6 +75,7 @@ utils:
config:
not_found: "Configuration file not found or is empty."
creating: "Creating a default one at \"{}\"."
replacing_lang: "Replacing language setting in config file to \"{}\"."
edit_prompt: "Please edit \"{}\" before reloading DMCC."
mode_mismatch: "Mode mismatch detected."
mode_mismatch_detail: "The expected mode is \"{}\" (from mode.yml or environment), but config.yml is for \"{}\"."

View file

@ -75,6 +75,7 @@ utils:
config:
not_found: "配置文件未找到或为空。"
creating: "正在于 \"{}\" 创建默认文件。"
replacing_lang: "正在将配置文件中的语言设置替换为 \"{}\"。"
edit_prompt: "请在重新加载 DMCC 之前编辑 \"{}\"。"
mode_mismatch: "检测到模式不匹配。"
mode_mismatch_detail: "期望的模式是 \"{}\"(来自 mode.yml 或环境),但 config.yml 是针对 \"{}\" 的。"