0

在我的项目中,我有 2 个properties files用于国际化。我使用ResourceBundlewithLocale参数并将属性文件中的键存储在集合中。不幸的是,在集合中存储了来自两个文件的组合键。我只想要来自单个文件的密钥,具体取决于语言环境。在我的情况下,语言环境是“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如果对我的解释和代码有任何不清楚的地方,请告诉我。

4

2 回答 2

1

您没有正确使用 ResourceBundle 系统。

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

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

于 2011-11-03T17:12:04.417 回答
0

要扩展之前的答案,您应该有一组本地化字符串的资源文件,然后是一个单独的数值文件:

time_intervals.properties:
    one_hour=1 hour

time_intervals_bg.properties:
    one_hour=1 час

time_intervals.numbers.properties:
    one_hour=1

从 加载要显示的字符串time_intervals,并从 加载相应的数值time_intervals.numbers

编辑:或者,如果您尝试使用数值来确定要显示的字符串,则在文件中切换键和值,并忘记任何time_intervals.numbers文件:

time_intervals.properties:
    1=1 hour

time_intervals_bg.properties:
    1=1 час
于 2011-11-03T17:19:14.647 回答