Jai*_*cia 4 java configuration user-interface
将用户消息存储在配置文件中,然后在整个应用程序中检索某些事件的最佳实践是什么?
我正在考虑使用1个单独的配置文件,例如
REQUIRED_FIELD = {0} is a required field
INVALID_FORMAT = The format for {0} is {1}
Run Code Online (Sandbox Code Playgroud)
等等,然后从类似这样的类中调用它们
public class UIMessages {
public static final String REQUIRED_FIELD = "REQUIRED_FIELD";
public static final String INVALID_FORMAT = "INVALID_FORMAT";
static {
// load configuration file into a "Properties" object
}
public static String getMessage(String messageKey) {
//
return properties.getProperty(messageKey);
}
}
Run Code Online (Sandbox Code Playgroud)
这是解决这个问题的正确方法还是已经有一些事实上的标准?
您将消息放入属性文件中是正确的.如果您使用Java,Java会非常简单ResourceBundle.您基本上创建一个属性文件,其中包含您要支持的每个语言环境的消息字符串(messages_en.properties,messages_ja.properties),并将这些属性文件捆绑到您的jar中.然后,在您的代码中,您提取消息:
ResourceBundle bundle = ResourceBundle.getBundle("messages");
String text = MessageFormat.format(bundle.getString("ERROR_MESSAGE"), args);
Run Code Online (Sandbox Code Playgroud)
加载捆绑包时,Java将确定您正在运行的区域设置并加载正确的消息.然后,将args与消息字符串一起传入并创建本地化消息.
| 归档时间: |
|
| 查看次数: |
2728 次 |
| 最近记录: |