I want to load multiple property files from various packages as ResourceBundle. Can I achieve that in Java
Java Multiple ResourceBundles
Asked Answered
Extend java.util.PropertyResourceBundle
and call setParent
.
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.error –
Cubic
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 it –
Wimble
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());
}
}
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
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.
© 2022 - 2024 — McMap. All rights reserved.