1
0
mirror of https://github.com/nitu2003/T18n synced 2024-11-24 16:58:47 +00:00

[+] 添加列表读取&添加几个方法

This commit is contained in:
Taskeren 2019-06-28 12:40:06 +08:00
parent b06fc3f1a3
commit ce04f7487e

View File

@ -1,6 +1,8 @@
package cn.glycol.t18n; package cn.glycol.t18n;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class I18n { public class I18n {
@ -19,16 +21,59 @@ public class I18n {
I18n.charset = Charset.forName(charset); I18n.charset = Charset.forName(charset);
} }
/** 自动翻译translate后再执行格式化format */
public static String format(String key, Object...format) { public static String format(String key, Object...format) {
return tryFormat( return tryFormat(
reEncode(getLanguageMapSafe().get(key), charset), translate(key),
format); 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
*/
public static String format2(String key, Object...format) {
List<String> vlist = new ArrayList<String>();
int i = 0;
int max = 1000;
while(true) {
if(i >= max) break;
String k = tryFormat(key, i);
if(hasKey(k)) {
vlist.add(translate(k));
} else {
break;
}
i++;
}
String v = "";
for(String o : vlist) {
v += o + "\n";
}
v = v.trim();
v = tryFormat(v, format);
return v;
}
public static boolean hasKey(String key) { public static boolean hasKey(String key) {
return getLanguageMapSafe().containsKey(key); return getLanguageMapSafe().containsKey(key);
} }
public static boolean canTranslate(String key) {
return hasKey(key);
}
private static String reEncode(String bef, Charset charset) { private static String reEncode(String bef, Charset charset) {
byte[] bytes = bef.getBytes(); byte[] bytes = bef.getBytes();
return new String(bytes, charset); return new String(bytes, charset);