這個類主要用來解決國際化和本地化問題。國際化和本地化可不是兩個概念,兩者都是一起出現的。可以說,國際化的目的就是為了實作本地化。比如對于“取消”,中文中我們使用“取消”來表示,而英文中我們使用“cancel”。若我們的程式是面向國際的(這也是軟體發展的一個趨勢),那麼使用的人群必然是多語言環境的,實作國際化就非常有必要。而ResourceBundle可以幫助我們輕松完成這個任務:當程式需要一個特定于語言環境的資源時(如 String),程式可以從适合目前使用者語言環境的資源包(大多數情況下也就是.properties檔案)中加載它。這樣可以編寫很大程度上獨立于使用者語言環境的程式代碼,它将資源包中大部分(即便不是全部)特定于語言環境的資訊隔離開來。
這使編寫的程式可以:
輕松地本地化或翻譯成不同的語言
一次處理多個語言環境
以後可以輕松進行修改,以便支援更多的語言環境
說的簡單點,這個類的作用就是讀取資源屬性檔案(properties),然後根據.properties檔案的名稱資訊(本地化資訊),比對目前系統的國别語言資訊(也可以程式指定),然後擷取相應的properties檔案的内容。
使用這個類,properties需要遵循一定的命名規範,一般的命名規範是: 自定義名語言代碼國别代碼.properties,如果是預設的,直接寫為:自定義名.properties。
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
當在中文作業系統下,如果myres_zh_CN.properties、myres.properties兩個檔案都存在,則優先會使用myres_zh_CN.properties,當myres_zh_CN.properties不存在時候,會使用預設的myres.properties。
沒有提供語言和地區的資源檔案是系統預設的資源檔案。
資源檔案都必須是ISO-8859-1編碼,是以,對于所有非西方語系的處理,都必須先将之轉換為Java Unicode Escape格式。轉換方法是通過JDK自帶的工具native2ascii.
ResourceBundle的類層次結構
PropertyResourceBundle将本地化的文本存儲于Java property檔案中。
從ResourceBundle中擷取值
擷取ResourceBundle執行個體後可以通過下面的方法獲得本地化值。
getObject(String key);
getString(String key);
getStringArray(String key);
還可以通過keySet()方法擷取所有的key。Set keys = bundle.keySet();
其它ResourceBundle 方法可以通過檢視文檔獲得。
測試及驗證
建立4個屬性檔案
my_en_US.properties:cancelKey=cancel
my_zh_CN.properties:cancelKey=\u53D6\u6D88(取消)
my_zh.properties:cancelKey=\u53D6\u6D88zh(取消zh)
my.properties:cancelKey=\u53D6\u6D88default(取消default)
擷取bundle
ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));
1
其中new Locale(“zh”, “CN”)提供本地化資訊,上面這行代碼,程式會首先在classpath下尋找my_zh_CN.properties檔案,若my_zh_CN.properties檔案不存在,則取找my_zh.properties,如還是不存在,繼續尋找my.properties,若都找不到就抛出異常。
代碼
import javax.annotation.Resource;
import java.util.Locale;
import java.util.ResourceBundle;
public class Main {
public static void main(String args[]) {
ResourceBundle bundle = ResourceBundle.getBundle("my", new Locale("zh", "CN"));
String cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("my", Locale.US);
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("my", Locale.getDefault());
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("my", Locale.GERMAN);
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("my");
for (String key : bundle.keySet()) {
System.out.println(bundle.getString(key));
}
}
}
輸出結果
取消
cancel
取消
取消
取消
說明:前面三個分别按照zh_CN,US,預設的結果輸出,第四個由于我們未定義GERMAN屬性檔案,這時ResourceBundle為我們提供了一個fallback(也就是一個備用方案),這個備用方案就是根據目前系統的語言環境來得到的本地化資訊。是以若是找不到GERMAN的,之後就會去找CHINA了,是以找到了res_zh_CH.properties這個資源包。最後一個是若有多個屬性檔案,可以按照Map的形式周遊,獲得屬性檔案内的各個值。
public class PropertiesFileUtil {
private ResourceBundle rb = null;
public PropertiesFileUtil(String bundleFile) {
rb = ResourceBundle.getBundle(bundleFile);
}
public String getValue(String key) {
return rb.getString(key);
}
public static void main(String[] args) {
//加了 \\之後,會自動從根資源檔案中取config.properties,config_zh_CN.properties等資源檔案,不加\\,要和java檔案放在一起
PropertiesFileUtil p = new PropertiesFileUtil("\\config");
System.out.println(p.getValue("remitRate"));
}
}