1

We are a team who develop a software written in Java, using bundles for internationalization. We are looking for an analysis tool which can check if all the keys written in the Java Code are in the resource bundle.

This is an example about how it's hard to identify the keys :

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);

We can pay for a tool or for a plugin for Netbeans but open source is better. Btw this tool could also be used to find unused keys in the bundle.

4

1 回答 1

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);
    }
}
于 2013-05-22T09:11:27.247 回答