Java 使用 JDK6 的 ResourceBundle 類
資源包包含特定于語言環境的對象。當程序需要一個特定于語言環境的資源時(如String),程序可以從適合當前用戶語言環境的資源包中加載它。使用這種方式,可以編 寫很大程度上獨立于用戶語言環境的程序代碼,它將資源包中大部分(即便不是全部)特定于語言環境的信息隔離開來。這使編寫的程序可以:輕松地本地化或翻譯成不同的語言 一次處理多個語言環境以后可以輕松進行修改,以便支持更多的語言環境
RBPropDemo.java
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
public class RBPropDemo {
public static void main(String[] args) {
ResourceBundle.clearCache();
String bundleName = "myproj.MyResources";
ResourceBundle myResources = ResourceBundle.getBundle(bundleName, Locale.GERMAN);
System.out.println("Key's values:");
System.out.println(myResources.getString("okKey"));
System.out.println(myResources.getString("cancelKey"));
System.out.println(myResources.getString("submitKey"));
System.out.println("\nChecking okKey in resource bundle:");
if (myResources.containsKey("okKey")) {
System.out.println("okKey exists! " + " Value = " + myResources.getString("okKey"));
} else {
System.out.println("The key Doesn't Exist");
}
System.out.println("\nGet a set of keys:");
Set<String> keySet = myResources.keySet();
Object[] keys = keySet.toArray();
for (int i = 0; i < keys.length; i++) {
System.out.println("Key " + (i + 1) + " = " + keys[i]);
}
}
}
/*
MyResources.properties file
okKey = OK
cancelKey = Cancel
submitKey = Submit
The MyResources_de.properties file
cancelKey = Abbrechen
*/
本文由用戶 mhpmii 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!