gpt4 book ai didi

java - 在 Java 中动态添加带有属性文件消息的字符串?

转载 作者:搜寻专家 更新时间:2023-11-01 03:01:01 24 4
gpt4 key购买 nike

我能够从属性文件中读取消息,我在其中添加了所有带有 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 变量有一个动态生成的字符串值,我必须附加消息。可以将这种类型与属性文件合并吗?

最佳答案

查看有关 Compound Messages: 的教程

就我个人而言,我倾向于用简单的方式来做

 bundle.getString("key").replace("{0}", firstParam);

关于java - 在 Java 中动态添加带有属性文件消息的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com