基于JAVA环境的实现多语种翻译JSON文件的同步与排序
程序员文章站
2022-04-29 14:05:59
...
在web开发中,需要提供多国语言的json静态文件,前期开发还好,后期维护上就会麻烦,而且比较乱,闲暇之余,用java写了个插件。直接放代码(丑陋的代码勿喷
package emrApp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import javax.swing.Spring;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.json.JSONStringer;
public class TakeJson {
static String strInput = "";
@SuppressWarnings("unchecked")
public static boolean main() throws IOException {
Boolean result = false;
File file = new File("F:/数据simple/zh-cn.json");
JSONObject jsonObject = getInputStream(file);
List nameList = jsonObject.names().toList();
//快速排序法
nameList.sort(new Comparator<String>() {
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
if(o1.compareTo(o2) > 0) {
return 1;
}
if(o1.compareTo(o2) == 0) {
return 0;
}
return -1;
}
});
result = outputStream(file, nameList, jsonObject);// 中文。JSON
File enFile = new File("F:/数据simple/en.json");
JSONObject EngObject = getInputStream(enFile);
result = outputStream(enFile, nameList, EngObject);
return result;
}
private static Boolean outputStream(File file, List nameList, JSONObject jsonObject) throws IOException, JSONException {
String outJson = "{\n ";
for (Object object : nameList) {
String key = object.toString();
Set<String> keys = jsonObject.keySet();
String value = key;
if(keys.contains(key)) {
value = (String) jsonObject.get(key);
}
outJson += compileStirng(key, ": ") + compileStirng(value, ",\n ");
}
outJson += "}";
OutputStream outF = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(outF, "UTF-8");
writer.write(outJson.replace(",\n }", "\n}"));
writer.close();
return true;
}
private static JSONObject getInputStream(File file) throws IOException {
InputStream inF = null;
try {
inF = new FileInputStream(file);
}catch(FileNotFoundException e){
System.out.println(e);
OutputStream outF = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(outF, "UTF-8");
writer.write("{\"name\": \"english\"}");
writer.close();
inF = new FileInputStream(file);
}
// 锟斤拷锟斤拷InputStreamReader锟斤拷锟斤拷,锟斤拷锟斤拷锟斤拷写锟斤拷锟斤拷同
InputStreamReader reader = new InputStreamReader(inF, "UTF-8");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
char a = (char) reader.read();
sb.append(a);
// 转锟斤拷char锟接碉拷StringBuffer锟斤拷锟斤拷锟斤拷
}
inF.close();
String strInput = sb.toString();
if (TakeJson.strInput.length() == 0) {
TakeJson.strInput = strInput;
}
JSONObject jsonObject=new JSONObject(strInput);
return jsonObject;
}
private static String compileStirng(String key, String string) {
return "\"" + key + "\"" + string;
}
}
)
上一篇: SEO优化:如何抓取手机网站图片
下一篇: 计划很美好