From 301340fdbc01fa063430b171d559838a91c31ce2 Mon Sep 17 00:00:00 2001 From: Shane Freeder Date: Thu, 7 Nov 2024 12:33:10 +0000 Subject: [PATCH] add methods to allow loading yaml locale files (#412) --- velocity/pom.xml | 2 +- .../co/aikar/commands/VelocityLocales.java | 109 ++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/velocity/pom.xml b/velocity/pom.xml index 66d82f594..d91f493f0 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -32,7 +32,7 @@ com.velocitypowered velocity-api - 3.1.0 + 3.4.0-SNAPSHOT provided diff --git a/velocity/src/main/java/co/aikar/commands/VelocityLocales.java b/velocity/src/main/java/co/aikar/commands/VelocityLocales.java index 71737129d..527d03a51 100644 --- a/velocity/src/main/java/co/aikar/commands/VelocityLocales.java +++ b/velocity/src/main/java/co/aikar/commands/VelocityLocales.java @@ -1,6 +1,19 @@ package co.aikar.commands; +import co.aikar.locales.MessageKey; +import org.spongepowered.configurate.ConfigurateException; +import org.spongepowered.configurate.ConfigurationNode; +import org.spongepowered.configurate.ConfigurationVisitor; +import org.spongepowered.configurate.yaml.YamlConfigurationLoader; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; import java.util.Locale; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; public class VelocityLocales extends Locales { private final VelocityCommandManager manager; @@ -18,4 +31,100 @@ public void loadLanguages() { String pluginName = "acf-" + manager.plugin.getDescription().getName().get(); addMessageBundles("acf-minecraft", pluginName, pluginName.toLowerCase(Locale.ENGLISH)); } + + /** + * Loads the given file + * + * @param file + * @param locale + * @return If any language keys were added + * @throws IOException + * @throws ConfigurateException + */ + public boolean loadYamlLanguageFile(File file, Locale locale) throws IOException { + ConfigurationNode configuration = YamlConfigurationLoader.builder().file(file).build().load(); + return loadLanguage(configuration, locale); + } + + /** + * Loads a file out of the plugin's data folder by the given name + * + * @param file + * @param locale + * @return If any language keys were added + * @throws IOException + * @throws ConfigurateException + */ + public boolean loadYamlLanguageFile(String file, Locale locale) throws IOException { + File pluginDir = new File("plugins", this.manager.getPlugin().getDescription().getId()); + return loadYamlLanguageFile(new File(pluginDir, file), 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(ConfigurationNode config, Locale locale) { + AtomicBoolean loaded = new AtomicBoolean(false); + Map visit = config.visit(new ConfigurationVisitor.Safe, Map>() { + @Override + public Map newState() { + return new HashMap<>(); + } + + @Override + public void beginVisit(ConfigurationNode node, Map state) { + } + + @Override + public void enterNode(ConfigurationNode node, Map state) { + } + + @Override + public void enterMappingNode(ConfigurationNode node, Map state) { + } + + @Override + public void enterListNode(ConfigurationNode node, Map state) { + } + + @Override + public void enterScalarNode(ConfigurationNode node, Map state) { + Iterator keyIter = Arrays.asList(node.path().array()).iterator(); + StringBuilder key = new StringBuilder(); + while (keyIter.hasNext()) { + key.append(keyIter.next()); + if (keyIter.hasNext()) { + key.append("."); + } + } + state.put(key.toString(), node.getString()); + } + + @Override + public void exitMappingNode(ConfigurationNode node, Map state) { + } + + @Override + public void exitListNode(ConfigurationNode node, Map state) { + } + + @Override + public Map endVisit(Map state) { + return state; + } + }); + + visit.forEach((key, value) -> { + this.addMessage(locale, MessageKey.of(key), value); + loaded.set(true); + }); + + + return loaded.get(); + } }