如果我有资源包属性文件:
A.属性:
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.
Resource Bundle 中没有方法可以做到这一点。另外,我在 Struts,有什么方法可以使用 MessageProperties 进行替换。
最佳答案
您要查找的类是 java.text.MessageFormat;具体来说,调用
MessageFormat.format("{0} This {1} a test", new Object[] {"Yes!!!", "is"});
或
MessageFormat.format("{0} This {1} a test", "Yes!!!", "is");
会回来
"Yes!!! This is a test"
[不幸的是,我无法帮助Struts 连接,虽然this看起来很相关。]
关于java - 来自 Java/Struts 的 ResourceBundle 和替换表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68018/