Skip to content

Commit

Permalink
add methods to allow loading yaml locale files (aikar#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicboy committed Nov 7, 2024
1 parent 43323e6 commit 301340f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion velocity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.1.0</version>
<version>3.4.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
109 changes: 109 additions & 0 deletions velocity/src/main/java/co/aikar/commands/VelocityLocales.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, String> visit = config.visit(new ConfigurationVisitor.Safe<Map<String, String>, Map<String, String>>() {
@Override
public Map<String, String> newState() {
return new HashMap<>();
}

@Override
public void beginVisit(ConfigurationNode node, Map<String, String> state) {
}

@Override
public void enterNode(ConfigurationNode node, Map<String, String> state) {
}

@Override
public void enterMappingNode(ConfigurationNode node, Map<String, String> state) {
}

@Override
public void enterListNode(ConfigurationNode node, Map<String, String> state) {
}

@Override
public void enterScalarNode(ConfigurationNode node, Map<String, String> state) {
Iterator<Object> 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<String, String> state) {
}

@Override
public void exitListNode(ConfigurationNode node, Map<String, String> state) {
}

@Override
public Map<String, String> endVisit(Map<String, String> state) {
return state;
}
});

visit.forEach((key, value) -> {
this.addMessage(locale, MessageKey.of(key), value);
loaded.set(true);
});


return loaded.get();
}
}

0 comments on commit 301340f

Please sign in to comment.