1. 程式人生 > 其它 >使用eclipse開發國際化程式

使用eclipse開發國際化程式

1.1 什麼是i18n

以下內容引自wikipedia Internationalization_and_localization

The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and the last n in the word internationalization, a usage coined at Digital Equipment Corporation in the 1970s or 1980s) and L10n for localization, due to the length of the words. Some writers have the latter acronym capitalized to help distinguish the two.
簡單來說,i18n是Internationalization的縮寫,18代表單詞中間的18個字母。

1.2 什麼是Locale

一個Locale物件代表了一個特定的地域、政府、或文化地區,它們使用的文字/語言是有別於其他Locale的。
Locale物件有三個主要元件:language、country、variant。
variant代表特定製造商或特定瀏覽器程式碼,例如WIN代表Windows,MAC代表Macintosh,POSIX代表POSIX,可以用下劃線將兩個variant隔開,重要的放前面。
完整的language、country、variant可以在language-subtag-registry中找到,
找language: search for "Type: language"
找country: search for "Type: region"
找variant: search for "Type: variant"

Locale有三個構造器:

Locale(String language)
// Construct a locale from a language code.
Locale(String language, String country)
// Construct a locale from language and country.
Locale(String language, String country, String variant)
// Construct a locale from language, country and variant.

例如要構造一個加拿大使用的英語的Locale物件,可以這樣寫:
Locale lcoale = new Locale("en", "CA");
此外,Locale類提供了靜態final域,他們返回特定國家或者語言的地區,因此可以通過呼叫它的靜態方法來構造Local物件:
Locale lcoale = Locale.CANADA_FRENCH;
完整的靜態final域可以在

Java的API中找到。

另外,靜態getDefault方法返回使用者計算機所在地區:
Locale lcoale = Locale.getDefault();

1.3 properties檔案

我們在開發程式時,一般都會將文字元素放在單獨的檔案中,
不同語言不同檔案,
然後使用ResourceBundle來讀取屬性檔案,
檔案的命名方式一般為:basename_languageCode_countryCode,
basename可以自己寫,後面的格式時固定的。
比如basename為MyResource,則可以有以下檔案:

有了計劃記得推動,不要原地踏步。