中文 | zh |
英语 | en |
日语 | ja |
法语 | fr |
中国大陆 | CN |
中国香港 | HK |
美国 | US |
英国 | EN |
//带有语言和国际地区信息的 Locale
Locale local1 = new Locale("zh","CN");
//带有语言信息的 Locale
Locale local2 = new Locale("zh");
//等同于 Locale("zh","CN");
Locale locale3 = Locale.CHINA;
//等同于 Locale("zh");
Locale locale4 = Locale.CHINESE;
//获取默认Locale
Locale defaultLocale = Locale.getDefault();
java -Duser.language=zh -Duser.region=CN MainClass
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,locale);
String fmtStr = dateFormat.format(date); //fmtStr = "Dec 1, 2017"
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM,locale);
String fmtStr = dateFormat.format(date); //fmtStr = "4:46:21 PM"
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale6);
String fmtStr = dateFormat.format(date); //fmtStr = "Dec 1, 2017 4:48:06 PM"
double value = 2333.33;
Locale locale = new Locale("en","US");
NumberFormat numberFmt = NumberFormat.getInstance(locale);
String fmtStr = numberFmt.format(value); //fmtStr = "2,333.33"
double value = 2333.33;
Locale locale = new Locale("zh","CN");
NumberFormat numberFmt = NumberFormat.getCurrencyInstance(locale);
String fmtStr = numberFmt.format(value); //fmtStr = "¥2,333.33"
//将一个本地化的货币字符串转化为double类型;
String currStr = "¥2,333.33";
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number num = numberFormat.parse(currStr);
double value = num.doubleValue(); //value=2333.33
//格式化模板字符串
String pattern1 = "{0},您于 {1,date,short} 支付 {2,number,currency} ";
String pattern2 = "{0} ,you paid {2,number,currency} at {1,date,short} ";
//动态替换格式化字符串的占位符的参数
Object[] params = {"Al-assad",new Date(),1.0E3};
//使用指定本地化对象格式化信息
MessageFormat msgFmt1 = new MessageFormat(pattern1,Locale.CHINA);
MessageFormat msgFmt2 = new MessageFormat(pattern2,Locale.US);
String msg1 = msgFmt1.format(params); //"Al-assad,您于 17-12-1 支付 ¥1,000.00 "
String msg2 = msgFmt2.format(params); //"Al-assad ,you paid $1,000.00 at 12/1/17 "
<资源名>_<语言代码>_<国家/地区代码>.properties
resource.properties //默认资源包,但程序查到不到当地的本地化资源包时,使用该资源包
resource_zh_CN.properties //中文资源包
resource_en_US.properties //英文资源包
greeting.common=How are you!
greeting.morning=Good Morning!
greeting.afternoon=Good Afternoon!
greeting.common=\u4F60\u597D!
greeting.morning=\u65E9\u4E0A\u597D!
greeting.afternoon=\u4E0B\u5348\u597D\uFF01
native2ascii [-encoding 编码] [输入文件 [输出文件]]
native2ascii -encoding utf-8 d:\resource_zh_CN_orginal.properties d:\resource_zh_CN.properties
//使用系统用默认Locale获取资源包
Locale locale = Locale.getDefault();
ResourceBundle resourceBundle = ResourceBundle.getBundle("site/assad/i18n/resource"); //指定资源包路径,基于类路径
//获取资源包中的对应键值
String common = resourceBundle.getString("greeting.common"); //"你好!"
String morning = resourceBundle.getString("greeting.morning"); //"早上好!"
String afternoon = resourceBundle.getString("greeting.afternoon"); //"下午好!"
//指定Locale获取资源包
ResourceBundle resourceBundle2 = ResourceBundle.getBundle("site/assad/i18n/resource",Locale.US);
String common2 = resourceBundle2.getString("greeting.common"); //"How are you!"
greeting.common=How are you!{0},today is {1,date,short}
greeting.morning=Good morning!{0},now is {1,time,short}
greeting.afternoon=Good afternoon!{0},now is{1,time,short}
//加载本地化资源
ResourceBundle resourceBundle1 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.US);
ResourceBundle resourceBundle2 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.CHINA);
//动态替换占位符参数
Object[] params = {"Al-assad",new Date()};
//获取本地化键值,并对其进行格式化
String common1 = new MessageFormat(resourceBundle1.getString("greeting.common"),Locale.US).format(params);
String morning1 = new MessageFormat(resourceBundle1.getString("greeting.morning"),Locale.US).format(params);
String afternoon1 = new MessageFormat(resourceBundle1.getString("greeting.afternoon"),Locale.US).format(params);
String common2 = new MessageFormat(resourceBundle2.getString("greeting.common"),Locale.CHINA).format(params);
String morning2 = new MessageFormat(resourceBundle2.getString("greeting.morning"),Locale.CHINA).format(params);
String afternoon2 = new MessageFormat(resourceBundle2.getString("greeting.afternoon"),Locale.CHINA).format(params);
System.out.println(common1+"\n"+morning1+"\n"+afternoon1+"\n"+common2+"\n"+morning2+"\n"+afternoon2);
/*output:
How are you!Al-assad,today is 12/1/17
Good morning!Al-assad,now is 2:22 PM
Good afternoon!Al-assad,now is2:22 PM
你好!Al-assad,今天是 17-12-1
上午好!Al-assad,现在的时间是 下午2:22
下午好!Al-assad,现在的时间是 下午2:22
* */