From ab53b7b4e5cfe263326c9b724666ba6809d8cb07 Mon Sep 17 00:00:00 2001 From: Glare Date: Mon, 19 Aug 2019 07:31:53 -0500 Subject: [PATCH] Added in loading from YAML files to bungee (#238) --- .../java/co/aikar/commands/BungeeLocales.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/bungee/src/main/java/co/aikar/commands/BungeeLocales.java b/bungee/src/main/java/co/aikar/commands/BungeeLocales.java index 12bd6d5e..d2f55012 100644 --- a/bungee/src/main/java/co/aikar/commands/BungeeLocales.java +++ b/bungee/src/main/java/co/aikar/commands/BungeeLocales.java @@ -1,5 +1,14 @@ package co.aikar.commands; +import co.aikar.locales.MessageKey; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + public class BungeeLocales extends Locales { private final BungeeCommandManager manager; @@ -16,4 +25,57 @@ public class BungeeLocales extends Locales { String pluginName = "acf-" + manager.plugin.getDescription().getName(); addMessageBundles("acf-minecraft", pluginName, pluginName.toLowerCase()); } + + /** + * Loads the given file + * + * @param file + * @param locale + * @return If any language keys were added + * @throws IOException + */ + public boolean loadYamlLanguageFile(File file, Locale locale) throws IOException { + Configuration yamlConfiguration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); + return loadLanguage(yamlConfiguration, locale); + } + + /** + * Loads a file out of the plugins data folder by the given name + * + * @param file + * @param locale + * @return If any language keys were added + * @throws IOException + */ + public boolean loadYamlLanguageFile(String file, Locale locale) throws IOException { + Configuration yamlConfiguration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(this.manager.plugin.getDataFolder(), file)); + return loadLanguage(yamlConfiguration, locale); + } + + + /** + * Loads every message from the Configuration object. Any nested values will be treated as namespace + * so acf-core:\n\tfoo: bar will be acf-core.foo = bar + * + * @param config + * @param locale + * @return If any language keys were added + */ + public boolean loadLanguage(Configuration config, Locale locale) { + boolean loaded = false; + for (String parentKey : config.getKeys()) { + Configuration inner = config.getSection(parentKey); + if (inner == null) { + continue; + } + for (String key : inner.getKeys()) { + String value = inner.getString(key); + if (value != null && !value.isEmpty()) { + addMessage(locale, MessageKey.of(parentKey + "." + key), value); + loaded = true; + } + } + } + return loaded; + } }