import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*; public class I18nUtils { private static final Map<String, Map<String, String>> i18nData = new HashMap<>(); public static Map<String, String> getI18nByLangCode(String langCode) {
langCode = langCode.toLowerCase();
String fileName = "string_" + langCode + ".properties";
return i18nData.get(fileName);
} public static Map<String, String> getI18nMap(String langCode) {
Map<String, String> m = getI18nByLangCode(langCode);
if (m == null) {
m = getI18nByLangCode("en");
}
return m;
} public static void loadI18nResouce() throws Exception { String path = I18nUtils.class.getResource("/").getPath(); File f = new File(path + "/i18n"); if (f.exists() && f.isDirectory()) { File[] files = f.listFiles(); if (files != null && files.length > 0) { for (File file : files) { String fileName = file.getName(); if (fileName.startsWith("string_") && fileName.endsWith(".properties")) { InputStream in = new FileInputStream(file); Properties properties = new Properties(); properties.load(in); Map<String, String> map = toMap(properties); i18nData.put(fileName, map); in.close();
properties.clear(); } } } } } private static Map<String, String> toMap(Properties properties) { Map<String, String> map = new HashMap<>(); Enumeration<?> names = properties.propertyNames(); while (names.hasMoreElements()) {
Object name = names.nextElement(); if (name != null) { String name1 = name.toString(); String value = properties.getProperty(name1); map.put(name1, value);
}
} return map; } }

  

05-08 08:12