开发者

Java Multiple ResourceBundles

开发者 https://www.devze.com 2022-12-26 11:38 出处:网络
I want to loa开发者_StackOverflow社区d multiple property files from various packages as ResourceBundle. Can I achieve that in JavaExtend java.util.PropertyResourceBundle and call setParent. Here is my

I want to loa开发者_StackOverflow社区d multiple property files from various packages as ResourceBundle. Can I achieve that in Java


Extend java.util.PropertyResourceBundle and call setParent.


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());
    }
}


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


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

精彩评论

暂无评论...
验证码 换一张
取 消