我们是一个开发用Java编写的软件的团队,使用bundle进行国际化。 我们正在寻找一种分析工具,可以检查在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支付工具或插件,但开源更好。 顺便说一下,这个工具也可用于查找捆绑包中未使用的密钥。
答案 0 :(得分:0)
如果资源文件中缺少您提供的密钥,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);
}
}