diff --git a/src/main/java/org/ttzero/excel/entity/I18N.java b/src/main/java/org/ttzero/excel/entity/I18N.java index e4c8ebd6..e6e70eaa 100644 --- a/src/main/java/org/ttzero/excel/entity/I18N.java +++ b/src/main/java/org/ttzero/excel/entity/I18N.java @@ -22,19 +22,29 @@ import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.Properties; /** * @author guanquan.wang on 2018-10-13 */ public class I18N { - private final Properties pro; + /** + * Here converted to HashMap is to improve performance, + * Properties extends to Hashtable All methods are synchronous methods, + * I18N only needs to read without writing, so converting to HashMap can greatly improve performance + */ + private Map pro; public I18N() { + init(); + } + + void init() { Locale locale = Locale.getDefault(); String fn = "message._.properties"; - pro = new Properties(); try { InputStream is = I18N.class.getClassLoader().getResourceAsStream("I18N/" + fn.replace("_", locale.toLanguageTag())); if (is == null) { @@ -42,12 +52,21 @@ public I18N() { } if (is != null) { try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { + Properties pro = new Properties(); pro.load(reader); + Map map = new HashMap<>(pro.size()); + for (Map.Entry entry : pro.entrySet()) { + map.put(entry.getKey().toString(), entry.getValue().toString()); + } + this.pro = map; } } + // FIXME classpath } catch (IOException e) { // nothing... } + + if (pro == null) pro = new HashMap<>(); } /** @@ -57,7 +76,7 @@ public I18N() { * @return I18N string */ public String get(String code) { - return pro.getProperty(code, code); + return pro.getOrDefault(code, code); } /** @@ -68,7 +87,7 @@ public String get(String code) { * @return I18N string */ public String getOrElse(String code, String other) { - return pro.getProperty(code, other); + return pro.getOrDefault(code, other); } /** @@ -79,7 +98,7 @@ public String getOrElse(String code, String other) { * @return I18N string */ public String get(String code, String... args) { - String msg = pro.getProperty(code, code); + String msg = pro.getOrDefault(code, code); char[] oldValue = msg.toCharArray(); int[] indexs = search(oldValue); int len = Math.min(indexs.length, args.length), size = 0;