我们是一个开发用Java编写的软件的团队,使用捆绑包进行国际化。我们正在寻找一种分析工具,可以检查在Java代码中编写的所有键是否都在资源包中。
这是一个很难识别密钥的示例:
Locale locale = new Locale("en", "EN");
ResourceBundle bundle = ResourceBundle.getBundle("MyBundle", locale);
// This is easy to check if the keys are written like this into the Java code.
String myString = bundle.getString("MyKey");
// It's also easy to check this kind of keys
String string1 = "MyKey1";
String string2 = string1;
myString = bundle.getString(string2);
// But it's very hard to check theses keys
String string3 = "MyKey2";
String string4 = string3.toLowerCase();
myString = bundle.getString(string4)
// This one too
String string5 = myfunction();
myString = bundle.getString(string5);我们可以为Netbeans购买工具或插件,但开源更好。顺便说一句,这个工具还可以用来查找捆绑包中未使用的键。
发布于 2013-05-22 17:11:27
如果您提供的密钥在资源文件中丢失,则getString(String key)方法将抛出MissingResourceException。
为了检查Java代码中的所有键是否都存在于资源包中,我建议将它们都包装在一个数组中,遍历该数组,并检查getString(String key)方法是否抛出异常。
String[] keys = new String[]{"key1", "key2"...};
for (String key : keys) {
try {
String value = bundle.getString(key);
System.out.println("Key = " + key + "; Value = " + value);
} catch (MissingResourceException e) {
System.err.println("No value is found for key = " + key);
}
}https://stackoverflow.com/questions/16687663
复制相似问题