gpt4 book ai didi

java - ResourceBundle同时读取2个Properties文件

转载 作者:行者123 更新时间:2023-12-01 15:41:23 26 4
gpt4 key购买 nike

在我的项目中,我有 2 properties files用于国际化。我用ResourceBundleLocale参数,我将属性文件中的键存储在集合中。不幸的是,集合中存储了两个文件的组合 key 。我只想要来自单个文件的 key ,具体取决于区域设置。就我而言,区域设置是“bg_BG”。属性文件是:

time_intervals.properties

enter image description here

time_intervals_bg.properties

enter image description here

这就是我阅读它们的方式:

public List<SelectItem> getTimeSpentList() {

timeSpentList = new ArrayList<SelectItem>();

FacesContext context = FacesContext.getCurrentInstance();

ResourceBundle bundle = ResourceBundle.getBundle("properties.time_intervals", context.getViewRoot().getLocale());

Enumeration<String> time_interval_keys = bundle.getKeys();

List<String> sortedKeys = new ArrayList<String>();

while(time_interval_keys.hasMoreElements()) {
String key = time_interval_keys.nextElement();
sortedKeys.add(key);
}

Collections.sort(sortedKeys, new Comparator<String>() {

@Override
public int compare(String o1, String o2) {
if (o1.charAt(1) != ' ') {
return -1;
} else if (o2.charAt(1) != ' ') {
return 1;
}

return o1.compareTo(o2);
}
});
for (String key : sortedKeys) {
timeSpentList.add(new SelectItem(key));
}

if (timeSpentList == null || timeSpentList.isEmpty()) {
timeSpentList.add(new SelectItem(""));
return timeSpentList;
}
return timeSpentList;
}

这里的问题是在 Enumeration<String> time_interval_keys 中调用 bundle.getKeys() 后,我从两个属性文件中获取组合键但我只想要其中之一的值。请帮忙。

附注如果我的解释和代码有任何不清楚的地方,请告诉我。

最佳答案

您没有正确使用ResourceBundle系统。

每个属性文件应包含相同的键(或更准确地说是基本属性文件中声明的键的子集)。当您请求键的值时(或者像您一样列出键/值时),ResourceBundle 会尝试在最精确的属性文件中查找键,默认为默认属性文件。

如果属性文件中的键不同,则这些键被视为不同。

关于java - ResourceBundle同时读取2个Properties文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7998661/

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