Java Multiple ResourceBundles

前端 未结 4 2116
别跟我提以往
别跟我提以往 2021-02-06 14:30

I want to load multiple property files from various packages as ResourceBundle. Can I achieve that in Java

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 15:21

    ResourceBundle.Control() controls the list of files for the ResourceBundle. You can overwrite getCandidateLocales and toBundleName. toBundleName converts locale to the "file name" and the list of locales you can control in getCandidateLocales. For example like

     final String[] variants = new String[]{"your names"};
     ResourceBundle.getBundle(baseName, locale,
                new ResourceBundle.Control() {
                    public List getCandidateLocales(String baseName, Locale locale) {
    
                            List out = new ArrayList();
                            String language = locale.getLanguage();
                            String country = locale.getCountry();
    
                            for (String variant : variants) {
                                out.add(new Locale(language, country, variant));
                            }
                            out.addAll(super.getCandidateLocales(baseName, locale));
                            return out;
                    }
    
                    public String toBundleName(String baseName, Locale locale) {
                            Locale l = new Locale(locale.getLanguage(), locale.getCountry());
                            return locale.getVariant() + "." + super.toBundleName(baseName, l);
                    }
                });
    

    It works only in Java 1.6

提交回复
热议问题