package totallynotmalware.i18n;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Language {
	private Map<String, String> keys;
	private String code;
	
	public Language(String code) throws IOException {
		this.keys=new HashMap<String, String>();
		this.code=code;
		
		// could be in these two places
		InputStream in=this.getClass().getClassLoader().getResourceAsStream(code+".lang");
		if(in==null) in=this.getClass().getClassLoader().getResourceAsStream("/data/"+code+".lang");
		
		// give up
		if(in==null) throw new IOException("Language file not found: "+code);
		
		// read the language file
		BufferedReader reader=new BufferedReader(new InputStreamReader(in));
		for(String line=reader.readLine(); line!=null; line=reader.readLine()) {
			if(line.isEmpty()) continue;
			String[] parts=line.split("=", 2);
			this.keys.put(parts[0], parts[1]);
		}
		reader.close();
	}
	
	public String get(String name) {
		return this.keys.getOrDefault(name, name);
	}
	public String getCode() {
		return this.code;
	}
}
