例1、Locale类的用法
package cn.itcast.test;
import java.util.Locale;
public class TestLocale {
public static void main(String[] args) {
Locale locale=Locale.CHINA;
System.out.println(locale.getCountry());//CN 国家代码
System.out.println(locale.getLanguage());//zh 语言代码
//获得此java虚拟机实例的当前默认语言环境值
locale = Locale.getDefault();
System.out.println(locale);//zh_CN
System.out.println("************************************");
locale=Locale.US;
System.out.println(locale.getCountry());//US 国家代码
System.out.println(locale.getLanguage());//en 语言代码
}
}
例2、DateFormat类的用法举例。
package cn.itcast.test;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
public class TestDateFormat {
public static void main(String[] args) throws ParseException {
Locale locale=Locale.CHINA;
Date date = new Date();
DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT, locale);
//讲一个Date格式化为日期/时间字符串
String source=df.format(date);
System.out.println(source);//12-10-26
df=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
source=df.format(date);
System.out.println(source);//2012-10-26
df=DateFormat.getDateInstance(DateFormat.LONG, locale);
source=df.format(date);
System.out.println(source);//2012年10月26日
System.out.println("***********************************************");
locale=Locale.US;
date = new Date();
df=DateFormat.getDateInstance(DateFormat.SHORT, locale);
//讲一个Date格式化为日期/时间字符串
source=df.format(date);
System.out.println(source);//10/26/12
df=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
source=df.format(date);
System.out.println(source);//Oct 26, 2012
df=DateFormat.getDateInstance(DateFormat.LONG, locale);
source=df.format(date);
System.out.println(source);//October 26, 2012
System.out.println("***********************************************");
//从给定字符串的开始位置解析文本,以生成一个日期
locale=Locale.CHINA;
df=DateFormat.getDateInstance(DateFormat.LONG, locale);
source="2012年10月26日";//上面应该设置为长格式
System.out.println(df.parse(source).toString());
}
}
例3、NumberFormat的用法。
package cn.itcast.test;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class TestNumberFormat {
public static void main(String[] args) throws ParseException {
Locale locale=Locale.CHINA;
NumberFormat nf=NumberFormat.getInstance(locale);
double number=1223456789.34;
//格式规范
String source = nf.format(number);
System.out.println(source);//1,223,456,789.34
System.out.println("***************************************");
nf=NumberFormat.getInstance(Locale.FRANCE);
source = nf.format(number);
System.out.println(source);//1?223?456?789,34
System.out.println("***************************************");
source="1,223,456,789.34";
nf=NumberFormat.getInstance(Locale.CHINA);
System.out.println(nf.parse(source));
System.out.println("***************************************");
}
}
例4、MessageFormat案例介绍。
package cn.itcast.test;
import java.text.MessageFormat;
public class TestMessageFormat {
public static void main(String[] args) {
//pattern="I worked at itcast and good!"
String pattern="I {0} at {1} and {2}!";
MessageFormat mf=new MessageFormat(pattern);
//格式化一个对象数组,并将messageFormat的模式添加到所提供的StringBuffer
Object[] param=new Object[]{"worked","itcast","good"};
StringBuffer sb=new StringBuffer();
mf.format(param, sb, null);
System.out.println(sb.toString());//I worked at itcast and good!
/*
* 如果 String pattern="I {1} at {0} and {2}!";
* 则输出为:I itcast at worked and good!
*/
}
}
例5、资源包应用之ResourceBundle。
第一步、建立三个资源包(注意文件名即路径)
1. resource_zh_CN.properties
username=username_zhongwen
password=password_zhongwen
submit=login_zhongwen
- resource_en_US.properties
username=username_en
password=password_en
submit=login_en
第二步、
package cn.itcast.test;
import java.util.Locale;
import java.util.ResourceBundle;
public class TestResourceBundle {
public static void main(String[] args) {
Locale locale=Locale.CHINA;
String baseName = "resource";
readResource(baseName, locale);//username_zhongwen:password_zhongwen:login_zhongwen
/*****************************************************************************
Locale locale=Locale.US;
String baseName="resource";
ResourceBundle rb=ResourceBundle.getBundle(baseName, locale);
String username = rb.getString("username");
String password = rb.getString("password");
String submit=rb.getString("submit");
System.out.println(username+":"+password+":"+submit);
//username_en:password_en:login_en
******************************************************************************/
}
public static void readResource(String baseName,Locale locale){
ResourceBundle rb=ResourceBundle.getBundle(baseName, locale);
String username = rb.getString("username");
String password = rb.getString("password");
String submit=rb.getString("submit");
System.out.println(username+":"+password+":"+submit);
}
}
第三步、如果resource_zh_CN.properties改为一下内容,应当如何处理才能不产生乱码问题呢?
username=用户名
password=密码
submit=登陆
- 新建一个resource_zh_CN.txt文件(在Src目录下面)
- 找到环境变量path中对应的目录,里面有个native2ascii.exe文件,进行字符编码
具体方法:在DOS中执行以下内容:
native2ascii –encoding gb2312 resource_zh_CN.txt resource_zh_CN.properties
同时删除原来的resource_zh_CN.properties文件,然后双击run.bat 即可将中文转为ASCII
转码后的resource_zh_CN.properties为:
username=\u7528\u6237\u540d
password=\u5bc6\u7801
submit=\u767b\u9646
然后执行TestResourceBundle.java便可以得到如下内容:
用户名:密码:登陆
总结:
国际化原理:
1. 针对不同国家定义不同的资源文件 基名是(resource)
资源文件的后缀名是properties 文件中保存文件的方式是key = value
在各个资源文件中key值相同的,而value是不同的
1.1 中国 定义的资源文件名称是:resource_zh_CN.properties
username=\u7528\u6237\u540D
password=\u5BC6\u7801
submit=\u767B\u9646
美国 定义的资源文件名称是:resource_zh_CN.properties
username=username_en
password=password_en
submit=login_en
默认的资源文件 resource.properties
*当要查找的资源文件不存在时,读取的该资源文件
2.读取资源文件:
使用ResourceBundle.getBundle(baseName,locale);方法,它会根据基名和区域名称自动查找对应的资源文件:
基名:baseName=resource
区域:locale=Locale.CHINA;
3. 中文要进行字符编码
3.1 定义jdk的环境变量
3.2 随便定义一个保存中文信息的文件(.txt),使用批处理文件进行文件的转码:
native2ascii –encoding gb2312 resource_zh_CN.txt resource_zh_CN.properties
例6、登陆页面的国际化
.properties文件与例5相同。
第一步、login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@page import="java.text.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
……
<%
//获取服务器首选区域
Locale locale = request.getLocale();
System.out.println("locale:"+locale);//locale:zh_CN 在IE中改了语言之后:locale:en_US
//获取浏览器中选择的所有语言
Enumeration e=request.getLocales();
while(e.hasMoreElements()){
System.out.println(" "+e.nextElement());//zh_CN en_US
}
//处理系统的时间
DateFormat df=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale);
String dateStr=df.format(new Date());
//国际化
ResourceBundle rb=ResourceBundle.getBundle("resource",locale);
%>
时间是:<%=dateStr %><br/>
<form action="" method="post">
<table border="1">
<tr>
<td style="text-align: right"><%=rb.getString("username") %>:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td style="text-align: right"><%=rb.getString("password") %></td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td style="text-align: right"><input type="submit" value="<%=rb.getString("submit") %>"/></td>
<td><input type="reset" value="重置"/></td>
……
第二步、验证,在IE中键入:http://localhost:8080/day19_i18nWeb/login.jsp
然后选择浏览器的工具–>常规->语言
然后上移相应内容,刷新页面可以看到页面显示不同内容!
例7、登陆页面的国际化—使用标签库
第一步、将上面的第一步改为如下内容:login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
……
<%
Date date=new Date();
pageContext.setAttribute("date",date);
%>
时间是:<fmt:formatDate value="${date}" dateStyle="MEDIUM" timeStyle="MEDIUM" type="both"/>
<fmt:bundle basename="resource">
<!—-上面这行的标签对等同于<fmt:setBundle basename="resource"/> -- >
<form action="" method="post">
<table border="1">
<tr>
<td style="text-align: right"><fmt:message key="username"/>:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td style="text-align: right"><fmt:message key="password"/>:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td style="text-align: right"><fmt:message key="submit"/>:</td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</fmt:bundle>
</body>
</html>
第二步、验证
例8、登陆页面的国际化—使用标签库实现中英文切换
将例7中第一步改为如下内容:login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@page import="java.text.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
……
<%
//定义locale的默认值
Locale l=Locale.CHINA;
String slocale = request.getParameter("locale");
if(slocale!=null){
if("zh".equals(slocale.trim())){
l=Locale.CHINA;
}
if("en".equals(slocale.trim())){
l=Locale.US;
}
}
pageContext.setAttribute("l",l);
Date date=new Date();
pageContext.setAttribute("date",date);
%>
<!-- 改变语言环境 -->
<fmt:setLocale value="${l}"/>
时间是:<fmt:formatDate value="${date}" dateStyle="MEDIUM" timeStyle="MEDIUM" type="both"/>
<fmt:setBundle basename="resource"/>
<form action="" method="post">
<table border="1">
<tr>
<td style="text-align: right"><fmt:message key="username"/>:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td style="text-align: right"><fmt:message key="password"/>:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td style="text-align: right"><fmt:message key="submit"/>:</td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
<a href="${pageContext.request.contextPath}/loginch_en.jsp?locale=zh">中文</a>
<a href="${pageContext.request.contextPath}/loginch_en.jsp?locale=en">English</a>
</body>
</html>
第二步、验证