[英]How do I check if strings called from a ResourceBundle exist without running?
I'm using ResourceBundle to get strings from MessagesBundle.properties files, but I can't figure out a way to check to make sure all strings called exist (and aren't mistyped) without actually running the program and looking through exceptions. 我正在使用ResourceBundle从MessagesBundle.properties文件中获取字符串,但是我想不出一种方法来检查所有被调用的字符串是否存在(并且不会键入错误),而无需实际运行程序并查看异常。 Is there a tool that lets me check this? 有没有可以让我检查的工具?
You should use one or more classes where entries of your properties files are mapped with public static attributes. 您应该使用一个或多个类,这些类中的属性文件条目是使用公共静态属性映射的。 Like this everything is checked at compilation time and you avoid later random/unexpected behavior/error. 这样,在编译时便会检查所有内容,从而避免以后出现随机/意外行为/错误。
public class Messages {
public static final String cancel = "cancel";
}
...
Locale locale = new Locale("en", "US");
ResourceBundle labels = ResourceBundle.getBundle("MyBundle", locale);
System.out.println(labels.getString(Messages.cancel));
...
For testing purpose, you can iterate (via reflection) over attributes of the Messages
class and try to load messages from bundles. 出于测试目的,您可以迭代(通过反射) Messages
类的属性,并尝试从包中加载消息。
At the end: 在末尾:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.