package com.publicuhc.translate;

import com.google.inject.Inject;
import com.publicuhc.configuration.Config;
import com.publicuhc.translate.exceptions.LocaleNotFoundError;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.HashMap;
import java.util.Map;

public class DefaultTranslate implements Translate {

    private final Config m_config;

    @Inject
    protected DefaultTranslate(Config config){
        m_config = config;
    }

    @Override
    public String translate(String key, String locale) {
        return translate(key, locale, new HashMap<String, String>());
    }

    @Override
    public String translate(String key, String locale, Map<String, String> vars) {
        FileConfiguration configuration = m_config.getConfig("translations/"+locale);

        String value = configuration.getString(key, "TRANSLATION KEY NOT SET FOR "+key);

        if(null == value){
            throw new LocaleNotFoundError();
        }
        for(String s : vars.keySet()){
            value = value.replaceAll("%"+s+"%", vars.get(s));
        }
        return value;
    }

    @Override
    public String translate(String key, String locale, String var, String value) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(var, value);
        return translate(key, locale, map);
    }

    @Override
    public String getLocaleForSender(CommandSender sender) {
        //TODO find the saved locale for the user
        //TODO return the default locale from the config file
        return null;
    }
}

package com.publicuhc.translate;

import com.google.inject.Inject;
import com.publicuhc.configuration.Config;
import com.publicuhc.translate.exceptions.LocaleNotFoundError;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.HashMap;
import java.util.Map;

public class DefaultTranslate implements Translate {

    private final Config m_config;

    @Inject
    protected DefaultTranslate(Config config){
        m_config = config;
    }

    @Override
    public String translate(String key, String locale) {
        return translate(key, locale, new HashMap<String, String>());
    }

    @Override
    public String translate(String key, String locale, Map<String, String> vars) {
        FileConfiguration configuration = m_config.getConfig("translations/"+locale);

        if(null == configuration){
            throw new LocaleNotFoundError();
        }

        String value = configuration.getString(key, "TRANSLATION KEY NOT SET FOR "+key);

        for(String s : vars.keySet()){
            value = value.replaceAll("%"+s+"%", vars.get(s));
        }
        return value;
    }

    @Override
    public String translate(String key, String locale, String var, String value) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(var, value);
        return translate(key, locale, map);
    }

    @Override
    public String getLocaleForSender(CommandSender sender) {
        //TODO find the saved locale for the user
        //TODO return the default locale from the config file
        return null;
    }
}

check the right thing for null locale