国际化工具:properties属性文件以及json文件的读取、修改值内容、保存。
读取和修改都是基于key-value的:方便理解和修改;具备properties文件<->json文件互相转换的能力。
在实际项目中还用到properties属性文件或json文件和excel文件的相互转换的。
关于国际化,还需要考虑去重(一个词条在系统中多种不同的翻译会显得很不专业,一个词条重复翻译浪费翻译费用),考虑专业术语的维护,词条的合理复用,不同语言的条目的一致性,字符宽度检测(避免界面变形),语法的一致性等。
一、属性文件properties的读取、修改值内容、保存
package tool.i18n;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
/**
* 属性文件properties的读取、修改值内容、保存
* @author 陈小稳 csdn ID:ccxw1983
*
*/
public class PropertyFileModify {
public static void main(String[] args) throws IOException {
String currentFilePath = "D:\\workspace123\\demo.json\\test_files\\unicode.properties";
String saveFilePath = "D:\\workspace123\\demo.json\\test_files\\unicode_new.properties";
HashMap<String, String> modifyKeyVals = new HashMap<String, String>();
{
modifyKeyVals.put("a.b.c", "newval");
}
modifyAndNewFile(currentFilePath, saveFilePath, modifyKeyVals, "【有待翻译】");
}
public static void modifyAndNewFile(String currentFilePath, String saveFilePath,
HashMap<String, String> modifyKeyVals, String defaultVal) throws IOException {
File file = new File(currentFilePath);
ArrayList<String> lines = FileUtils2.readFile2Lines(file, FileUtils2.encoding);
HashMap<String, String> currentMap = getCurrentKeyVals(lines);
Boolean setDefaultVal = StringUtils.isNotBlank(defaultVal);
if (currentMap.size() > modifyKeyVals.size()) {
System.out.println("json文件key-val数目:" + currentMap.size() + "> 修改条目:" + modifyKeyVals.size());
// 设置默认值
if (setDefaultVal) {
for (Iterator iterator = currentMap.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
if (!modifyKeyVals.containsKey(key)) {
// System.out.println("没有条目的翻译:" + key);
modifyKeyVals.put(key, defaultVal);
}
}
}
}
// 修改文件
StringBuffer newcontent = new StringBuffer();
for (int i = 0; i < lines.size(); i++) {
String line = StringUtils.trimToEmpty(lines.get(i));
if (line.startsWith("#")) {
newcontent.append(line);
newcontent.append("\r\n");
continue;
} else {
String[] keyvalarr = line.split("=");
String key = StringUtils.trimToEmpty(keyvalarr[0]);
if(modifyKeyVals.containsKey(key)){
String val = modifyKeyVals.get(key);
val = utf82unicode(val);
newcontent.append(key);
newcontent.append("=");
newcontent.append(val);
newcontent.append("\r\n");
}else{
newcontent.append(line);
newcontent.append("\r\n");
}
}
}
// 保存为文件
File newfile = new File(saveFilePath);
if (newfile.exists()) {
newfile.delete();
}
FileUtils.writeStringToFile(newfile, newcontent.toString(), "UTF-8");
}
private static HashMap<String, String> getCurrentKeyVals(ArrayList<String> lines) {
HashMap<String, String> currentKeyVals = new HashMap<String, String>();
for (int i = 0; i < lines.size(); i++) {
String line = StringUtils.trimToEmpty(lines.get(i));
if (line.startsWith("#")) {
continue;
} else {
String[] keyvalarr = line.split("=");
String key = StringUtils.trimToEmpty(keyvalarr[0]);
String val = StringUtils.trimToEmpty(keyval