Java 多 ResourceBundle

发布于 2024-08-28 21:37:48 字数 51 浏览 4 评论 0原文

我想从各种包中加载多个属性文件作为 ResourceBundle。我可以用Java实现吗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天冷不及心凉 2024-09-04 21:37:48

扩展 java.util.PropertyResourceBundle 并调用 setParent。

Extend java.util.PropertyResourceBundle and call setParent.

白首有我共你 2024-09-04 21:37:48

这是我的实现:

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

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());
    }
}
违心° 2024-09-04 21:37:48

ResourceBundle.Control() 控制 ResourceBundle 的文件列表。你可以覆盖
getCandidateLocales 和 toBundleName。 toBundleName 将语言环境转换为“文件名”以及您可以在 getCandidateLocales 中控制的语言环境列表。
例如,

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

它仅适用于 Java 1.6

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

半岛未凉 2024-09-04 21:37:48

看看这堂课。它非常适合我!类的 Javadoc 解释了如何使用它。

MultiplePropertiesResourceBundle
(+附属
ResourceBundleEnumeration

在这里您可能会发现有用的单元测试 又名代码文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文