java国际化key抽取 package com.test;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.HashSet;import java.util.Set;public class SearchFile {public static S
package com.test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class SearchFile { public static Set国际化资源文件读取mainList = new HashSet (); public static void main(String[] args) { String filepath = "/Users/chentengteng/Documents/idea/vswork/site-module/webapp-vswork-api"; readfile(filepath); String filepath1 = "/Users/chentengteng/Documents/idea/vswork/vswork-core-module"; readfile(filepath1); String filepath2 = "/Users/chentengteng/Documents/idea/vswork/oss-ali-module"; readfile(filepath2); for(String st:mainList){ System.out.println(st+":\"\","); } // // String s = "lang(\"112312 sdjfalsdjf 3\") lang(\"21都焕发健康的话\")lang(\"3深度研发 iu 阿萨德引发 iu 是的\")"; // getWorld(s); } public static void readfile(String filepath){ File file = new File(filepath); if(file.isDirectory()){ String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "/" + filelist[i]); if (!readfile.isDirectory()) { if(isJavaFile(readfile.getPath())){ //System.out.println("path=" + readfile.getPath()); search(readfile); } } else if (readfile.isDirectory()) { //System.out.println("path=" + readfile.getPath()); readfile(readfile.getPath()); } } } } public static boolean isJavaFile(String filepath){ File file = new File(filepath); String fileName = file.getName(); if(fileName.indexOf(".java")>1){ return true; } return false; } public static String mainKey="lang("; public static void search(File file){ try{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String s; try{ while((s = br.readLine())!=null){ if(s.contains(mainKey)){ getWorld(s); } } }catch(Exception e){ e.printStackTrace(); } br.close(); }catch(Exception e){ e.printStackTrace(); } } public static void getWorld(String s){ String before="lang("; String after=")"; if(s.contains(before)){ int start = s.indexOf(before)+5; String subStr1 = s.substring(start); int end = subStr1.indexOf(after); String mainStr = subStr1.substring(0,end); //System.out.println(mainStr+":\"\","); mainList.add(mainStr); //String subStr = s.substring(end); if(subStr1.contains(before)){ getWorld(subStr1); } }else{ return; } } }
package com.glarysea.vswork.common.language; import com.glarysea.vswork.common.utils.GsonUtil; import com.glarysea.vswork.common.utils.SessionUtil; import java.io.*; import java.util.HashMap; import java.util.Map; /** * Created by chentengteng on 2017/8/29. */ public class Lang { private static String language = "cn"; private static MapGosn处理json国际化资源对象languageMap = null; public static String lang(String key){ String currentLang = SessionUtil.getLanuage(); // String currentLang = "cn"; if(currentLang.equalsIgnoreCase("cn")){ return key; } if(!currentLang.equals(language)){ language = currentLang; try{ BufferedReader br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("language/" + language + ".json"), "UTF-8")); String s; StringBuffer sb = new StringBuffer(); try{ s=br.readLine(); while(s!= null && s.length()!=0){ sb.append(s); s=br.readLine(); } if(sb.length()!= 0){ System.out.println("读取国际化文件"); languageMap = new GsonUtil ().JSONToObject(sb.toString(),HashMap.class); } }catch(Exception e){ e.printStackTrace(); } br.close(); }catch(Exception e){ e.printStackTrace(); } } return languageMap.get(key); } /** * 读取流 * * @param inStream * @return 字节数组 * @throws Exception */ public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } public static void main(String[] args) { System.out.println(lang("你好")); } }
package com.glarysea.vswork.common.utils; import com.glarysea.vswork.common.token.Token; import com.google.gson.Gson; /** * @author ctt */ public class GsonUtil{ private static Gson gson = new Gson(); /** *将对象格式化 * @param obj 格式化对象 * @return */ public static String toJson(Object obj){ return gson.toJson(obj); } public T JSONToObject(String json, Class beanClass) { Gson gson = new Gson(); T res = gson.fromJson(json, beanClass); return res; } }