1
0
mirror of https://github.com/nitu2003/T18n synced 2024-11-21 15:18:47 +00:00

[+] 编码控制功能

This commit is contained in:
Taskeren 2019-06-26 17:10:15 +08:00
parent 574e37e9f7
commit b06fc3f1a3
2 changed files with 55 additions and 1 deletions

View File

@ -1,21 +1,39 @@
package cn.glycol.t18n;
import java.nio.charset.Charset;
public class I18n {
private static LanguageMap map;
private static Charset charset;
static {
charset = Charset.forName(System.getProperty("file.encoding"));
}
public static void setLanguageMap(LanguageMap map) {
I18n.map = map;
}
public static void setEncoding(String charset) {
I18n.charset = Charset.forName(charset);
}
public static String format(String key, Object...format) {
return tryFormat(getLanguageMapSafe().get(key), format);
return tryFormat(
reEncode(getLanguageMapSafe().get(key), charset),
format);
}
public static boolean hasKey(String key) {
return getLanguageMapSafe().containsKey(key);
}
private static String reEncode(String bef, Charset charset) {
byte[] bytes = bef.getBytes();
return new String(bytes, charset);
}
private static String tryFormat(String context, Object...format) {
try {
return String.format(context, format);

View File

@ -1,6 +1,10 @@
package cn.glycol.t18n;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
@ -78,4 +82,36 @@ public class LanguageMapBuilder {
return s[0] == null;
}
public static LanguageMap fromJarResource(String path) {
return fromJarResource(path, LanguageMapConfiguration.DEFAULT);
}
public static LanguageMap fromJarResource(String path, LanguageMapConfiguration config) {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
if(is == null) {
return new LanguageMap();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String cache;
List<String> strs = new ArrayList<>();
try {
while(true) {
if((cache = br.readLine()) != null) {
strs.add(cache);
} else {
break;
}
}
} catch(IOException e) {
}
return getLanguageMapFromLines(config, strs);
}
}