【发布时间】:2011-12-21 08:52:59
【问题描述】:
在我的项目中,我有 2 个 properties files 用于国际化。
我将ResourceBundle 与Locale 参数一起使用,并将属性文件中的密钥存储在一个集合中。不幸的是,在集合中存储了来自两个文件的组合键。我只想要来自单个文件的密钥,具体取决于语言环境。在我的情况下,语言环境是“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() 后从两个属性文件中获取组合键,但我只想要其中一个的值。请帮忙。
附:如果对我的解释和代码有任何不清楚的地方,请告诉我。
【问题讨论】:
-
第二个属性文件的文件名是什么?您将其列为 time_intervals_bg_properties,而不是 time_intervals_bg.properties
-
我的错,谢谢。
-
没问题。您的 face-config.xml 中是否有一个条目表明您具有受支持的语言环境
BG.
标签: java jakarta-ee properties-file