您的位置:首页 > 移动开发 > Android开发

Android 比对国际化资源文件中的翻译缺失

2017-03-05 08:25 330 查看
因为app要做英文,繁体,中文,目前三种翻译。然后目前还在用eclipse开发,导致有的时候编译器并没有提示哪些有缺失翻译。

又经常新增。导致有的字段可能有漏掉。不仅仅是研发会漏掉,也有可能是翻译人员会漏掉。所以利用闲暇时间,打了个jar包。

可以直接使用 AndroidCompare.jar

目前可以比对两种文件。string.xml和array.xml



一共需要三个参数。第一个是要比对文件的类型,string,或者array

然后后面两个参数就是文件的路径,

结果就是比较两个文件的不同。string 会直接把缺失的打印出来。

array会把key对应的item个数打印出来。因为没办法对内容校对。所以就只有对item个数校对。

代码很简单。这里就不上传了。直接贴出来。

public class CompareClass {

static String pathCh = "D:\\translate\\array.xml";
static String pathEn = "D:\\translate\\array2.xml";
static String pathStringWrong = "D:\\strings_wrong.txt";
static String pathArrayWrong = "D:\\array_wrong.txt";
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("\n" + "1.第一个参数string 或者是 array\n" + "2.然后需要两个文件的路径");
return;
}

String type = args[0];
pathCh = args[1];
pathEn = args[2];

if ("string".equalsIgnoreCase(type)) {
List<String> listInput = new ArrayList<String>();
List<String> listOut = new ArrayList<String>();
List<String> listWrong = new ArrayList<String>();
listInput = listKey(pathCh);
listOut = listKey(pathEn);

String str = pathEn + "缺失以下----\n";
listWrong.add(str);
List<String> listTemp = list1ComList2(listInput, listOut);
if (listTemp != null) {
listWrong.addAll(listTemp);
}
String str2 = pathCh + "缺失以下----\n";
listWrong.add(str2);
listTemp = list1ComList2(listOut, listInput);
if (listTemp != null) {
listWrong.addAll(listTemp);
}

writeWrong(listWrong);
System.out.println("*****************以下是有区别的地方**并已经写入***" + pathStringWrong);
for (int i = 0; i < listWrong.size(); ++i) {
System.out.println(listWrong.get(i));
}
} else if ("array".equalsIgnoreCase(type)) {
List<ArrayBean> listInput = new ArrayList<ArrayBean>();
List<ArrayBean> listOut = new ArrayList<ArrayBean>();
List<ArrayBean> listWrong = new ArrayList<ArrayBean>();
listInput = listArrayKey(pathCh);
listOut = listArrayKey(pathEn);

ArrayBean ab = new ArrayBean();
ab.setStrCount(-1);
ab.setStrKey(pathEn + "两个文件不同有下----\n");
listWrong.add(ab);
List<ArrayBean> listTemp = list1ComArrayList2(listInput, listOut);

if (listTemp != null) {
listWrong.addAll(listTemp);
}
ArrayBean ab2 = new ArrayBean();
ab2.setStrCount(-1);
ab2.setStrKey(pathCh + "两个文件不同有下----\n");
listWrong.add(ab2);
listTemp = list1ComArrayList2(listOut, listInput);
if (listTemp != null) {
listWrong.addAll(listTemp);
}
System.out.println("*****************以下是有区别的地方**并已经写入***" + pathArrayWrong+"\n\n");
for (int i = 0; i < listWrong.size(); ++i) {
if(listWrong.get(i).getStrCount() == -1){
System.out.println(listWrong.get(i).strKey);
}
else{
System.out.println(listWrong.get(i).toString());
}
}
writeArrayWrong(listWrong);

}

System.out.println("\n\n\n****finish******\n\n\n");

}

// 返回第二个文件的缺失
public static List<ArrayBean> list1ComArrayList2(List<ArrayBean> list1, List<ArrayBean> list2) {
if (list1 == null || list1.isEmpty()) {
return null;
}
if (list2 == null || list2.isEmpty()) {
return list1;
}

List<ArrayBean> listWrong = new ArrayList<ArrayBean>();
boolean flag = false;
for (int i = 0; i < list1.size(); ++i) {
flag = false;
for (int j = 0; j < list2.size(); ++j) {
if (list1.get(i).equals(list2.get(j))) {
flag = true;
break;
}
}
if (!flag) {
listWrong.add(list1.get(i));
}
}

return listWrong;

}

// 返回的是第二个文件缺失的list2
public static List<String> list1ComList2(List<String> list1, List<String> list2) {
if (list1 == null || list1.isEmpty()) {
return null;
}
if (list2 == null || list2.isEmpty()) {
return list1;
}
List<String> listWrong = new ArrayList<String>();
boolean flag = false;
for (int i = 0; i < list1.size(); ++i) {
flag = false;
for (int j = 0; j < list2.size(); ++j) {
if (list1.get(i).equals(list2.get(j))) {
flag = true;
break;
}
}
if (!flag) {
listWrong.add(list1.get(i));
}
}
return listWrong;
}

public static void writeArrayWrong(List<ArrayBean> listWrong) {

try {

OutputStreamWriter writerStream =
new OutputStreamWriter(new FileOutputStream(pathArrayWrong), "UTF-8");
BufferedWriter writer = new BufferedWriter(writerStream);

for (int i = 0; i < listWrong.size(); i++) {
if(listWrong.get(i).strCount == -1){
writer.write(listWrong.get(i).strKey + "\n\r\n");
}
else{
writer.write(listWrong.get(i).toString() + "\n\r\n");
}

}

writer.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void writeWrong(List<String> listWrong) {

try {
OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(pathStringWrong), "UTF-8");
BufferedWriter writer = new BufferedWriter(writerStream);
for (int i = 0; i < listWrong.size(); i++) {
writer.write(listWrong.get(i) + "\n\r\n");

}
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static List<ArrayBean> listArrayKey(String path) {
List<ArrayBean> listInput = new ArrayList<ArrayBean>();
InputStreamReader isr = null;
BufferedReader reader = null;
String tempString = null;
// 一次读入一行,直到读入null为文件结束
try {
isr = new InputStreamReader(new FileInputStream(path), "UTF-8");
reader = new BufferedReader(isr);
ArrayBean ab = null;
while ((tempString = reader.readLine()) != null) {
// 显示行号

tempString = tempString.trim();
if (tempString.startsWith("<!--")) {
// 注释
continue;
} else if (tempString.startsWith("<string-array name=\"")) {
ab = new ArrayBean();
tempString = tempString.replace("<string-array name=\"", "");
String[] strs = tempString.split("\">");
if (strs != null && strs.length > 0) {
ab.setStrKey(strs[0]);
}
} else if (tempString.contains("<item>")) {
if(ab != null){
ab.countAdd();
}
} else if (tempString.contains("</string-array>")) {
if(ab != null){

listInput.add(ab);
}
}
}
} catch (Exception e) {
System.out.println(" exception " + e.getMessage());
}

return listInput;
}

public static List<String> listKey(String path) {
List<String> listInput = new ArrayList<String>();
InputStreamReader isr = null;
BufferedReader reader = null;
String tempString = null;
// 一次读入一行,直到读入null为文件结束
try {
isr = new InputStreamReader(new FileInputStream(path), "UTF-8");
reader = new BufferedReader(isr);
while ((tempString = reader.readLine()) != null) {
// 显示行号
tempString = tempString.trim();
if (tempString.startsWith("<!--")) {
// 注释
continue;
} else if (tempString.startsWith("<string name=\"")) {
tempString = tempString.replace("<string name=\"", "");
String[] strs = tempString.split("\">");
if (strs != null && strs.length > 0) {
listInput.add(strs[0]);
}

}
}
} catch (Exception e) {
System.out.println(" exception " + e.getMessage());
}
return listInput;
}

}


public class ArrayBean {

String strKey ;
int strCount;
public String getStrKey() {
return strKey;
}
public void setStrKey(String strKey) {
this.strKey = strKey;
}
public int getStrCount() {
return strCount;
}
public void setStrCount(int strCount) {
this.strCount = strCount;
}
public void countAdd(){
strCount++;
}

@Override
public boolean equals(Object arg0) {
if(arg0 == null){
return false;
}
if(arg0 == this){
return true;
}
if(arg0 instanceof ArrayBean){
ArrayBean ab = (ArrayBean) arg0;
if(this.strKey.equals(ab.strKey) && this.strCount == ab.strCount){
return true;
}
}
return false;
}
@Override
public String toString() {
return "key = "+strKey+" item 个数 "+strCount;

}
}


好了这是晚上自己临时赶出来的。第二天就使用了。然后他们说要是根据第二个文件的顺序,将两个资源文件排序挺好。我也觉得建议不错。但是现在好像没用到。就难得写了。看哪天有心情在搞。嘿嘿 T_~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息