Java Multiple ResourceBundles

前端 未结 4 2110
别跟我提以往
别跟我提以往 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:10

    Extend java.util.PropertyResourceBundle and call setParent.

    0 讨论(0)
  • 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<Locale> getCandidateLocales(String baseName, Locale locale) {
    
                            List<Locale> out = new ArrayList<Locale>();
                            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

    0 讨论(0)
  • 2021-02-06 15:27

    Look at this class. It works for me perfectly! Javadoc for class explains how to use it.

    MultiplePropertiesResourceBundle (+ subsidiary ResourceBundleEnumeration)

    Here you may find helpfull unit-tests a.k.a. code documentation.

    0 讨论(0)
  • 2021-02-06 15:31

    Here is my implementation:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    import java.util.ResourceBundle;
    
    public class CombinedResourceBundle extends ResourceBundle
    {
        private Map<String, String> combinedResources = new HashMap<>();
        private List<String> bundleNames;
        private Locale locale;
        private Control control;
    
        public CombinedResourceBundle(List<String> bundleNames, Locale locale, Control control)
        {
            this.bundleNames = bundleNames;
            this.locale = locale;
            this.control = control;
        }
    
        public void load()
        {
            bundleNames.forEach(bundleName ->
            {
                ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, control);
                Enumeration<String> keysEnumeration = bundle.getKeys();
                ArrayList<String> keysList = Collections.list(keysEnumeration);
                keysList.forEach(key -> combinedResources.put(key, bundle.getString(key)));
            });
        }
    
        @Override
        public Object handleGetObject(String key)
        {
            return combinedResources.get(key);
        }
    
        @Override
        public Enumeration<String> getKeys()
        {
            return Collections.enumeration(combinedResources.keySet());
        }
    }
    
    0 讨论(0)
提交回复
热议问题