mirror of
https://github.com/nitu2003/T18n
synced 2024-11-21 23:28:49 +00:00
[+/-] 对方法进行了调整
1. 添加了 translateList 和 flattenList 方法 2. 修改了 formatList 方法
This commit is contained in:
parent
ce04f7487e
commit
4b01a2e58d
@ -9,6 +9,8 @@ public class I18n {
|
||||
private static LanguageMap map;
|
||||
private static Charset charset;
|
||||
|
||||
public static final int LOOP_MAX_COUNT = 1000;
|
||||
|
||||
static {
|
||||
charset = Charset.forName(System.getProperty("file.encoding"));
|
||||
}
|
||||
@ -21,6 +23,11 @@ public class I18n {
|
||||
I18n.charset = Charset.forName(charset);
|
||||
}
|
||||
|
||||
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */
|
||||
public static String translate(String key) {
|
||||
return reEncode(getLanguageMapSafe().get(key), charset);
|
||||
}
|
||||
|
||||
/** 自动翻译(translate)后再执行格式化(format) */
|
||||
public static String format(String key, Object...format) {
|
||||
return tryFormat(
|
||||
@ -28,24 +35,19 @@ public class I18n {
|
||||
format);
|
||||
}
|
||||
|
||||
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */
|
||||
public static String translate(String key) {
|
||||
return reEncode(getLanguageMapSafe().get(key), charset);
|
||||
}
|
||||
|
||||
/** 自动翻译(translate)列表,将多行替换为一行后再执行格式化(format)<br>
|
||||
* @param key 进入列表的格式化代码(format key),例如<code>app.message.welcome.%s</code>,其中<code>%s</code>会被替换为从<code>0</code>开始的数字,逐行读取,直到空翻译或最大值(1000)
|
||||
/**
|
||||
* 连续读取翻译(translate)。
|
||||
* @param keyRegular 翻译键值表达式,需要填入一个“%s”用于替换为行数。行数从0开始。例如“welcome.%s”,则程序会依次向列表中添加“welcome.0”,“welcome.1”...的翻译,直到获取到空值。
|
||||
*/
|
||||
public static String format2(String key, Object...format) {
|
||||
public static List<String> translateList(String keyRegular) {
|
||||
|
||||
List<String> vlist = new ArrayList<String>();
|
||||
|
||||
int i = 0;
|
||||
int max = 1000;
|
||||
|
||||
while(true) {
|
||||
if(i >= max) break;
|
||||
String k = tryFormat(key, i);
|
||||
if(i >= LOOP_MAX_COUNT) break;
|
||||
String k = tryFormat(keyRegular, i);
|
||||
if(hasKey(k)) {
|
||||
vlist.add(translate(k));
|
||||
} else {
|
||||
@ -54,18 +56,33 @@ public class I18n {
|
||||
i++;
|
||||
}
|
||||
|
||||
return vlist;
|
||||
|
||||
}
|
||||
|
||||
/** 使字符串列表扁平化。 */
|
||||
public static String flattenList(List<String> vlist) {
|
||||
|
||||
String v = "";
|
||||
for(String o : vlist) {
|
||||
v += o + "\n";
|
||||
}
|
||||
v = v.trim();
|
||||
|
||||
v = tryFormat(v, format);
|
||||
|
||||
return v;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将列表翻译中的内容进行format操作。
|
||||
* @see #translateList(String)
|
||||
*/
|
||||
public static String formatList(String keyRegular, Object...format) {
|
||||
|
||||
return tryFormat(flattenList(translateList(keyRegular)), format);
|
||||
|
||||
}
|
||||
|
||||
public static boolean hasKey(String key) {
|
||||
return getLanguageMapSafe().containsKey(key);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user