是否可以在资源包中包含资源包文件 [英] Is it possible to include resource bundle files within a resource bundle

查看:99
本文介绍了是否可以在资源包中包含资源包文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 java.util.ResourceBundle 来加载属性信息。我们的属性文件已经变得如此庞大,我们正在考虑将主属性文件分成几个子模块。有可能实现这个目标吗?

We are using java.util.ResourceBundle to load property information. Our property file has grown so huge and we are thinking of splitting the master property file into several sub modules. Is it possible to achieve this?

master.properties

==> 

 master.properties
   include moduleA.properties
   include moduleB.properties

请告诉我?

推荐答案

首先,我想知道为什么你选择 java.util.ResourceBundle java.util.Properties 。鉴于您的问题是如何制定的,您似乎并不关心本地化/国际化,也不关心包文件继承。

First of all, I wonder why you've chosen java.util.ResourceBundle over java.util.Properties. Given how your question is formulated, you don't seem to care about localization/internationalization nor about bundle file inheritance.

使用属性它非常简单,因为它实现了 地图 反过来提供 putAll() 方法合并另一个地图。开球示例:

With Properties it's extraordinary easy since it implements Map which in turn offers a putAll() method to merge another map. Kickoff example:

Properties master = new Properties();
master.load(masterInput);

Properties moduleA = new Properties();
moduleA.load(moduleAinput);
master.putAll(moduleA);

Properties moduleB = new Properties();
moduleB.load(moduleBinput);
master.putAll(moduleB);

// Now `master` contains the properties of all files.

如果你真的坚持使用 ResourceBundle ,你最好的办法是创建一个自定义 ResourceBundle ,其中你通过自定义 Control

If you really insist in using ResourceBundle, your best bet is to create a custom ResourceBundle wherein you contol the loading by a custom Control.

假设您在 master.properties 中有以下条目,它表示带有模块属性文件基本名称的逗号分隔字符串:

Assuming that you've the following entry in master.properties which represents a commaseparated string with base names of the module properties files:

include=moduleA,moduleB

然后以下自定义 ResourceBundle 示例应该有效:

Then the following custom ResourceBundle example should work:

public class MultiResourceBundle extends ResourceBundle {

    protected static final Control CONTROL = new MultiResourceBundleControl();
    private Properties properties;

    public MultiResourceBundle(String baseName) {
        setParent(ResourceBundle.getBundle(baseName, CONTROL));
    }

    protected MultiResourceBundle(Properties properties) {
        this.properties = properties;
    }

    @Override
    protected Object handleGetObject(String key) {
        return properties != null ? properties.get(key) : parent.getObject(key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Enumeration<String> getKeys() {
        return properties != null ? (Enumeration<String>) properties.propertyNames() : parent.getKeys();
    }

    protected static class MultiResourceBundleControl extends Control {
        @Override
        public ResourceBundle newBundle(
            String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
                throws IllegalAccessException, InstantiationException, IOException
        {
            Properties properties = load(baseName, loader);
            String include = properties.getProperty("include");
            if (include != null) {
                for (String includeBaseName : include.split("\\s*,\\s*")) {
                    properties.putAll(load(includeBaseName, loader));
                }
            }
            return new MultiResourceBundle(properties);
        }

        private Properties load(String baseName, ClassLoader loader) throws IOException {
            Properties properties = new Properties();
            properties.load(loader.getResourceAsStream(baseName + ".properties"));
            return properties;
        }
    }

}

(除了琐碎的异常处理和本地化处理,这取决于你)

这可以用作:

ResourceBundle bundle = new MultiResourceBundle("master");

这篇关于是否可以在资源包中包含资源包文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆