Java Multiple ResourceBundles
Asked Answered
C

4

8

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

Cubic answered 13/4, 2010 at 14:30 Comment(0)
R
3

Extend java.util.PropertyResourceBundle and call setParent.

Rozina answered 13/4, 2010 at 14:36 Comment(3)
private class ParentResourceHoleder extends ResourceBundle { @Override public Enumeration<String> getKeys() { // TODO Auto-generated method stub return null; } @Override protected Object handleGetObject(String key) { // TODO Auto-generated method stub return null; } @Override protected void setParent(ResourceBundle parent) { super.setParent(parent); } } java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key input.errorCubic
Um, extends java.util.PropertyResourceBundle.Rozina
https://mcmap.net/q/1314294/-is-it-possible-to-include-resource-bundle-files-within-a-resource-bundle maybe this is what you wanted, but didn't know you wanted itWimble
N
1

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());
    }
}
Nimocks answered 31/1, 2018 at 21:17 Comment(1)
In my opinion the Control parameter is not needed (ResourceBundle contructor add one by default) except if you need a specific one. As the load() method is mandatory for this code to work, why don't you put his code in the constructor ? This will simplify code...Siglos
E
0

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

Epiphany answered 1/12, 2010 at 14:3 Comment(0)
I
0

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.

Israel answered 9/5, 2016 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.