1. 什么是国际化和本地化:
I. 本地化:一个软件在某个国家或地区使用时,采用该国家或地区的语言,数字,货币,日期等习惯。
II. 国际化:软件开发时,让它能支持多个国家和地区的本地化应用。使得应用软件能够适应多个地区的语言和文化风俗习惯
III. 本地敏感数据: 随用户区域信息而变化的数据称为本地信息敏感数据。例如数字,货币, 日期,时间等数据
Locale: Java 中表示国家或地区的类. JDK 中提供了很多常量.
也可以通过 Locale(languageCode, countryCode) 的方式来创建
在 WEB 应用中可以通过 request.getLocale() 方法来获取.
@Test
public void testLoacle() {
Locale locale = Locale.CHINA;
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getCountry());
System.out.println(locale.getLanguage());
System.out.println("-----------------");
locale = new Locale("en", "US");
System.out.println(locale.getDisplayCountry());
System.out.println(locale.getCountry());
System.out.println(locale.getLanguage());
}
输出
中国
CN
zh
-----------------
美国
US
en
DateFormat: 格式化日期的工具类.
DateFormate 本身是一个抽象类.
1. 若只希望通过 DateFormat 把一个 Date 对象转为一个字符串, 则可以通过 DateFormat 的工厂方法来获取 DateFormat 对象
2. 可以获取只格式化 Date 的 DateFormat 对象: getDateInstance(int style, Locale aLocale)
3. 可以获取只格式化 Time 的 DateFormat 对象: getTimeInstance(int style, Locale aLocale)
4. 可以获取既格式化 Date, 也格式化 Time 的 DateFormat 对象:
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
5. 其中 style 可以取值为: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale 则为代表国家地区的 Locale 对象
6. 通过 DateFormat 的 format 方法来格式化个 Date 对象到字符串.
7. 若有一个字符串, 如何解析为一个 Date 对象呢 ?
I. 先创建 DateFormat 对象: 创建 DateFormat 的子类 SimpleDateFormat 对象
SimpleDateFormat(String pattern).
其中 pattern 为日期, 时间的格式, 例如: yyyy-MM-dd hh:mm:ss
II. 调用 DateFormat 的 parse 方法来解析字符串到 Date 对象
@Test
public void testDateFormat(){
Locale locale = Locale.US;
Date date = new Date();
System.out.println(date);
System.out.println("-------");
//获取DateFormat对象
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
String str = format.format(date);
System.out.println(str);
}
输出
Thu Aug 31 23:07:30 CST 2017
-------
August 31, 2017 11:07:30 PM
@Test
public void testDateFormat2() throws ParseException{
String str ="1991-03-11 07:10:10";
DateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dateFormat.parse(str);
System.out.println(date);
}
1. 通过工厂方法获取 NumberFormat 对象
NumberFormat.getNumberInstance(locale); //仅格式化为数字的字符串
NumberFormat.getCurrencyInstance(locale); //格式为货币的字符串
2. 通过 format 方法来进行格式化
3. 通过 parse 方法把一个字符串解析为一个 Number 类型.
@Test
public void testNumberFormat() throws ParseException{
double d =12345435.123;
Locale locale = Locale.CHINA;
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
String str= numberFormat.format(d);
System.out.println(str);
NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
String str2 = numberFormat2.format(d);
System.out.println(str2);
System.out.println("--------");
str ="12,345,435.123";
d =(double) numberFormat.parse(str);
System.out.println(d);
str2 ="¥12,345,435.12";
d = (double) numberFormat2.parse(str2);
System.out.println(d);
}
输出
12,345,435.123
¥12,345,435.12
--------
1.2345435123E7
1.234543512E7
MessageFormat: 可以格式化模式字符串
模式字符串: 带占位符的字符串: "Date: {0}, Salary: {1}"
可以通过 format 方法会模式字符串进行格式化
@Test
public void testMessageFormat(){
String str ="Date:{0},Salary:{1}";
Locale locale = Locale.CHINA;
Date date = new Date();
double salary = 12345.12;
DateFormat dateformat = DateFormat.getDateInstance(DateFormat.MEDIUM,locale);
String dateStr = dateformat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(salary);
String result = MessageFormat.format(str, dateStr,salStr);
System.out.println(result);
}
输出
Date:2017-8-31,Salary:¥12,345.12
ResourceBundle: 资源包类.
1. 在类路径下需要有对应的资源文件: baseName.properties. 其中 baseName 是基名.
2. 可以使用 基名_语言代码_国家代码.properties 来添加不同国家或地区的资源文件. i18n_zh_CN.properties
3. 要求所有基名相同的资源文件的 key 必须完全一致.
4. 可以使用 native2ascii 命令来得到 汉字 对一个的 asc 码. Eclipse 内置了工具
5. 可以调用 ResourceBundle 的 getBundle(基名, Locale 实例) 获取获取 ResourceBundle 对象
6. 可以调用 ResourceBundle 的 getString(key) 来获取资源文件的 value 字符串的值.
7. 结合 DateFormat, NumberFormat, MessageFormat 即可实现国际化.
@Test
public void testResourceBundle(){
Locale locale = Locale.CHINA;
ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale);
System.out.println(resourceBundle.getString("date"));
System.out.println(resourceBundle.getString("salary"));
System.out.println("-----");
String dateLabel = resourceBundle.getString("date");
String salLabel = resourceBundle.getString("salary");
String str = "{0}:{1} {2}:{3}";
Date date = new Date();
double sal = 12345.12;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal);
String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
System.out.println(result);
}
i18n_en_US.properties
i18n.properties
date=Date
salary=Salary
i18n_zh_CN.properties
date=日期
salary=工资
输出
日期
工资
-----
日期:2017-8-31 工资¥12,345.12
index.jsp
<%@page import="java.util.Locale"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Date date = new Date();
request.setAttribute("date", date);
request.setAttribute("salary", 123345.75);
%>
<fmt:bundle basename="i18n">
<fmt:message key="date"></fmt:message>:
<fmt:formatDate value="${date}" />
<fmt:message key="salary"></fmt:message>:
<fmt:formatNumber value="${salary}"></fmt:formatNumber>
</fmt:bundle>
<br>
<br>
<%
String code = request.getParameter("code");
if (code != null) {
if ("en".equals(code)) {
session.setAttribute("locale", Locale.US);
} else if ("zh".equals(code)) {
session.setAttribute("locale", Locale.CHINA);
}
}
%>
<c:if test="${sessionScope.locale!=null }">
<fmt:setLocale value="${sessionScope.locale}" />
</c:if>
<fmt:bundle basename="i18n">
<fmt:message key="date"></fmt:message>:
<fmt:formatDate value="${date}" />
<fmt:message key="salary"></fmt:message>:
<fmt:formatNumber value="${salary}"></fmt:formatNumber>
</fmt:bundle>
<a href="index.jsp?code=en">English</a>
<a href="index.jsp?code=zh">中文</a>
</body>
</html>
更改浏览器语言,第一行会随浏览器语言变化
点击English 或中文 第二行会随点击语言变化