属性与资源捆绑 [英] Properties vs Resource Bundle

查看:45
本文介绍了属性与资源捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关属性和资源包的信息.但是我无法区分两者之间的差异.何时使用属性文件以及何时使用资源包.

I read about properties and resource bundle. But I was unable to get difference between these. When to use Properties file and when to use Resource bundle.

要加载属性文件,请使用以下代码

Properties tempProp = new Properties();
FileInputStream propsFile = new FileInputStream(xyz.properties);
tempProp.load(propsFile);

加载资源包

ResourceBundle labels =
    ResourceBundle.getBundle("xyz", currentLocale);
Enumeration bundleKeys = labels.getKeys();

在两种情况下(在资源包和属性中),我们都使用属性文件.我发现的一个区别是,使用属性文件存储特定于应用程序的数据,而使用资源包存储i18n数据.我不知道我是否正确.

In both of the cases (in resource bundle and in Properites) we are using properties file. The one difference I found is that to store application specific data we use properties file and to use i18n data we use resource bundle. I don't know whether i am right or not.

我想知道以上两个的用法.两者有什么区别.

推荐答案

是的,您正在按照正确的思路进行思考.

Yes, you're thinking along the right lines.

资源包没有必须使用属性文件-它只是一种实现( PropertyResourceBundle ).属性文件实际上只是一个字符串到字符串的映射-可以用于i18n,但不必一定要使用.

Resource bundles don't have to use property files - it's just one implementation (PropertyResourceBundle). A properties file is really just a string-to-string mapping - and that can be used for i18n, but doesn't have to be.

ResourceBundle 为您提供了一种一致的方式来请求特定语言环境的适当对象(通常是字符串),并带有回退等.这通常(但并非总是)由单独的属性文件作为后盾每种语言.

ResourceBundle gives you a consistent way of requesting the appropriate object (usually a string) for a particular locale, with fallbacks etc. This is often, but not always, backed by a separate property file for each language.

是的:如果您正在使用i18n,则应使用 ResourceBundle ;如果只需要在文本文件中保留字符串对字符串的映射,则可以直接使用 Properties .

So yes: if you're dealing with i18n, you should use ResourceBundle; if you just need a string-to-string map persisted in a text file, it's fine to use Properties directly.

这篇关于属性与资源捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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