来自 Java/Struts 的 ResourceBundle 和替换表达式
如果我有一个资源包属性文件:
A.properties:
thekey={0} This is a test
然后我有加载资源包的 java 代码:
ResourceBundle labels = ResourceBundle.getBundle("A", currentLocale);
labels.getString("thekey");
如何用某个值替换 {0} 文本
labels.getString("thekey", "Yes!!!");
使得输出结果为:
Yes!!! This is a test.
没有方法是执行此操作的资源包的一部分。 另外,我在Struts,有什么方法可以使用MessageProperties来进行替换。
If I have a Resource bundle property file:
A.properties:
thekey={0} This is a test
And then I have java code that loads the resource bundle:
ResourceBundle labels = ResourceBundle.getBundle("A", currentLocale);
labels.getString("thekey");
How can I replace the {0} text with some value
labels.getString("thekey", "Yes!!!");
Such that the output comes out as:
Yes!!! This is a test.
There are no methods that are part of Resource Bundle to do this. Also, I am in Struts, is there some way to use MessageProperties to do the replacement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的类是 java.text.MessageFormat; 具体来说,调用
or
将返回
[不幸的是,我无法帮助 Struts 连接,尽管 这看起来很相关。]
The class you're looking for is java.text.MessageFormat; specifically, calling
or
will return
[Unfortunately, I can't help with the Struts connection, although this looks relevant.]
有类 org.apache.struts。 util.MessageResources 具有各种 getMessage 方法,其中一些方法采用参数来插入到实际消息中。
例如。:
There is the class org.apache.struts.util.MessageResources with various methods getMessage, some of them take arguments to insert to the actual message.
Eg.: