一:介绍

1.国际化

  国际化_字符串

 

2.特征

  国际化_字符串_02

 

3.解决方案

  国际化_资源文件_03

 

二:Locale类

1.介绍

  国际化_html_04

 

2.示例程序

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%=request.getLocale() %>
11 </body>
12 </html>

 

3.效果

  运行后的结果:

  国际化_字符串_05

 

4.测试类

 1 package com.caojun.i18n;
 2 
 3 import static org.junit.Assert.*;
 4 import java.util.Locale;
 5 import org.junit.Test;
 6 
 7 public class I18nTest {
 8 
 9     @Test
10     public void testLocale() {
11         Locale locale=Locale.CHINA;
12         System.out.println(locale.getLanguage());
13         System.out.println(locale.getDisplayCountry());
14     }
15 
16 }

 

5.效果

  国际化_java_06

 

三:DateFormat类

1.介绍

  国际化_资源文件_07

  国际化_html_08

 

2.测试

/**
* 日期转为对应国家的日期字符串
*/

 1 @Test
 2     public void testDateformat() {
 3         Date date=new Date();
 4         System.out.println(date);
 5         
 6         //创建DateFormat对象
 7         Locale locale=Locale.CHINA;
 8         DateFormat dateformat=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);   //dateStyle=medium
 9         String str=dateformat.format(date);
10         System.out.println(str);
11         
12         dateformat=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale);      //dateStyle=full
13         str=dateformat.format(date);
14         System.out.println(str);
15     }

 

3.效果

  国际化_java_09

 

4.测试

/**
* 日期字符串转为date类型
* @throws Exception
*/

1 @Test
2     public void testDateformat2() throws Exception {
3         String str="1990-12-12 12:12:12";
4         DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
5         Date date=dateFormat.parse(str);
6         System.out.println(date);
7     }

 

5.效果

  国际化_字符串_10

 

四:NumberFormat类

1.介绍

  国际化_bundle_11

 

2.测试

  getNumberInstance:格式化为数字的字符串

  getCurrencyInstance:格式化为货币的字符串

 1 /**
 2      * 转为对应国家的字符串
 3      */
 4     @Test
 5     public void testNumberformat() {
 6         double d=123243223.23d;
 7         Locale locale=Locale.US;
 8         //转为对应的字符串
 9         NumberFormat numberFormat=NumberFormat.getNumberInstance(locale);
10         String str=numberFormat.format(d);
11         System.out.println(str);
12         //货币
13         numberFormat=NumberFormat.getCurrencyInstance(locale);
14         str=numberFormat.format(d);
15         System.out.println(str);
16         
17     }

 

3.效果

  国际化_java_12

 

4.测试

 1 /**
 2      * 转为对应的数字
 3      * @throws ParseException 
 4      */
 5     @Test
 6     public void testNumberformat2() throws ParseException {
 7         String str="123,223.23";
 8         Locale locale=Locale.US;
 9         NumberFormat numberFormat=NumberFormat.getNumberInstance(locale);
10         double d=(double) numberFormat.parse(str);
11         System.out.println(d);
12         
13         str="$123,223.23";
14         numberFormat=NumberFormat.getCurrencyInstance(locale);
15         d=(double) numberFormat.parse(str);
16         System.out.println(d);
17     }

 

5.效果

  国际化_bundle_13

 

五:MessageFormat类

1.介绍

  国际化_java_14

  模式字符串:

  国际化_资源文件_15

 

2.测试

 1 /**
 2      * messaheFormat的格式化模式字符串
 3      */
 4     @Test
 5     public void testMessageformat() {
 6         String str="Date:{0},salary:{1}";
 7         Locale locale=Locale.CHINA;
 8         Date date=new Date();
 9         double sa1=1234.12;
10         
11         DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
12         String datestr=dateFormat.format(date);
13         NumberFormat numberFormat=NumberFormat.getCurrencyInstance(locale);
14         String salstr=numberFormat.format(sa1);
15         
16         String result=MessageFormat.format(str,datestr,salstr);
17         System.out.println(result);
18     }

 

3.效果

  国际化_java_16

 

六:ResourceBundle类

1.介绍

  国际化_字符串_17

  国际化_java_18

  国际化_html_19

  国际化_资源文件_20

 

2.项目目录

  国际化_bundle_21

 

3.i8n.properties

  国际化_html_22

 

4.i8n_en_US.properties

  国际化_java_23

 

5.i8n_zh_CN.properties

  国际化_字符串_24

 

6.测试程序

 1 @Test
 2     public void testResourceBundle() {
 3         Locale locale=Locale.CHINA;
 4         ResourceBundle resourcebundle=ResourceBundle.getBundle("i8n", locale);
 5         System.out.println(resourcebundle.getString("salary"));
 6         System.out.println(resourcebundle.getString("date"));
 7         
 8         String dateLabel=resourcebundle.getString("date");
 9         String salaryLabel=resourcebundle.getString("salary");
10         String str="{0}:{1},{2}:{3}";
11         
12         Date date=new Date();
13         double sal=123.43;
14         
15         DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
16         String dateStr=dateFormat.format(date);
17         
18         NumberFormat numberFormat=NumberFormat.getCurrencyInstance(locale);
19         String salStr=numberFormat.format(sal);
20         
21         String result=MessageFormat.format(str, dateLabel,dateStr,salaryLabel,salStr);
22         System.out.println(result);
23     }

 

7.效果

  国际化_字符串_25

 

8.程序中注意点

  在类路径下需要有对应的资源文件:basenName.properties,其中baseName是基名

  可以使用基名_语言代码_国家代码.properties 来添加不同国家或者地区的资源文件

  所有的资源文件的key必须相同

  可以调用ResourceBundle类的方法获取对象

  可以使用native2ascii命令得到汉字对应的ascii码,也可以是使用eclipse内置的工具

 

七:国际化格式标签

1.fmt

  使用标签fmt进行web前端的国际化开发。

  需要导入el的lib包。