gpt4 book ai didi

java - 从代码向 Java 资源包添加新条目

转载 作者:行者123 更新时间:2023-12-03 19:37:55 35 4
gpt4 key购买 nike

我使用 Java 资源包来本地化我的应用程序,并且我创建了一个函数,该函数返回根据代码中的资源包本地化的消息......像这样:

public String getDescription(String code, ResourceBundle resBundle){
String returnValue = null;
try{
returnValue=resBundle.getString(code);
}catch(Exception ex){
returnValue = null;
}
}

但是,我想知道是否可以在资源包中添加一个条目,以防传递的代码不存在,例如:
if(!resBundle.containsKey(code)){
//This next line is pseudo-code... it is not valid at all
resBundle.addEntry(code, "xyz");
}

有什么帮助吗?

最佳答案

您可以尝试使用 Properties类并通过将 ResourceBundle 的键转换为 Stream 来填充它类,因为属性类的功能类似于 Map带有键和值。例如 :

Properties props = new Properties();
resBundle.keySet().stream().forEach(k -> props.put(k, resBundle.getString(k)));

然后你可以使用 getProperty() Properties的方法它返回您在第二个参数中指定的值,以防它找不到指定的键(在您的情况下,键是 code ):
returnValue=props.getProperty(code,"xyz");

关于java - 从代码向 Java 资源包添加新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45583870/

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