测试 MessageFormat .properties 文件中的 null 参数
使用资源包和 MessageFormat 是否可以得到以下结果?
- 当我调用
getBundle("message.07", "test")
来获取"Group test"
- 当我调用
getBundle("message.07", null 时)
获取“未选择组”
我在互联网上找到的每个示例都涉及行星、磁盘上的文件等等。
我只需要检查资源包属性文件中的一个参数是否为 null
(或不存在)。我希望为 null 参数找到一种特殊格式,例如 {0,choice,null#No group selected|notnull#Group {0}}
。
我用来获取捆绑包的方法是:
public String getBundle(String key, Object... params) {
try {
String message = resourceBundle.getString(key);
if (params.length == 0) {
return message;
} else {
return MessageFormat.format(message, params);
}
} catch (Exception e) {
return "???";
}
}
我也为其他捆绑包调用此方法,例如
getBundle("message.08", 1, 2)
=>“第 1 页,共 2 页”
(始终为参数,无需检查null
)getBundle("message.09")
=>;“打开文件”
(无参数,无需检查null
)
我应该在 .properties 文件中为 message.07
编写什么有描述结果吗?
我现在拥有的是:
message.07=Group {0}
message.08=Page {0} of {1} # message with parameters where I always send them
message.09=Open file # message without parameters
Is it possible, with resource bundles and MessageFormat to have the following result?
- when I call
getBundle("message.07", "test")
to get"Group test"
- when I call
getBundle("message.07", null)
to get"No group selected"
Every example I found on the Internet is with planets, with files on the disk and so on.
I only need to check if one parameter is null
(or doesn't exist) in the resource bundle's properties file. I hope to find a special format for the null parameter something like {0,choice,null#No group selected|notnull#Group {0}}
.
The method I use to get the bundles is:
public String getBundle(String key, Object... params) {
try {
String message = resourceBundle.getString(key);
if (params.length == 0) {
return message;
} else {
return MessageFormat.format(message, params);
}
} catch (Exception e) {
return "???";
}
}
I also call this method for other bundles, like
getBundle("message.08", 1, 2)
=>"Page 1 of 2"
(always parameters, no need to check fornull
)getBundle("message.09")
=>"Open file"
(no parameters, no need to check fornull
)
What should I write in my .properties file for message.07
to have the result described?
What I have now is:
message.07=Group {0}
message.08=Page {0} of {1} # message with parameters where I always send them
message.09=Open file # message without parameters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要尝试更改捆绑包功能(即使您有一个封装它的 getBundle 方法)。
只需在您的代码中执行以下操作:
或者创建另一种方法:
确实如此
I'll recommend not trying to change the bundle functionality (even if you have a getBundle method encapsulating it).
Simply do in your code:
Or make another method:
that does
您的
.properties
文件,然后您需要更改代码以对
params
进行显式检查null
。如果null
,那么您可以执行resourceBundle.getString(NULL_MSG)
之类的操作。其中NULL_MSG
就是这样,所以,现在你原来的方法将变成这样。
像下面这样调用我的方法,
现在告诉我问题出在哪里?
Your
.properties
file,And then you need to change your code to put an explicit check
params
fornull
. And ifnull
then you can do something likeresourceBundle.getString(NULL_MSG)
. WhereNULL_MSG
will be this,So, now your original method would become something like this.
Calling my method like below,
Now tell me where is the problem?