简体   繁体   English

如何检查从ResourceBundle调用的字符串是否存在而不运行?

[英]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: 在末尾:

  • You're able to identify used/unused messages 您可以识别已使用/未使用的邮件
  • You're able to check every messages from every provided bundles 您可以检查每个提供的捆绑软件中的每条消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免Java ResourceBundle字符串中的重复? - How do I avoid repetition in Java ResourceBundle strings? Java:如何在运行时将数据库中的属性添加到R​​esourceBundle? - Java: How do I add properties from a database to a ResourceBundle on runtime? 如何使用来自fxml文档中resourcebundle的字符串填充javafx ChoiceBox - How can I populate a javafx ChoiceBox with strings from a resourcebundle in an fxml document Java 如何检查 ResourceBundle 是否有效(过期或从缓存中清除) - Java how to check if ResourceBundle is valid (expired or cleared from cache) Java ResourceBundle从错误的文件加载字符串 - Java ResourceBundle loads strings from wrong file 我如何找出ResourceBundle的来源? - How can I find out where a ResourceBundle came from? 如何在ResourceBundle中为未翻译的字符串设置默认值? - How to set the default for non translated strings in a ResourceBundle? Java:如何将图像从文件添加到ResourceBundle? - Java: How can I add images from a file into a ResourceBundle? 如何从资源构建路径加载ResourceBundle? - How can I load a ResourceBundle from my resources build path? 如何从 Java 到 sqlite 检索数据以检查数据是否已经存在,如果数据匹配则更新 - How do I retrieve data from Java to sqlite to check if the data has already exist and update if the data matched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM