001package co.aikar.commands;
002
003import co.aikar.locales.MessageKey;
004import net.md_5.bungee.config.Configuration;
005import net.md_5.bungee.config.ConfigurationProvider;
006import net.md_5.bungee.config.YamlConfiguration;
007
008import java.io.File;
009import java.io.IOException;
010import java.util.Locale;
011
012public class BungeeLocales extends Locales {
013    private final BungeeCommandManager manager;
014
015    public BungeeLocales(BungeeCommandManager manager) {
016        super(manager);
017
018        this.manager = manager;
019        this.addBundleClassLoader(this.manager.getPlugin().getClass().getClassLoader());
020    }
021
022    @Override
023    public void loadLanguages() {
024        super.loadLanguages();
025        String pluginName = "acf-" + manager.plugin.getDescription().getName();
026        addMessageBundles("acf-minecraft", pluginName, pluginName.toLowerCase(Locale.ENGLISH));
027    }
028
029    /**
030     * Loads the given file
031     *
032     * @param file
033     * @param locale
034     * @return If any language keys were added
035     * @throws IOException
036     */
037    public boolean loadYamlLanguageFile(File file, Locale locale) throws IOException {
038        Configuration yamlConfiguration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
039        return loadLanguage(yamlConfiguration, locale);
040    }
041
042    /**
043     * Loads a file out of the plugin's data folder by the given name
044     *
045     * @param file
046     * @param locale
047     * @return If any language keys were added
048     * @throws IOException
049     */
050    public boolean loadYamlLanguageFile(String file, Locale locale) throws IOException {
051        Configuration yamlConfiguration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(this.manager.plugin.getDataFolder(), file));
052        return loadLanguage(yamlConfiguration, locale);
053    }
054
055
056    /**
057     * Loads every message from the Configuration object. Any nested values will be treated as namespace
058     * so acf-core:\n\tfoo: bar will be acf-core.foo = bar
059     *
060     * @param config
061     * @param locale
062     * @return If any language keys were added
063     */
064    public boolean loadLanguage(Configuration config, Locale locale) {
065        boolean loaded = false;
066        for (String parentKey : config.getKeys()) {
067            Configuration inner = config.getSection(parentKey);
068            if (inner == null) {
069                continue;
070            }
071            for (String key : inner.getKeys()) {
072                String value = inner.getString(key);
073                if (value != null && !value.isEmpty()) {
074                    addMessage(locale, MessageKey.of(parentKey + "." + key), value);
075                    loaded = true;
076                }
077            }
078        }
079        return loaded;
080    }
081}