JDK對國際化的支持

一 JDK 提供對國際化支持相關的3個類

1.1 java.util.Locale

java.util.Locale 主要在軟件的本地化時使用。它本身沒有什麼功能,更多的是作爲一個參數輔助其他方法完成輸出的本地化。java.util.Locale 類對象表示了特定的地理,政治和文化地區。

public class I18nTest1 {
    public static void main(String args[]){
        //系統默認語言環境,相當於 Locale.CHINA
        Locale defaultLocale = Locale.getDefault();
        //輸出結果:CN x 中國 x zh x 中文 x 中文 (中國)
        System.out.println(defaultLocale.getCountry()+" x "+defaultLocale.getDisplayCountry()
            +" x "+defaultLocale.getLanguage()+" x "+defaultLocale.getDisplayLanguage()
            +" x "+defaultLocale.getDisplayName());

        Locale usLocale = Locale.US;
        //輸出結果:US x 美國 x en x 英文 x 英文 (美國)
        System.out.println(usLocale.getCountry()+" x "+usLocale.getDisplayCountry()
        +" x "+usLocale.getLanguage()+" x "+usLocale.getDisplayLanguage()
        +" x "+usLocale.getDisplayName());

        Locale chineseLocale = Locale.CHINESE;
        //輸出結果: x  x zh x 中文 x 中文
        System.out.println(chineseLocale.getCountry()+" x "+chineseLocale.getDisplayCountry()
        +" x "+chineseLocale.getLanguage()+" x "+chineseLocale.getDisplayLanguage()
        +" x "+chineseLocale.getDisplayName());

        Locale englishLocale = Locale.ENGLISH;
        //輸出結果: x  x en x 英文 x 英文
        System.out.println(englishLocale.getCountry()+" x "+englishLocale.getDisplayCountry()
        +" x "+englishLocale.getLanguage()+" x "+englishLocale.getDisplayLanguage()
        +" x "+englishLocale.getDisplayName());

        //獲得系統所有已安裝的語言包信息
        Locale[] locales = Locale.getAvailableLocales();
        for(Locale locale:locales){
            System.out.println(locale.getCountry()+" x "+locale.getDisplayCountry()
        +" x "+locale.getLanguage()+" x "+locale.getDisplayLanguage()
        +" x "+locale.getDisplayName());
        }
    }
}

1.2 java.util.ResourceBoudle(抽象類)

java.util.ResourceBoudle 爲軟件國際化提供了讀取國際化資源文件的途徑。

說的簡單點,這個類的作用就是讀取classpath路徑下的資源屬性文件(properties),然後根據.properties文件的名稱信息(本地化信息),匹配當前系統的國家語言信息(也可以程序指定),然後獲取相應的properties文件的內容。

國際化資源文件的命名規則爲 baseName_語言代號_國家代號.properties,(其中 baseName 爲自定義)。如果資源文件命名爲 baseName.properties 則當該類讀取不到對應語言環境的資源文件時,默認讀取 baseName.properties 。

資源文件都必須是ISO-8859-1編碼,因此,對於所有非西方語系的處理,都必須先將之轉換爲Java Unicode Escape格式。轉換方法是通過JDK自帶的工具native2ascii.

#資源文件名:i18nTest_en_US.properties
name=Mary
age=eighteen
language=English
Country=US 
#資源文件名:i18nTest_zh_CN.properties
#需將中文轉換爲Unicode編碼
name=\u739B\u4E3D
age=18
language=\u4E2D\u6587
Country=\u4E2D\u56FD

-----------------分割線------------------

public class I18nTest2 {
    public static void main(String args[]){
        //會自動查找classpath路徑下的資源文件
        //找到 baseName 爲"i18nTest",並且locale爲默認locale的詳細信息,在本系統下即爲讀取 i18nTest_zh_CN.properties 文件
        ResourceBundle bundle = ResourceBundle.getBundle("i18nTest");
        String name = bundle.getString("name");
        String age = bundle.getString("age");
        String language = bundle.getString("language");
        String country = bundle.getString("country");
        //輸出結果:我的名字叫瑪麗,我今年18歲了,我出生於中國,所以我的母語是中文
        System.out.println("我的名字叫"+name+",我今年"+age+"歲了,我出生於"+country+",所以我的母語是"+language);

        bundle = ResourceBundle.getBundle("i18nTest", Locale.US);
        name = bundle.getString("name");
        age = bundle.getString("age");
        language = bundle.getString("language");
        country = bundle.getString("country");
        //輸出結果:My name is Mary. I'm eighteen years old. I was born in US , so my first language is English
        System.out.println("My name is "+name+". I'm "+age+" years old. I was born in "+country+", so my first language is "+language);
    }
}

1.3 java.text.MessageFormat

Java.text.MessageFormat 用來輔助信息的格式化。對於字符串中可能變動的部分,可以先使用參數索引佔住文字位置,參數索引是格式爲形似{0}、{1}、{10}、{11}的非負整數,之後再使用 MessageFormat 的靜態方法 format() ,將字符串中的參數索引用具體的字符串信息替換。

String values = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";
Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};  
values = MessageFormat.format(values,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q");
//或 
//values = MessageFormat.format(values, array); 
//輸出結果:ABCDEFGHIJKLMNOPQ
System.out.println(values);

因此,可以在國際化資源文件中以佔位符的方式來表示需根據程序變動的信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章