1
0
mirror of https://github.com/nitu2003/T18n synced 2024-10-31 17:38:48 +00:00
T18n/src/main/java/cn/glycol/t18n/I18n.java

50 lines
1.1 KiB
Java
Raw Normal View History

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-25 13:29:10 +00:00
public class I18n {
private static LanguageMap map;
2019-06-26 09:10:15 +00:00
private static Charset charset;
static {
charset = Charset.forName(System.getProperty("file.encoding"));
}
2019-06-25 13:29:10 +00:00
public static void setLanguageMap(LanguageMap map) {
I18n.map = map;
}
2019-06-26 09:10:15 +00:00
public static void setEncoding(String charset) {
I18n.charset = Charset.forName(charset);
}
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(
reEncode(getLanguageMapSafe().get(key), charset),
format);
2019-06-25 13:29:10 +00:00
}
public static boolean hasKey(String key) {
return getLanguageMapSafe().containsKey(key);
}
2019-06-26 09:10:15 +00:00
private static String reEncode(String bef, Charset charset) {
byte[] bytes = bef.getBytes();
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;
}
}
private static LanguageMap getLanguageMapSafe() {
return map == null ? new LanguageMap() : map;
}
}