---
title: "深入ResourceBundle"
author: "ender"
date: "2013-09-13T13:03:34Z"
tags: []
categories: ["编程语言"]
slug: "resourcebundle"
draft: false
lastmod: "2013-09-13T13:03:34Z"
id: 1941859
url: "https://ender.iteye.com/blog/1941859"
---

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 不对 ，有空接着写 :-)