87 lines
3.2 KiB
Java
87 lines
3.2 KiB
Java
package cn.infstar.cmagicgem.config;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
public final class LanguageManager {
|
|
private final JavaPlugin plugin;
|
|
private volatile YamlConfiguration messages = new YamlConfiguration();
|
|
private volatile String language = "zh_CN";
|
|
|
|
public LanguageManager(JavaPlugin plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
public void reload() {
|
|
String configured = plugin.getConfig().getString("language",
|
|
plugin.getConfig().getString("lang", "zh_CN"));
|
|
language = safeName(configured);
|
|
File file = new File(plugin.getDataFolder(), "languages/" + language + ".yml");
|
|
YamlConfiguration loaded = YamlConfiguration.loadConfiguration(file);
|
|
try (InputStream input = plugin.getResource("languages/" + language + ".yml")) {
|
|
if (input != null) {
|
|
YamlConfiguration bundled = YamlConfiguration.loadConfiguration(
|
|
new InputStreamReader(input, StandardCharsets.UTF_8));
|
|
loaded.setDefaults(bundled);
|
|
}
|
|
} catch (Exception exception) {
|
|
plugin.getLogger().warning("无法读取内置语言 " + language + ": " + exception.getMessage());
|
|
}
|
|
messages = loaded;
|
|
}
|
|
|
|
public String language() {
|
|
return language;
|
|
}
|
|
|
|
public String text(String path, String fallback, Object... replacements) {
|
|
String value = messages.getString(path, fallback);
|
|
if (value == null) value = fallback == null ? "" : fallback;
|
|
value = commandName(value);
|
|
for (int index = 0; index + 1 < replacements.length; index += 2) {
|
|
String key = String.valueOf(replacements[index]);
|
|
String replacement = String.valueOf(replacements[index + 1]);
|
|
value = value.replace("{" + key + "}", replacement).replace("%" + key + "%", replacement);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public String format(String path, String fallback, Object... arguments) {
|
|
String pattern = text(path, fallback);
|
|
try {
|
|
return String.format(Locale.ROOT, pattern, arguments);
|
|
} catch (RuntimeException exception) {
|
|
return pattern;
|
|
}
|
|
}
|
|
|
|
public List<String> section(String path) {
|
|
ConfigurationSection section = messages.getConfigurationSection(path);
|
|
if (section == null) return List.of();
|
|
List<String> result = new ArrayList<>();
|
|
for (String key : section.getKeys(false)) {
|
|
String value = section.getString(key);
|
|
if (value != null) result.add(commandName(value));
|
|
}
|
|
return List.copyOf(result);
|
|
}
|
|
|
|
private String commandName(String value) {
|
|
return value;
|
|
}
|
|
|
|
private String safeName(String configured) {
|
|
if (configured == null || !configured.matches("[A-Za-z0-9_-]+")) return "zh_CN";
|
|
return configured;
|
|
}
|
|
}
|