Localization 為本地化、在地化的意思,是指讓應用程式根據不同使用者的語言與地區顯示不同的資料,比如文字、日期時間格式、數字格式、貨幣格式等顯示方式。
在華語地區顯示中文字,英語系國家顯示英文,日本地區顯示日文。使用 Localization 就可以一支程式適用全球各地,不需發行不同的版本。
多國語系 Resources
切換到 Files 頁面,然後在專案按右鍵/new Folder/建立 resources 目錄。
切換到 Projects 頁面,在專案按右鍵/Properties/Sources/Source Package Folders/Add Folders/ 選擇 resources。
resources 按右鍵/other,再由彈出視窗中的 Other 選取 Empty file,分別建立檔名message_zh_TW.properties 及 message_en_US.properties。
上述的檔名之命名方式為「檔名_語系_區碼.properties」,比如「message_zh_TW.properties」、「message_en_US.properties」。
Resources 設定檔
message_zh_TW.properties、message_en_US.properties 二個檔案的內容使用 「key=value」的方式填入 。
message_zh_TW.properties 檔案內容如下
title = 猜拳遊戲 authorized = 吳明學
message_en_US.properties 的肉容如下
title = Game authorized = Thomas
Java 代碼
在 Java 代碼使用 ResourceBundle.getBundle 取得要使用的資源檔 (msg),第一個參數為 Resources 裏的檔名 (message),第二個參數為地區(Locale.Taiwan),然後用 msg.getString(“key”) 取得其值。
package test; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; import java.util.ResourceBundle; public class LocaleTest { public static void main(String[] args){ try { System.setOut(new PrintStream(System.out, true, "UTF-8")); } catch (UnsupportedEncodingException ex) {} ResourceBundle msg; msg=ResourceBundle.getBundle("message", Locale.TAIWAN); System.out.printf("title : %s\n",msg.getString("title")); System.out.printf("Authorized : %s\n",msg.getString("authorized")); msg=ResourceBundle.getBundle("message", Locale.US); System.out.printf("title : %s\n",msg.getString("title")); System.out.printf("Authorized : %s\n",msg.getString("authorized")); //System.out.println(Locale.getDefault().toString()); } } 結果 : title : 猜拳遊戲 Authorized : 吳明學 title : Game Authorized : Thomas
系統預設語系
想要取得系統預設語系,然後顯示適合系統的語言,則使用 Locale.getDefaulte()。
package test;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
public class LocaleTest {
public static void main(String[] args){
try {
System.setOut(new PrintStream(System.out, true, "UTF-8"));
} catch (UnsupportedEncodingException ex) {}
ResourceBundle msg=ResourceBundle.getBundle("message", Locale.getDefault());
System.out.printf("title : %s\n",msg.getString("title"));
System.out.printf("Authorized : %s\n",msg.getString("authorized"));
}
}
結果 :
title : 猜拳遊戲
Authorized : 吳明學
使用Locale 設定語系
有二種方式
Locale zhLocale=new Locale(“zh”, “TW”);
Locale zhLocale=Locale.TAIWAN;
使用ResourceBundle 變更語系
ResourceBundle messages=ResourceBundle.getBundle(“message”, zhLocale);
使用ResourceBundle 取得字串
messages.getString(“menu1″), 然後將之印出
Properties檔案命名規則
message_語系_國別.properties
en_US
zh_CN
zh_TW
語系為小寫, 國別為大寫
顯示日期
日期的顯示格式, 可以先使用DateFormat.getXXX來設定. 日期格式有如下三種
DateFormat.getDateInstance() : 可設定日期格式
DateFormat.getTimeInstance() : 可設定時間格式
DateFormat.getDateTimeInstance() : 可設定日期, 時間格式
上述的最後一個參數可以加入Locale.
格式有四種.
DateFormat.SHORT : 12.13.52 3:30PM
DateFormat.MEDIUM : Jan 12, 1952
DateFormat.LONG : January 12, 1952 3:30:32pm
DateFormat.FULL : Tuesday, April 12, 1952 3:30:42pm PST
public static void main(String[] args) { Locale zhLocale=Locale.TAIWAN; DateFormat []df=new DateFormat[8]; df[0]=DateFormat.getDateInstance(DateFormat.SHORT, zhLocale); df[1]=DateFormat.getTimeInstance(DateFormat.SHORT, zhLocale); df[2]=DateFormat.getDateInstance(DateFormat.MEDIUM, zhLocale); df[3]=DateFormat.getTimeInstance(DateFormat.MEDIUM, zhLocale); df[4]=DateFormat.getDateInstance(DateFormat.LONG, zhLocale); df[5]=DateFormat.getTimeInstance(DateFormat.LONG, zhLocale); df[6]=DateFormat.getDateInstance(DateFormat.FULL, zhLocale); df[7]=DateFormat.getTimeInstance(DateFormat.FULL, zhLocale); for (int i=0;i<df.length;i++){ System.out.printf("%s\n",df[i].format(new Date())); } }
結果如下
2018/7/14 下午 10:24 2018/7/14 下午 10:24:14 2018年7月14日 下午10時24分14秒 2018年7月14日 星期六 下午10時24分14秒 TST
SimpleDateFormat
OCP認証考式中, 超愛考上述的DateFormat. 但相信我, 一般專案中用到的機會非常小. 反而是SimpleDateFormat最常用
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd"); System.out.println(sdf1.format(new Date())); SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm:ss"); System.out.println(sdf2.format(new Date()));
常見的格式如下
yyyy : 4位數西洋年
MM : 2位數月份, 不足2位補0
dd : 2位數日期
hh : 時, 12小時制
HH: 時, 24小時制
mm: 分
ss: 秒
Calendar
new Date()可以取得目前的系統時間. 但若要指定某個日期時間的話, 則需使用到Calendar
Calendar calendar=Calendar.getInstance(); calendar.set(2000, 6, 18, 14, 25, 59); SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); System.out.println(sdf1.format(calendar.getTime()));
上述calendar要使用getTime()轉換成Date物件, 再交由sdf列轉換格式列印出來
顯示貨幣
NumberFormat currency=NumberFormat.getCurrencyInstance(Locale.CHINA);
System.out.println(currency.format(100000));
結果如下
¥100,000.00