Java 对国际化 (i18n) 的支持&资源包的调用

来源:互联网 发布:淘宝评价怎么看不到 编辑:程序博客网 时间:2024/06/05 07:43
Java 国际化 i18n

国际化信息也成为本地化信息,一般需要2个条件才能确定一个特定的国际化信息,分别是:“语言类型” 和 “国家/地区信息”,Java 为国际化提供了一系列类支持;
常用的“语言类型” 和 “国家/地区信息”代码如下:
语言类型代码:
中文zh英语en日语ja法语fr
国家/地区代码:
中国大陆CN中国香港HK美国US英国EN
完整语言代码可以参见:http://www.lingoes.cn/zh/translator/langcode.htm
完整国家/地区代码可以参见:http://www.loc.gov/standards/iso639-2/php/English_list.php


以下示例代码的完整代码地址:https://gitee.com/assad/springframework-test-i18n

Locale

java.util.Locale 是表示“语言”和“国家/地区”的本地化类,是 Java 创建国际化应用的基础,以下是一些示例使用:
 
//带有语言和国际地区信息的 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();
如果在测试时需要改变系统默认的本地化设置,可以在启动JVM时通过命令参数指定:
 
java -Duser.language=zh -Duser.region=CN MainClass 




本地化工具类


Java 的 java.text 包提供了一系列支持本地化格式化操作的工具类,如:NumberFormat,DateFormat,MessageFormat

DateFormat

java.text.DateFormat 用于对日期时间进行本地化格式化操作,本身实现为一个工厂类,可以通过 DataFormat.getXxxInstance() 获取实例;

① 格式化日期

 
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"
其中 DataFormat.getXxxInstance() 的 dateStyle / timeStyle 参数包含以下可用常量:
  • DateFormat.SHORT:如:7/24/98   4:49 PM
  • DateFormat.MEDIUM:如 :24-Jul-98,4:49:05 PM
  • DateFormat.LONG:如:July 24, 1998  4:49:05 PM EST
  • DateFormat.FULL:如:Friday,July 24, 1998    4:49:05 o'clock PM EST


NumberFormat

java.text.NumberFormat 用于对数字进行本地化格式化操作,身实现为一个工厂类,可以通过 DataFormat.getXxxInstance() 获取实例;
如:DataFormat.geCurrencyInstance() 格式化数字为本地化货币字符串;
        DataFormat.gePercentInstance() 格式化数字为本地化百分比字符串等;

① 普通数字格式化

 
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"

③ 本地化数字字符串转换为数字

使用 NumberFromat 实例的 parse() 方法,可以将本地化货币字符串,百分比字符串,本地化数字字符串等转换回 java.lang.Number 的一个实例;
 
//将一个本地化的货币字符串转化为double类型;
String currStr = "¥2,333.33";
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number num = numberFormat.parse(currStr);
double value = num.doubleValue();   //value=2333.33


MessageFormat

java.text.MessageFormat 在 DateFormat , NumberFormat 的基础上提供了强大的占位符字符串格式化功能,支持时间、数字、对象属性的格式化;
 
//格式化模板字符串
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 "




资源包 和 ResourceBundle


资源包


在国际化应用中,国际化信息一般以 .properties 文件中键值对的形式存在,国际化资源包的命名格式如下:
 
<资源名>_<语言代码>_<国家/地区代码>.properties
一个项目包含典型资源包文件如下:
 
resource.properties       //默认资源包,但程序查到不到当地的本地化资源包时,使用该资源包
resource_zh_CN.properties   //中文资源包
resource_en_US.properties   //英文资源包
......

资源包的示例代码如下:
resource.properties
 
greeting.common=How are you!
greeting.morning=Good Morning!
greeting.afternoon=Good Afternoon!
resource_zh_CN.properties
 
greeting.common=\u4F60\u597D!
greeting.morning=\u65E9\u4E0A\u597D!
greeting.afternoon=\u4E0B\u5348\u597D\uFF01
注意在.properties 文件中只能存在ASCII字符,必须将非ASCII字符(如中文字符)转换为相应的Unicode码,以上resource_zh_CN.properties是经过转换后的结果,原本编写时的 Native 编码内容如下:
greeting.common=你好!
gretting.morning=早上好!
gretting.afternoon=下午好!

可以使用 native2ascii.jar 工具实现这一过程,调用的语法如下:
 
native2ascii [-encoding 编码] [输入文件 [输出文件]]
示例如下:
 
native2ascii -encoding utf-8 d:\resource_zh_CN_orginal.properties d:\resource_zh_CN.properties
如果开发环境为 Intellij IDEA,可以通过设置【File -> settings -> Editor -> File Encodings -> 勾选 Transparent native-to-ascii conversion】,此时在 IDEA 中可以直接编写 native 的 properties 文件,IDEA 会自动将 native 字符转化为 ascii 字符,同时在 IDEA 视图只能仍然以 native 的方式显示,这对于开发调试十分方方便;


ResouceBundle

java.util.ResourceBundle 用于获取国际化资源包中的键值;

获取国际化资源包中的键值

完整示例代码模块:site.assad.i18n/ResourceBoundle.class,site.assad.i18n/resource
 
        //使用系统用默认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!"

在资源文件中使用占位符模板字符串

可以在资源文件中使用占位符模板字符串,同时结合 MessageFormat ,就可以实现在运行时灵活构造国际化信息;
完整示例代码模块:site.assad.i18n/FormatResourceBoundle.class,site.assad.i18n/fmt_resource

fmt_resource.properties
 
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
 * */


原创粉丝点击