From f02dbeabc1ec8efe432bdc885f93ad9aa8485413 Mon Sep 17 00:00:00 2001 From: Taskeren Date: Wed, 10 Jul 2019 15:12:27 +0800 Subject: [PATCH] =?UTF-8?q?[D]=20=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=A5=9E=E5=A5=87=E7=9A=84=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/glycol/t18n/I18n.java | 28 +++++++++++++++++++ src/main/java/cn/glycol/t18n/LanguageMap.java | 17 ++++++++++- src/main/java/cn/glycol/t18n/T18nUtils.java | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/glycol/t18n/I18n.java b/src/main/java/cn/glycol/t18n/I18n.java index 005d71b..07012cd 100644 --- a/src/main/java/cn/glycol/t18n/I18n.java +++ b/src/main/java/cn/glycol/t18n/I18n.java @@ -16,6 +16,12 @@ public class I18n { charset = Charset.forName(System.getProperty("file.encoding")); } + /* ******************************************************* + * + * Settings + * + * *******************************************************/ + public static void setLanguageMap(LanguageMap map) { I18n.map = map; } @@ -24,6 +30,12 @@ public class I18n { I18n.charset = Charset.forName(charset); } + /* ******************************************************* + * + * Localization functions + * + * *******************************************************/ + /** 自动从语言文件中提取翻译,空翻译时返回原键值 */ public static String translate(String key) { return reEncode(getLanguageMapSafe().get(key), charset); @@ -75,6 +87,12 @@ public class I18n { return getLanguageMapSafe().containsKey(key); } + /* ******************************************************* + * + * Utilities + * + * *******************************************************/ + /** @see #hasKey(String) */ private static boolean canTranslate(String key) { return hasKey(key); @@ -97,4 +115,14 @@ public class I18n { return map == null ? new LanguageMap() : map; } + /* ******************************************************* + * + * For Devlopers + * + * *******************************************************/ + + public static LanguageMap getLangMap() { + return map; + } + } diff --git a/src/main/java/cn/glycol/t18n/LanguageMap.java b/src/main/java/cn/glycol/t18n/LanguageMap.java index 68b3005..47c0dec 100644 --- a/src/main/java/cn/glycol/t18n/LanguageMap.java +++ b/src/main/java/cn/glycol/t18n/LanguageMap.java @@ -2,6 +2,7 @@ package cn.glycol.t18n; import java.util.ArrayList; import java.util.HashMap; +import java.util.Set; public class LanguageMap extends HashMap { @@ -11,7 +12,15 @@ public class LanguageMap extends HashMap { return super.getOrDefault(key, key.toString()); } - public ArrayList getAllKeyValuePairs() { + /** 返回所有键值对 + * @see #entrySet() + */ + public Set> getAllKeyValuePairs() { + return this.entrySet(); + } + + /** 获取所有键值对文字 */ + public ArrayList getAllKeyValuePairsString() { ArrayList list = new ArrayList(); for(Entry set : this.entrySet()) { @@ -28,4 +37,10 @@ public class LanguageMap extends HashMap { return list; } + /** 输出键值对 */ + public static void printKVPair(LanguageMap langMap) { + ArrayList lines = langMap.getAllKeyValuePairsString(); + lines.forEach(s->System.out.println(s)); + } + } diff --git a/src/main/java/cn/glycol/t18n/T18nUtils.java b/src/main/java/cn/glycol/t18n/T18nUtils.java index 20ca6de..125ac5b 100644 --- a/src/main/java/cn/glycol/t18n/T18nUtils.java +++ b/src/main/java/cn/glycol/t18n/T18nUtils.java @@ -68,7 +68,7 @@ public class T18nUtils { try { - String content = flattenList(map.getAllKeyValuePairs()); + String content = flattenList(map.getAllKeyValuePairsString()); if(!file.exists()) { file.getParentFile().mkdirs();