如果我有一个资源包属性文件:

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.

资源包中没有任何方法可以执行此操作。另外,我在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 连接,尽管 看起来相关。]

其他提示

有班级 org.apache.struts.util.MessageResources 使用各种 getMessage 方法,其中一些方法采用参数来插入到实际消息中。

例如。:

messageResources.getMessage("thekey", "Yes!!!");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top