國際化技術

resourceBoundle (1)resourceBoudle的使用 有兩種類型的resourceBoudle, 一種從properties文件中讀取數據 ,另外一種從class文件中讀取數據, 可是他們的使用方法都是同樣的。以下所示: //第一個參數爲語種, //第二個參數爲國家 //第三個參數隨便定,通常用於區別操做系統 Locale currentLocale = new Locale("cn","CN", "windows") ; // 得到 ResourceBundle ResourceBundle messages=ResourceBundle.getBundle("MessageBundle", currentLocale); // 得到所需的值 String value = messages.getString(key); (2)對於properties文件中讀取數據的ResourceBundle, getBundle函數的第一個參數是資源文件的名稱(暫時取名爲名稱),第二個參數爲區域(暫時取名爲區域)。 對於(1) 中的例子, 系統將如下面的順序在class所在路徑下查找文件(假設jre運行在english版的windows平臺上) MessageBundle_cn_CN_windows.properties MessageBundle_cn_CN.properties MessageBundle_cn.properties MessageBundle_en_En.properties(默認locale,與jre所在的操做系統有關) MessageBundle_en.properties(默認locale,與jre所在的操做系統有關) MessageBundle.properties 注意: 若是ResourceBundle的代碼以下所示,第一個參數爲com.neusoft.MessageBudle, 則,系統將到class所在文件夾的中子路徑com\neusoft中查找 ResourceBundle messages=ResourceBundle.getBundle("com.neusoft.MessageBudle", currentLocale) (2)對於class文件中讀取resourceBundle, 則被讀取的資源文件(class文件)必須從ListResourceBundle中繼承,而且實現getContents方法。以下所示 import java.util.ResourceBundle; import java.util.ListResourceBundle; public class MessageBundle_cn_CN_windows extends ListResourceBundle { // key-value , contained the resource item private Object[][] contents = { {"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}, {"key4", "value4"} }; // must be inplement public Object[][] getContents(){ return contents ; } } 對於(1)中的例子, 系統將如下面的順序在class所在路徑下查找文件(假設jre運行在english版的windows平臺上) MessageBundle_cn_CN_windows.class MessageBundle_cn_CN.class MessageBundle_cn.class MessageBundle_en_En.class(默認locale,與jre所在的操做系統有關) MessageBundle_en.class(默認locale,與jre所在的操做系統有關) MessageBundle.class 那麼,resourceBundle既然已經提供從properties中讀取資源項,爲何要提供listResourceBundle,這是由於從listResourceBundle讀取數據不須要i/o開銷,具備更快的速度。 注意:若是在給定目錄下同時有properties資源文件和listResourceBunle資源class ,則 listResourceBunle資源class具備更高的優先權。 Message format 最簡單的例子 代碼: String message = "these is the {0} message format "; String messageOut = MessageFormat.format(message, new String[]{ "first"}); System.out.println(messageOut); 運行結果: these is the first message format 說明: 大括號內的數字表示第幾個參數 使用日期的例子 代碼: String message = "At {1,time} on {1,date}, there was {2} on planet {0,number,integer} "; Object[] arguments = { new Integer(7), new Date(System.currentTimeMillis()), "a disturbance in the Force" }; String messageOut = MessageFormat.format(message,arguments ); System.out.println(messageOut); 運行結果: At 13:30:51 on 2003-5-14, there was a disturbance in the Force on planet 7 說明: 大括號內的第一個參數表示位置 ,第二個參數是類型,第三個參數是風格(能夠沒有,沒有表示default),以下表所示: Format Type Format style Subformat Created (none) null number (none) NumberFormat.getInstance(getLocale()) integer NumberFormat.getIntegerInstance(getLocale()) currency NumberFormat.getCurrencyInstance(getLocale()) percent NumberFormat.getPercentInstance(getLocale()) SubformatPattern new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale())) date (none) DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale()) short DateFormat.getDateInstance(DateFormat.SHORT, getLocale()) medium DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale()) long DateFormat.getDateInstance(DateFormat.LONG, getLocale()) full DateFormat.getDateInstance(DateFormat.FULL, getLocale()) SubformatPattern new SimpleDateFormat(subformatPattern, getLocale()) time (none) DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale()) short DateFormat.getTimeInstance(DateFormat.SHORT, getLocale()) medium DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale()) long DateFormat.getTimeInstance(DateFormat.LONG, getLocale()) full DateFormat.getTimeInstance(DateFormat.FULL, getLocale()) SubformatPattern new SimpleDateFormat(subformatPattern, getLocale()) choice SubformatPattern new ChoiceFormat(subformatPattern) 使用數字例子 代碼: MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}"); Object[] objs = {new Double(3.1415)}; String result = mf.format( objs ); System.out.println(result); 運行結果: 3.14, 3.1 說明: 對於float 和double , 風格能夠#.##形式表示,逗號後面有幾個#, 就表示有幾位小數,分號以前的無所謂,甚至能夠不寫 Character internalize 錯誤的例子: char ch; // ch is a letter if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) // ch is a digit if (ch >= '0' && ch <= '9') // ch is a whitespace if ((ch == ' ') || (ch =='\n') || (ch == '\t')) 正確的例子 //import java.lang.Character; char ch; // ch is a letter if (Character.isLetter(ch)) // ch is a digit if (Character.isDigit(ch)) // ch is a whitespace if (Character.isSpaceChar(ch)) 緣由(copy from sun document): The preceding code is wrong because it works only with English and a few other languages. String compare 錯誤的例子: String a String b // compare a and b if(a.compareTo(b) < 0) 正確的例子: // import java.text. Collator String a = "及"; String b = "中" ; Collator collator = Collator.getInstance(); // compare a and b if(collator.compare(a, b) < 0) 緣由(copy from sun document): The preceding code is wrong because it works only with English and a few other languages.
相關文章
相關標籤/搜索