2019-06-25 13:29:10 +00:00
|
|
|
|
package cn.glycol.t18n;
|
|
|
|
|
|
2019-06-26 09:10:15 +00:00
|
|
|
|
import java.nio.charset.Charset;
|
2019-06-28 04:40:06 +00:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2019-06-26 09:10:15 +00:00
|
|
|
|
|
2019-08-30 04:17:48 +00:00
|
|
|
|
|
2019-06-25 13:29:10 +00:00
|
|
|
|
public class I18n {
|
|
|
|
|
|
2019-08-30 04:17:48 +00:00
|
|
|
|
protected static LanguageMap map;
|
|
|
|
|
protected static Charset charset;
|
2019-06-26 09:10:15 +00:00
|
|
|
|
|
2019-08-30 04:17:48 +00:00
|
|
|
|
protected static final int LOOP_MAX_COUNT = 32767;
|
2019-06-30 07:26:41 +00:00
|
|
|
|
|
2019-06-26 09:10:15 +00:00
|
|
|
|
static {
|
2019-08-30 04:17:48 +00:00
|
|
|
|
map = new LanguageMap();
|
2019-06-26 09:10:15 +00:00
|
|
|
|
charset = Charset.forName(System.getProperty("file.encoding"));
|
|
|
|
|
}
|
2019-06-25 13:29:10 +00:00
|
|
|
|
|
2019-07-10 07:12:27 +00:00
|
|
|
|
/* *******************************************************
|
|
|
|
|
*
|
|
|
|
|
* Localization functions
|
|
|
|
|
*
|
|
|
|
|
* *******************************************************/
|
|
|
|
|
|
2019-06-30 07:26:41 +00:00
|
|
|
|
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */
|
2019-10-01 11:17:50 +00:00
|
|
|
|
protected static String translate(String key) {
|
2019-08-30 04:17:48 +00:00
|
|
|
|
return reEncode(map.get(key), charset);
|
2019-06-30 07:26:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 04:40:06 +00:00
|
|
|
|
/** 自动翻译(translate)后再执行格式化(format) */
|
2019-06-25 13:29:10 +00:00
|
|
|
|
public static String format(String key, Object...format) {
|
2019-06-26 09:10:15 +00:00
|
|
|
|
return tryFormat(
|
2019-06-28 04:40:06 +00:00
|
|
|
|
translate(key),
|
2019-06-26 09:10:15 +00:00
|
|
|
|
format);
|
2019-06-25 13:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 07:26:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* 连续读取翻译(translate)。
|
|
|
|
|
* @param keyRegular 翻译键值表达式,需要填入一个“%s”用于替换为行数。行数从0开始。例如“welcome.%s”,则程序会依次向列表中添加“welcome.0”,“welcome.1”...的翻译,直到获取到空值。
|
2019-06-28 04:40:06 +00:00
|
|
|
|
*/
|
2019-10-01 11:17:50 +00:00
|
|
|
|
protected static List<String> translateList(String keyRegular) {
|
2019-06-28 04:40:06 +00:00
|
|
|
|
|
|
|
|
|
List<String> vlist = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
while(true) {
|
2019-06-30 07:26:41 +00:00
|
|
|
|
if(i >= LOOP_MAX_COUNT) break;
|
|
|
|
|
String k = tryFormat(keyRegular, i);
|
2019-06-28 04:40:06 +00:00
|
|
|
|
if(hasKey(k)) {
|
|
|
|
|
vlist.add(translate(k));
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 07:26:41 +00:00
|
|
|
|
return vlist;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将列表翻译中的内容进行format操作。
|
|
|
|
|
* @see #translateList(String)
|
|
|
|
|
*/
|
|
|
|
|
public static String formatList(String keyRegular, Object...format) {
|
2019-08-30 04:17:48 +00:00
|
|
|
|
|
2019-07-07 06:22:20 +00:00
|
|
|
|
return tryFormat(T18nUtils.flattenList(translateList(keyRegular)), format);
|
2019-06-30 07:26:41 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 13:29:10 +00:00
|
|
|
|
public static boolean hasKey(String key) {
|
2019-08-30 04:17:48 +00:00
|
|
|
|
return map.containsKey(key);
|
2019-06-25 13:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 07:12:27 +00:00
|
|
|
|
/* *******************************************************
|
|
|
|
|
*
|
|
|
|
|
* Utilities
|
|
|
|
|
*
|
|
|
|
|
* *******************************************************/
|
|
|
|
|
|
2019-06-26 09:10:15 +00:00
|
|
|
|
private static String reEncode(String bef, Charset charset) {
|
2019-07-10 18:40:25 +00:00
|
|
|
|
byte[] bytes = bef.getBytes(charset);
|
2019-06-26 09:10:15 +00:00
|
|
|
|
return new String(bytes, charset);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 13:29:10 +00:00
|
|
|
|
private static String tryFormat(String context, Object...format) {
|
|
|
|
|
try {
|
|
|
|
|
return String.format(context, format);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|