资源包java

发布于 2024-11-10 22:36:26 字数 250 浏览 5 评论 0原文

是否可以在类似于以下内容的文件夹结构中维护资源包;

 1. us\en
 2. ae\en
 3. ae\ar

资源包文件名将相同,但维护在不同的文件夹中。我知道Java推荐

myresource_en_US.properties

但我需要将它们维护在文件夹中并使用资源包类来访问。我正在使用 JDK 6。有谁知道我该怎么做?

Is it possible to maintain the resource bundle in a folder structure something similar to the following;

 1. us\en
 2. ae\en
 3. ae\ar

The resource bundle file name will be the same but maintained in different folders. I know Java recomends

myresource_en_US.properties

But I need to maintained them in folders and use the resource bundle classes to access. I am using JDK 6. Does anyone know how I can do it?

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

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

发布评论

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

评论(2

匿名的好友 2024-11-17 22:36:26

是的,您可以使用自定义 < 来控制加载代码>控制。下面是一个启动示例:

public class FolderControl extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException
    {
        String resourceName = "/" + locale.getCountry() + "/" + locale.getLanguage() 
            + "/" baseName + ".properties";
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
}

(源代码是从默认实现复制的,仅更改了 resourceName 并将 PropertyResourceBundle 更改为以 UTF-8 格式读取流--不再需要native2ascii)

如下所示

ResourceBundle bundle = ResourceBundle.getBundle("myresource", new FolderControl());
// ...

另请参阅:

Yes, you can control the loading with a custom Control. Here's a kickoff example:

public class FolderControl extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException
    {
        String resourceName = "/" + locale.getCountry() + "/" + locale.getLanguage() 
            + "/" baseName + ".properties";
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
}

(the source code is copied from the default implementation with only the resourceName changed and the PropertyResourceBundle being changed to read the stream as UTF-8 --no need for native2ascii anymore)

which you use as follows

ResourceBundle bundle = ResourceBundle.getBundle("myresource", new FolderControl());
// ...

See also:

小兔几 2024-11-17 22:36:26

看一下 http://download.oracle .com/javase/6/docs/api/java/util/ResourceBundle.Control.html。您可以将一个作为参数传递给 ResourceBundle.getBundle() 作为一种“在本地化资源的组织和打包中提供不同约定”的方式。

Have a look at http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.Control.html. You can pass one as an argument to ResourceBundle.getBundle() as a way "to provide different conventions in the organization and packaging of localized resources".

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