1
0
mirror of https://github.com/nitu2003/T18n synced 2024-11-21 23:28:49 +00:00

[+] 添加了从在线网站读取的方法

1. 添加了从在线网站读取的方法
2. 移动了部分方法到 T18nUtils 里
This commit is contained in:
Taskeren 2019-07-01 17:22:54 +08:00
parent 4b01a2e58d
commit b4fa3f0fd0
3 changed files with 187 additions and 79 deletions

View File

@ -1,5 +1,6 @@
package cn.glycol.t18n;
import java.util.ArrayList;
import java.util.HashMap;
public class LanguageMap extends HashMap<String, String> {
@ -10,4 +11,21 @@ public class LanguageMap extends HashMap<String, String> {
return super.getOrDefault(key, key.toString());
}
public ArrayList<String> getAllKeyValuePairs() {
ArrayList<String> list = new ArrayList<String>();
for(Entry<String, String> set : this.entrySet()) {
String key = set.getKey();
String val = set.getValue();
String line;
if(key != null && !key.equals("")) {
line = key + "=" + val;
list.add(line);
}
}
return list;
}
}

View File

@ -5,8 +5,6 @@ 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;
import java.util.List;
@ -18,59 +16,79 @@ public class LanguageMapBuilder {
public static LanguageMap fromFile(File langFile, LanguageMapConfiguration config) {
List<String> lines = getLinesFromFile(langFile);
return getLanguageMapFromLines(config, lines);
List<String> lines = T18nUtils.getLocalContent(langFile);
return processFinal(config, lines);
}
private static List<String> getLinesFromFile(File file) {
if(file.isFile()) {
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 {
return Files.readAllLines(file.toPath(), Charset.forName("UTF-8"));
} catch (Exception e) {
while (true) {
if ((cache = br.readLine()) != null) {
strs.add(cache);
} else {
break;
}
}
return new ArrayList<>();
}
private static LanguageMap getLanguageMapFromLines(LanguageMapConfiguration config, List<String> lines) {
final LanguageMap map = new LanguageMap();
for(String s : lines) {
String[] pair = getLanguageMapFromLine(config, s);
if(!isUnreadable(pair)) {
map.put(pair[0], pair[1]);
}
}
return map;
} catch (IOException e) {
}
private static final String[] UNREADABLE = new String[] {null, null};
return processFinal(config, strs);
private static String[] getLanguageMapFromLine(LanguageMapConfiguration config, String s) {
}
public static LanguageMap fromURL(String url) {
return fromURL(url, LanguageMapConfiguration.DEFAULT);
}
public static LanguageMap fromURL(String url, LanguageMapConfiguration config) {
return processFinal(config, T18nUtils.getOnlineContent(url));
}
private static final String[] UNREADABLE = new String[] { null, null };
/** 处理单行翻译 */
private static String[] process(LanguageMapConfiguration config, String s) {
/* Empty Line */
if(s == null || s.length() == 0) {
if (s == null || s.length() == 0) {
return UNREADABLE;
}
/* Annotation */
if(config.isAnnotation(s)) {
if (config.isAnnotation(s)) {
return UNREADABLE;
}
String[] parts = s.split(config.getSplitter(s), 2);
/* Part fewer than 2 */
if(parts.length < 2) {
return UNREADABLE;
}
/* The Key is Null */
if(parts[0] == null || parts[0].length() == 0) {
if (parts[0] == null || parts[0].length() == 0) {
return UNREADABLE;
}
/* The Value is Null */
if(parts[1] == null) {
if (parts[1] == null) {
parts[1] = "null";
}
@ -82,35 +100,19 @@ public class LanguageMapBuilder {
return s[0] == null;
}
public static LanguageMap fromJarResource(String path) {
return fromJarResource(path, LanguageMapConfiguration.DEFAULT);
}
/** 将处理的结果整合到翻译表中 */
private static LanguageMap processFinal(LanguageMapConfiguration config, List<String> lines) {
public static LanguageMap fromJarResource(String path, LanguageMapConfiguration config) {
final LanguageMap map = new LanguageMap();
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;
for (String s : lines) {
String[] pair = process(config, s);
if (!isUnreadable(pair)) {
map.put(pair[0], pair[1]);
}
}
} catch(IOException e) {
}
return getLanguageMapFromLines(config, strs);
return map;
}

View File

@ -0,0 +1,88 @@
package cn.glycol.t18n;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class T18nUtils {
/** 从在线网站上读取内容 */
public static List<String> getOnlineContent(String _url) {
URL url;
try {
url = new URL(_url);
} catch(MalformedURLException e) {
e.printStackTrace();
return Collections.emptyList();
}
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<String> lines = new ArrayList<String>();
String s;
while((s = br.readLine()) != null) {
lines.add(s);
}
is.close();
br.close();
return lines;
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
}
/** 从本地文件读取内容 */
public static List<String> getLocalContent(File file) {
if (file.isFile()) {
try {
return Files.readAllLines(file.toPath(), Charset.forName("UTF-8"));
} catch (Exception e) {
}
}
return new ArrayList<>();
}
/** 保存翻译表到文件 */
public static void saveLanguageMap(LanguageMap map, File file) {
Objects.requireNonNull(map);
Objects.requireNonNull(file);
try {
String content = I18n.flattenList(map.getAllKeyValuePairs());
if(!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
fw.write(content);
fw.close();
} catch (Exception e) {
System.err.println("Unable to write "+file);
}
}
}