我能够从属性文件中读取消息,其中我添加了key/value
对
message.template.success.equipment.create = Equipment template created successfully
message.template.success.equipment.create.termination = Termination template created successfully
message.template.success.equipment.update = Equipment template updated successfully
message.template.success.termination.update = Termination template updated successfully
从属性文件中读取消息我创建了一个包含以下代码的类
public class PropertyReader {
private static PropertyReader instance = null;
private ResourceBundle bundleResource;
private ResourceBundle bundleResourceMenu;
private ResourceBundle bundleResourceMessage;
private static final Logger log = Logger.getLogger(PropertyReader.class);
private PropertyReader() {}
private PropertyReader(Locale locale) {
log.info("Property Reader loading files with locale : "+locale.getLanguage());
bundleResourceMenu = ResourceBundle.getBundle("bsm-portal-menu",locale);
bundleResource = ResourceBundle.getBundle("bsmResource",locale);
bundleResourceMessage = ResourceBundle.getBundle("bsm-portal-message",locale);
log.info("**** BundleResourceMessage *****");
}
public static synchronized PropertyReader getInstance(Locale locale) {
if (instance == null)
instance = new PropertyReader(locale);
return instance;
}
public String getBundleResource(String propKey) {
return this.bundleResource.getString(propKey);
}
public String getBundleResourceMenu(String propKey) {
return this.bundleResourceMenu.getString(propKey);
}
public String getBundleResourceMessage(String propKey) {
return this.bundleResourceMessage.getString(propKey);
}
}
这一切都很好。
现在我想让这些类型的消息动态
message = "Entities " + appenMessage.toString() + " are already added in another association";
这里appenMessage
变量有一个动态生成的String值,我必须附加message.It是否可以将这种类型与属性文件合并?
答案 0 :(得分:2)
答案 1 :(得分:1)
java.text.MessageFormat.format(pattern, argument…)
功能仅用于此目的。
在属性文件中:
message.template.entities.associated = Entities {0} are already added in another association
在您的代码中:
MessageFormat.format(getBundleResourceMessage("message.template.entities.associated"), appenMessage)
这会导致appenMessage.toString()
替换{0}
占位符。 MessageFormat
类还支持更高级别的占位符变体,以支持您在国际化中遇到的问题,例如复数化和日期格式化。