(1)国际化概述
软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。国际化(internationalization)又称为 i18n(读法为i 18 n,据说是因为internationalization(国际化)这个单词从i到n之间有18个英文字母,i18n的名字由此而来)
(2)创建资源包和资源文件
(3)编程实现固定文本的国际化
在JavaAPI中提供了一个ResourceBundle 类用于描述一个资源包,并且 ResourceBundle类提供了相应的方法getBundle,这个方法可以根据来访者的国家地区自动获取与之对应的资源文件予以显示。ResourceBundle类提供了一个静态方法getBundle,该方法用于装载资源文件,并创建ResourceBundle实例:
Locale currentLocale = Locale.getDefault();
ResourceBundle myResources =ResourceBundle.getBundle(basename, currentLocale);
basename为资源包基名(且必须为完整路径)。
如果与该locale对象匹配的资源包子类找不到。一般情况下,则选用默认资源文件予以显示。 加载资源文件后, 程序就可以调用ResourceBundle 实例对象的 getString 方法获取指定的资源信息名称所对应的值。
String value = myResources.getString(“key");
import java.util.Locale;
import java.util.ResourceBundle;
/**
* @ClassName: I18NTest
* @Description: 编程实现固定文本的国际化
* @author: 孤傲苍狼
* @date: 2014-8-29 下午9:34:05
*
*/
public class I18NTest {
public static void main(String[] args) {
//资源包基名(包名+myproperties)
String basename = "me.gacl.i18n.resource.myproperties";
//设置语言环境
Locale cn = Locale.CHINA;//中文
Locale us = Locale.US;//英文
//根据基名和语言环境加载对应的语言资源文件
ResourceBundle myResourcesCN = ResourceBundle.getBundle(basename,cn);//加载myproperties_zh.properties
ResourceBundle myResourcesUS = ResourceBundle.getBundle(basename,us);//加载myproperties_en.properties
//加载资源文件后, 程序就可以调用ResourceBundle实例对象的 getString方法获取指定的资源信息名称所对应的值。
//String value = myResources.getString(“key");
String usernameCN = myResourcesCN.getString("username");
String passwordCN = myResourcesCN.getString("password");
String usernameUS = myResourcesUS.getString("username");
String passwordUS = myResourcesUS.getString("password");
System.out.println(usernameCN+"--"+passwordCN);
System.out.println(usernameUS+"--"+passwordUS);
}
}
(4)在WEB应用中实现固定文本的国际化
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>国际化(i18n)测试</title>
</head>
<%
//加载i18n资源文件,request.getLocale()获取访问用户所在的国家地区
ResourceBundle myResourcesBundle = ResourceBundle.getBundle("me.gacl.i18n.resource.myproperties",request.getLocale());
%>
<body>
<form action="" method="post">
<%=myResourcesBundle.getString("username")%>:<input type="text" name="username"/><br/>
<%=myResourcesBundle.getString("password")%>:<input type="password" name="password"/><br/>
<input type="submit" value="<%=myResourcesBundle.getString("submit")%>">
</form>
</body>
</html>
(5)动态数据的国际化
1、数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)
2、Locale 类
Locale 实例对象代表一个特定的地理,政治、文化区域。一个 Locale 对象本身不会验证它代表的语言和国家地区信息是否正确,只是向本地敏感的类提供国家地区信息,与国际化相关的格式化和解析任务由本地敏感的类去完成。(若JDK中的某个类在运行时需要根据 Locale 对象来调整其功能,这个类就称为本地敏感类)
3、DateFormat类(日期格式化)
DateFormat 类可以将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符串。DateFormat 类除了可按国家地区格式化输出日期外,它还定义了一些用于描述日期/时间的显示模式的 int 型的常量,包括FULL, LONG, MEDIUM, DEFAULT, SHORT,实例化DateFormat对象时,可以使用这些常量,控制日期/时间的显示长度。
4、实例化DateFormat类
实例化DateFormat类有九种方式,以下三种为带参形式,下面列出的三种方式也可以分别不带参,或只带显示样式的参数。
getDateInstance(int style, Locale aLocale):以指定的日期显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理时间值部分。
getTimeInstance(int style, Locale aLocale):以指定的时间显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理日期值部分。
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale):以单独指定的日期显示模式、时间显示模式和本地信息来获得DateFormat实例对象。
5、DateFormat 对象的方法
format:将date对象格式化为符合某个本地环境习惯的字符串。parse:将字符串解析为日期/时间对象
注意:parse和format完全相反,一个是把date时间转化为相应地区和国家的显示样式,一个是把相应地区的时间日期转化成date对象,该方法在使用时,解析的时间或日期要符合指定的国家、地区格式,否则会抛异常。DateFormat 对象通常不是线程安全的,每个线程都应该创建自己的 DateFormat 实例对象
6、DateFormat使用范例
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
/**
* @ClassName: NumberFormatTest
* @Description: NumberFormat类测试
* @author: 孤傲苍狼
* @date: 2014-8-29 下午10:25:29
*
*/
public class NumberFormatTest {
public static void main(String[] args) throws ParseException {
int price = 89;
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
String result = nf.format(price);
System.out.println(result);
String s = "¥89.00";
nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number n = nf.parse(s);
System.out.println(n.doubleValue() + 1);
double num = 0.5;
nf = NumberFormat.getPercentInstance();
System.out.println(nf.format(num));
}
}
(6)MessageFormat(文本格式化)
1、如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。例如:At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage。以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。MessageFormat 类如何进行批量处理呢?
1.MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。
2.MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
2、模式字符串与占位符
模式字符串:
At {0} on {1},a destroyed {2} houses and caused {3} of damage
字符串中的{0}、{1}、{2}、{3}就是占位符
3、格式化模式字符串
1、实例化MessageFormat对象,并装载相应的模式字符串。
2、使用format(object obj[])格式化输出模式字符串,参数数组中指定占位符相应的替换对象。
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
/**
* @ClassName: MessageFormatTest
* @Description: MessageFormat类测试
* @author: 孤傲苍狼
* @date: 2014-8-29 下午10:29:19
*
*/
public class MessageFormatTest {
public static void main(String[] args) {
//模式字符串
String pattern = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";
//实例化MessageFormat对象,并装载相应的模式字符串
MessageFormat format = new MessageFormat(pattern, Locale.CHINA);
Object arr[] = {new Date(), 99, 100000000};
//格式化模式字符串,参数数组中指定占位符相应的替换对象
String result = format.format(arr);
System.out.println(result);
}
}
4、占位符的三种书写方式
{argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号
{argumentIndex,formatType}: 参数的格式化类型
{argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
/**
* @ClassName: MessageFormatTest
* @Description: MessageFormat类测试
* @author: 孤傲苍狼
* @date: 2014-8-29 下午10:29:19
*
*/
public class MessageFormatTest {
public static void main(String[] args) {
//模式字符串
String pattern = "At {0, time, short} on {0, date}, a destroyed {1} houses and caused {2, number, currency} of damage.";
//实例化MessageFormat对象,并装载相应的模式字符串
MessageFormat format = new MessageFormat(pattern, Locale.US);
Object arr[] = {new Date(), 99, 100000000};
//格式化模式字符串,参数数组中指定占位符相应的替换对象
String result = format.format(arr);
System.out.println(result);
}
}
(7)在WEB应用中使用国际化标签库实现固定文本的国际化
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--导入国际化标签库 --%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML>
<html>
<head>
<title>国际化(i18n)测试</title>
</head>
<%--
//加载i18n资源文件,request.getLocale()获取访问用户所在的国家地区
ResourceBundle myResourcesBundle = ResourceBundle.getBundle("me.gacl.i18n.resource.myproperties",request.getLocale());
--%>
<body>
<%--
<form action="" method="post">
<%=myResourcesBundle.getString("username")%>:<input type="text" name="username"/><br/>
<%=myResourcesBundle.getString("password")%>:<input type="password" name="password"/><br/>
<input type="submit" value="<%=myResourcesBundle.getString("submit")%>">
</form>
--%>
<fmt:setBundle var="bundle" basename="me.gacl.i18n.resource.myproperties" scope="page"/>
<form action="">
<fmt:message key="username" bundle="${bundle}"/><input type="text" name="username"><br/>
<fmt:message key="password" bundle="${bundle}"/><input type="password" name="password"><br/>
<input type="submit" value="<fmt:message key="submit" bundle="${bundle}"/>">
</form>
</body>
</html>