一个自动查找多语言Resource Bundle的properties文件是否包含有重复项的工具,如果有重复Item,则提示出来,便于删除重复的资源项。包括两个功能:1、查找同一个文件中是否有重复项。2、查找多语言的各Bundle文件中的Item是否完全一致,有否缺漏。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
public class RedundancyChecker {
public final static String filepath_en_US="conf/spring/resources/Vmm_en_US.properties";
public final static String filepath_zh_CN="conf/spring/resources/Vmm_zh_CN.properties";
public final static String filepath_zh_TW="conf/spring/resources/Vmm_zh_TW.properties";
public Properties loadFile(String path)
{
Properties prop = null;
try
{
BufferedInputStream inBuff = new BufferedInputStream(
new FileInputStream(path));
prop = new Properties();
prop.load(inBuff);
inBuff.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return prop;
}
public boolean isKeysTheSameInTwoFiles(String filepath1,String filepath2)
{
boolean result=true;
int count=0;
Properties props1=loadFile(filepath1);
Properties props2=loadFile(filepath2);
Enumeration keys1 = props1.keys();
String fileName1=filepath1.substring(filepath1.lastIndexOf("/")+1);
String fileName2=filepath2.substring(filepath2.lastIndexOf("/")+1);
while(keys1.hasMoreElements())
{
String key1 =(String)keys1.nextElement();
if(!props2.containsKey(key1))
{
System.err.println("ERROR when "+fileName1+"-->"+fileName2);
System.err.println(count+++":key ["+key1+"] in "+fileName1+" not contained in "+fileName2);
result=false;
}
}
if(result==true)
{
System.out.println(fileName1+"-->"+fileName2+" keys the same");
}
return result;
}
public boolean isKeysDuplicateInOneFile(String filepath)
{
boolean result= true;
BufferedReader br= null;
Hashtable hash=new Hashtable();
String filename=filepath.substring(filepath.lastIndexOf("/")+1,filepath.length());
try
{
br= new BufferedReader(new FileReader(filepath));
String line=br.readLine();
while(line!=null )
{
if(line.contains("="))
{
String key =line.substring(0, line.indexOf("="));
if(hash.containsKey(key))
{
System.err.println("ERROR:key ["+key+"] already exists in "+filename);
result=false;
}
else
{
hash.put(key, key);
}
}
line=br.readLine();
}
} catch (Exception e)
{
e.printStackTrace();
}
if(result ==true)
{
System.out.println(filename+": identical keys size:"+hash.size());
}
return result;
}
public boolean checkAll()
{
System.out.println();
boolean b1=isKeysDuplicateInOneFile(filepath_en_US);
boolean b2=isKeysDuplicateInOneFile(filepath_zh_CN);
boolean b3=isKeysDuplicateInOneFile(filepath_zh_TW);
System.out.println();
boolean b4=isKeysTheSameInTwoFiles(filepath_en_US,filepath_zh_CN);
boolean b5=isKeysTheSameInTwoFiles(filepath_en_US,filepath_zh_TW);
boolean b6=isKeysTheSameInTwoFiles(filepath_zh_CN,filepath_en_US);
boolean b7=isKeysTheSameInTwoFiles(filepath_zh_CN,filepath_zh_TW);
boolean b8=isKeysTheSameInTwoFiles(filepath_zh_TW,filepath_en_US);
boolean b9=isKeysTheSameInTwoFiles(filepath_zh_TW,filepath_zh_CN);
System.out.println();
if(b1 &&b2 &&b3 &&b4 &&b5 &&b6 &&b7 &&b8 &&b9)
{
System.out.println("Right.All Files' Keys are THE SAME!");
System.out.println("-------------OK---------------------");
}
else
{
System.out.println("attention:there are some Errors!");
}
return true;
}
public static void main(String[] args)
{
RedundancyChecker aChecker = new RedundancyChecker();
aChecker.checkAll();
}
}

本文介绍了一款用于检查多语言ResourceBundle的properties文件是否存在重复项的工具。该工具能够帮助开发者确保不同语言包的一致性和完整性,避免因资源文件错误导致的应用问题。

被折叠的 条评论
为什么被折叠?



