在我的项目中,我有 2 个properties files
用于国际化。我使用ResourceBundle
withLocale
参数并将属性文件中的键存储在集合中。不幸的是,在集合中存储了来自两个文件的组合键。我只想要来自单个文件的密钥,具体取决于语言环境。在我的情况下,语言环境是“bg_BG”。属性文件是:
time_intervals.properties
time_intervals_bg.properties
这就是我阅读它们的方式:
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()
但我只想要其中一个的值。请帮忙。
PS如果对我的解释和代码有任何不清楚的地方,请告诉我。