深入ResourceBundle

ResourceBundle是java开发中非常实用的一个类,主要用来处理应用程序多语言这样的国际化问题。

如果你的应用程序如果有国际化的需求,可以考虑使用ResourceBundle, 你要做的就是给出满足特定格式的Properties 文件,例如

resource.propreties

resource_zh_CN.properties

resource_ja_JP.properties.

然后应用程序使用ResourceBundle.getBundle(“resource”, locale) 就可以自动的搜索的相应Locale的Properties 文件。

虽然看起来很方便,但使用起来需要注意两个问题: 1 Properties 文件的搜索次序, 2.  决定是否能找到Properties 文件的ClassLoader , 这也是很多初学者遇到的问题

1.  搜索次序。

先来看个例子,假设你的系统只有两个Properties

(1) resource.zh_CN.properties : 中文的Properties

(2) resource.properties  : 英文的Properties

假设你Java 的default locale是zh_CN,  如果你调用 ResourceBundle.getBundle(“resource”, Locale.US) , 你觉得系统会使用哪一个文件中的内容?

很多人会觉得会使用resource.properties 中的内容, 但实际上不是这样的,当你传入一个Locale.US 给ResourceBundle的时候 ,  ResourceBundle的搜索次序是这样:
(1) resource_en_US.properties     --- 没找到
(2) resource_en.properties           --- 还是没找到
(3) resource_zh_CN.properties  ---- default locale, 找到了
(4) resource_zh_properties      
(5) resource.properties              

注意,ResourceBundle会自动的加上一个default locale 即 zh_CN 来搜索

系统没有找到xxxx__en_US.properties,  也没有找到xxx_en.properties, 而是找到了xxx_zh_CN.properties, 就会使用其中的内容, 所以你看的的是中文的结果。

实际上ResourceBundle 搜索结束以后,会建立一个ResourceBundle 对象的Chain, 对于上面的例子会是这样:

ResourceBundle_2  [locale=zh_CN , parent = ResourceBundle_1]

ResourceBundle_1 [locale = empty parent = null]

你可能要问,这个链表中问什么没有en_US,en 和zh相关的信息?   这是因为他们相关的Properties 不存在, 没有必要加入这个链表中。

如果你的应用程序访问resource文件的一个值得时候, 系统会先在ResourceBundle_2[Locale=zh_CN] 这个对象中找, 如果找到,直接返回相应的值

如果没有找到,顺着parent 即ResourceBundle_1继续寻找, 如果还没有找到,只好返回null 了, 因为没有parent 了

2. ClassLoader

这个也是经常出问题的地方, 很多时候当你准备好各种Locale 的Properties 文件, 调用ResourceBundle.getBundle(“resource”, Locale.US) 时,系统总是告诉你, 找不到resource_en_US的文件, 很是令人抓狂。

主要的原因就是ClassLoader 不对 ,有空接着写 :-)



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值