Skip to content
Permalink
Browse files

Added in loading from YAML files to bungee (#238)

  • Loading branch information...
darbyjack authored and chickeneer committed Aug 19, 2019
1 parent 82c4314 commit ab53b7b4e5cfe263326c9b724666ba6809d8cb07
Showing with 62 additions and 0 deletions.
  1. +62 −0 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 void loadLanguages() {
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;
}
}

0 comments on commit ab53b7b

Please sign in to comment.
You can’t perform that action at this time.