package com.zjinja.mcmod.decor.misc; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.zjinja.mcmod.decor.ZJinJaDecoration; import net.minecraft.util.StringTranslate; import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.IllegalFormatException; import java.util.Iterator; import java.util.regex.Pattern; public class I18NHandler { private static final Splitter equalSignSplitter = Splitter.on('=').limit(2); private static final Pattern numericVariablePattern = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]"); private HashMap table = Maps.newHashMap(); public I18NHandler(String locale){ try { InputStream is = StringTranslate.class.getResourceAsStream( String.format("/serverside/%s/i18n/%s.lang", ZJinJaDecoration.MODID, locale) ); Iterator iterator = IOUtils.readLines(is, Charsets.UTF_8).iterator(); while (iterator.hasNext()) { String s = (String)iterator.next(); if (!s.isEmpty() && s.charAt(0) != '#') { String[] astring = (String[]) Iterables.toArray(equalSignSplitter.split(s), String.class); if (astring != null && astring.length == 2) { String s1 = astring[0]; String s2 = numericVariablePattern.matcher(astring[1]).replaceAll("%$1s"); table.put(s1, s2); } } } ZJinJaDecoration.logger.info(String.format("[zjinjadecoration-I18N] %d Locale Dictionary Items Loaded.\n",table.size())); }catch (IOException e){ ZJinJaDecoration.logger.error(String.format("[zjinjadecoration] Error: Failed Load I18N Locale Resource: '%s'.\n",locale)); e.printStackTrace(); } } private String tryTranslateKey(String key) { String s1 = this.table.get(key); return s1 == null ? key : s1; } public synchronized boolean containsTranslateKey(String key) { return this.table.containsKey(key); } public synchronized String translateKeyFormat(String key, Object ... param) { String s1 = this.tryTranslateKey(key); try { return String.format(s1, param); } catch (IllegalFormatException illegalformatexception) { return "Format error: " + s1; } } public synchronized String translateKey(String key) { return this.tryTranslateKey(key); } }