1
0
mirror of https://github.com/nitu2003/T18n synced 2024-11-21 23:28:49 +00:00

[D] 添加一些神奇的注解

This commit is contained in:
Taskeren 2019-07-10 15:12:27 +08:00
parent 009699b11e
commit f02dbeabc1
3 changed files with 45 additions and 2 deletions

View File

@ -16,6 +16,12 @@ public class I18n {
charset = Charset.forName(System.getProperty("file.encoding")); charset = Charset.forName(System.getProperty("file.encoding"));
} }
/* *******************************************************
*
* Settings
*
* *******************************************************/
public static void setLanguageMap(LanguageMap map) { public static void setLanguageMap(LanguageMap map) {
I18n.map = map; I18n.map = map;
} }
@ -24,6 +30,12 @@ public class I18n {
I18n.charset = Charset.forName(charset); I18n.charset = Charset.forName(charset);
} }
/* *******************************************************
*
* Localization functions
*
* *******************************************************/
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */ /** 自动从语言文件中提取翻译,空翻译时返回原键值 */
public static String translate(String key) { public static String translate(String key) {
return reEncode(getLanguageMapSafe().get(key), charset); return reEncode(getLanguageMapSafe().get(key), charset);
@ -75,6 +87,12 @@ public class I18n {
return getLanguageMapSafe().containsKey(key); return getLanguageMapSafe().containsKey(key);
} }
/* *******************************************************
*
* Utilities
*
* *******************************************************/
/** @see #hasKey(String) */ /** @see #hasKey(String) */
private static boolean canTranslate(String key) { private static boolean canTranslate(String key) {
return hasKey(key); return hasKey(key);
@ -97,4 +115,14 @@ public class I18n {
return map == null ? new LanguageMap() : map; return map == null ? new LanguageMap() : map;
} }
/* *******************************************************
*
* For Devlopers
*
* *******************************************************/
public static LanguageMap getLangMap() {
return map;
}
} }

View File

@ -2,6 +2,7 @@ package cn.glycol.t18n;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
public class LanguageMap extends HashMap<String, String> { public class LanguageMap extends HashMap<String, String> {
@ -11,7 +12,15 @@ public class LanguageMap extends HashMap<String, String> {
return super.getOrDefault(key, key.toString()); return super.getOrDefault(key, key.toString());
} }
public ArrayList<String> getAllKeyValuePairs() { /** 返回所有键值对
* @see #entrySet()
*/
public Set<Entry<String, String>> getAllKeyValuePairs() {
return this.entrySet();
}
/** 获取所有键值对文字 */
public ArrayList<String> getAllKeyValuePairsString() {
ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list = new ArrayList<String>();
for(Entry<String, String> set : this.entrySet()) { for(Entry<String, String> set : this.entrySet()) {
@ -28,4 +37,10 @@ public class LanguageMap extends HashMap<String, String> {
return list; return list;
} }
/** 输出键值对 */
public static void printKVPair(LanguageMap langMap) {
ArrayList<String> lines = langMap.getAllKeyValuePairsString();
lines.forEach(s->System.out.println(s));
}
} }

View File

@ -68,7 +68,7 @@ public class T18nUtils {
try { try {
String content = flattenList(map.getAllKeyValuePairs()); String content = flattenList(map.getAllKeyValuePairsString());
if(!file.exists()) { if(!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();