From b4fa3f0fd02cfb831b4394d4d7467085d8a0e4b5 Mon Sep 17 00:00:00 2001 From: Taskeren Date: Mon, 1 Jul 2019 17:22:54 +0800 Subject: [PATCH] =?UTF-8?q?[+]=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BB=8E?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=BD=91=E7=AB=99=E8=AF=BB=E5=8F=96=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 添加了从在线网站读取的方法 2. 移动了部分方法到 T18nUtils 里 --- src/main/java/cn/glycol/t18n/LanguageMap.java | 18 ++ .../cn/glycol/t18n/LanguageMapBuilder.java | 160 +++++++++--------- src/main/java/cn/glycol/t18n/T18nUtils.java | 88 ++++++++++ 3 files changed, 187 insertions(+), 79 deletions(-) create mode 100644 src/main/java/cn/glycol/t18n/T18nUtils.java diff --git a/src/main/java/cn/glycol/t18n/LanguageMap.java b/src/main/java/cn/glycol/t18n/LanguageMap.java index 15ef433..68b3005 100644 --- a/src/main/java/cn/glycol/t18n/LanguageMap.java +++ b/src/main/java/cn/glycol/t18n/LanguageMap.java @@ -1,5 +1,6 @@ package cn.glycol.t18n; +import java.util.ArrayList; import java.util.HashMap; public class LanguageMap extends HashMap { @@ -10,4 +11,21 @@ public class LanguageMap extends HashMap { return super.getOrDefault(key, key.toString()); } + public ArrayList getAllKeyValuePairs() { + + ArrayList list = new ArrayList(); + for(Entry 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; + } + } diff --git a/src/main/java/cn/glycol/t18n/LanguageMapBuilder.java b/src/main/java/cn/glycol/t18n/LanguageMapBuilder.java index 3f7b896..3a57298 100644 --- a/src/main/java/cn/glycol/t18n/LanguageMapBuilder.java +++ b/src/main/java/cn/glycol/t18n/LanguageMapBuilder.java @@ -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; @@ -15,103 +13,107 @@ public class LanguageMapBuilder { public static LanguageMap fromFile(File langFile) { return fromFile(langFile, LanguageMapConfiguration.DEFAULT); } - + public static LanguageMap fromFile(File langFile, LanguageMapConfiguration config) { - - List lines = getLinesFromFile(langFile); - return getLanguageMapFromLines(config, lines); + + List lines = T18nUtils.getLocalContent(langFile); + return processFinal(config, lines); } - - private static List getLinesFromFile(File file) { - if(file.isFile()) { - try { - return Files.readAllLines(file.toPath(), Charset.forName("UTF-8")); - } catch (Exception e) { - - } - } - return new ArrayList<>(); - } - - private static LanguageMap getLanguageMapFromLines(LanguageMapConfiguration config, List 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; - - } - - private static final String[] UNREADABLE = new String[] {null, null}; - - private static String[] getLanguageMapFromLine(LanguageMapConfiguration config, String s) { - - /* Empty Line */ - if(s == null || s.length() == 0) { - return UNREADABLE; - } - - /* Annotation */ - if(config.isAnnotation(s)) { - return UNREADABLE; - } - - String[] parts = s.split(config.getSplitter(s), 2); - - /* The Key is Null */ - if(parts[0] == null || parts[0].length() == 0) { - return UNREADABLE; - } - - /* The Value is Null */ - if(parts[1] == null) { - parts[1] = "null"; - } - - return parts; - - } - - private static boolean isUnreadable(String[] s) { - 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) { + if (is == null) { return new LanguageMap(); } - + BufferedReader br = new BufferedReader(new InputStreamReader(is)); - + String cache; List strs = new ArrayList<>(); - + try { - while(true) { - if((cache = br.readLine()) != null) { + while (true) { + if ((cache = br.readLine()) != null) { strs.add(cache); } else { break; } } - } catch(IOException e) { - + } catch (IOException e) { + } - - return getLanguageMapFromLines(config, strs); - + + return processFinal(config, strs); + } + 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) { + return UNREADABLE; + } + + /* Annotation */ + 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) { + return UNREADABLE; + } + + /* The Value is Null */ + if (parts[1] == null) { + parts[1] = "null"; + } + + return parts; + + } + + private static boolean isUnreadable(String[] s) { + return s[0] == null; + } + + /** 将处理的结果整合到翻译表中 */ + private static LanguageMap processFinal(LanguageMapConfiguration config, List lines) { + + final LanguageMap map = new LanguageMap(); + + for (String s : lines) { + String[] pair = process(config, s); + if (!isUnreadable(pair)) { + map.put(pair[0], pair[1]); + } + } + + return map; + + } + } diff --git a/src/main/java/cn/glycol/t18n/T18nUtils.java b/src/main/java/cn/glycol/t18n/T18nUtils.java new file mode 100644 index 0000000..565e199 --- /dev/null +++ b/src/main/java/cn/glycol/t18n/T18nUtils.java @@ -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 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 lines = new ArrayList(); + 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 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); + } + + } + +}