java国际化

Tip1:国际化开发概念
*软件的国际化:软件开发时,要使它能够应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或者数据。
*国际化又称为i18n:internationalization

 

Tip2:合格的国际化软件
*对于程序中固定使用的文本,例如菜单栏、导航条等中使用的文本元素或错误提示信息,状态信息等,需要根据来访者的地区和国家,选择不同语言的文本为之服务。
*对于程序动态产生的数据,例如(日期,货币等),软件应能根据当前所在的国家或者地区文化习惯进行显示。

 

Tip3:固定文本元素的国际化
*对于软件中菜单栏、导航条、错误信息、状态信息等这些固定不变的文本信息,可以把它们写在一个properties文件中,并根据不同的国家编写不同的properties文件。这一组properties文件称之为一个资源包。

*在javaAPI中提供了一个ResourceBundle类用于描述一个资源包,并且ResourceBundle类提供了相应的方法getBundle,这个方法可以根据来访者的国家地区自动获取与之对应的资源文件予以显示。


Tip4:创建资源包和资源文件
*一个资源包中的每个资源文件都必须拥有共同的基名。除了基名,每个资源文件的名称中还必须有标识本地信息的附件部分,本地信息的附件部分可以IE浏览器查。例如:一个资源包的基名是"myproperties",则与中文、英文环境相对应的资源文件名则为:
"myproperties_zh.properties" "myproperties_en.properties"

*每个资源包都应有一个默认资源文件,这个文件不带标识本地信息的附件部分。若ResourceBundle对象在资源包中找不到与用户匹配的资源文件,它将选择该资源包中与用户最相近的资源文件,如果再找不到,则使用默认资源文件。例如:mypropertiess.properties

 

Tip5:资源文件的书写格式
*资源文件的内容通常采用“关键字-值”的形式,软件根据关键字检索值显示在页面上。一个资源包中的所有资源文件的关键字必须相同,值则为相应国家的文字。
*并且资源文件中采用的是properties格式文件,所以文件中的所有字符都必须是ASCII字码,对于像中文这样的非ASCII字符,需先进行编码。(java提供了一个native2ascii命令用于编码)。例:属性文件是不能保存中文的

 

Tip6:编程实现固定文本的国际化
*ResourceBundle类提供了一个静态方法getBundle,该方法用于装载资源文件,并创建ResourceBundle实例:
locale currentLocale = Locale.getDefault();
ResourceBundle myResources = ResourceBundle.getBundle(basename,currentLocale);
--basename为资源包基名(且必须为完整路径)
--如果与该locale对象匹配的资源包子类找不到。一般情况下,则选用默认资源文件予以显示。
*加载资源文件后,程序就可以调用ResourceBundle实例对象的getString方法获取指定的资源信息名称所对应的值。
String value = myResources.getString(key);

 

国际化实例

实例1:
建立资源包,properties文件不能保存非ASCII值。java提供native2ascii工具将非ascii转ascii字符。

MessageResource_zh.properties
username=\u7528\u6237
password=\u5bc6\u7801

MessageResource_en.properties
username=username
password=password

MessageResource.properties
username=username
password=password


public class Demo1{

public static void mian(String[] args){
ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.resource.MessageResource",Locale.CHINESE); //ResourceBundle.getBundle("cn.itcast.resource.MessageResource",Locale.CANADA);
String username = bundle.getString("username");
String password = bundle.getString("password");
System.out.println(username);
System.out.println(password);
}

}


实例2:浏览器切换国际化-登录页面实例(login.jsp)
<html>
<body>
<%
ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.resource.MessageResource",reguest.getLocale());
%>

<form>
<%=bundle.getString("username")%><input type="text" name="username"><br/>
<%=bundle.getString("password")%><input type="text" name="password"><br/>
</form>
</html>
</body>

 


实例3:按钮切换国际化-登录页面实例(login.jsp)
<html>
<body>

<a href="/day12/login.jsp?language=zh">中文(网页)</a>
<a href="/day12/login.jsp?language=zh">english(pages)</a>

<%
String language = reguest.getParamter("language");
Locale locale= new Locale(language); //locale对象代表语言、国家、地区

ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.resource.MessageResource",locale);
%>

<form>
<%=bundle.getString("username")%><input type="text" name="username"><br/>
<%=bundle.getString("password")%><input type="text" name="password"><br/>
</form>
</html>
</body>

 


Tip7:动态数据的国际化
数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java中提供了解决这些问题的API类(位于Java.util包和java.text包中)

Locale类
*Locale实例对象代表一个特定的地理,政治、文化区域。
*一个Locale对象本身不会验证它代表的语言和国家地区信息是否正确,只是向本地敏感的类提供国家地区信息,与国际化相关的格式和解析任务由本地敏感的类去完成。(若JDK中的某个类在运行时需要根据Locale对象来调整其功能,这个类就称为本地敏感类 )

 

动态国际化三个类

Tip8:DateFormat(国际化日期)

Tip9:NumberFormat(国际化数字)

Tip10:MessageFormat(动态文本)

 


Tip10:MessageFormat(动态文本)
*如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。

*例如:
At 12:30 on jul 3,1990 a hurricance destoryed 99 houses and cased $1000000 of damage
以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关数据进行批量处理。

*MessageFormat类如何进行批量处理呢?
MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)
MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。


格式化模式字符串
on {0} a hurricance destoryed {1} houses and cased {2} of damage
*MessageFormat类
MessageFormat(String pattern)
实例化MessageFormat对象,并装载相应的模式字符串。
format(Object obj[]);
*格式化输出模式字符串,参数数组中指定占位符相应的替换对象。
format(new Object[]{date,new Integer(99),new Double(1e7)});


实例:
public class Demo5{

public static void mian(String[] args){
String pattern = "on {0} a hurricance destoryed {1} houses and cased {2} of damage";
MessageFormat format = new MessageFormat(pattern,Locale.CHINA);
Object[] params = {new Date(),99,1000000};
String message = format.format(params);
System.out.println(message);
}
}

public class Demo6{

public static void mian(String[] args){
String pattern = "At {0,time,short} on {0,date} a hurricance destoryed {1} houses and cased {2,number,currentcy} of damage";
MessageFormat format = new MessageFormat(pattern,Locale.CHINA);
Object[] params = {new Date(),99,1000000};
String message = format.format(params);
System.out.println(message);
}
}

 

//资源文件中动态替换
message="在{0},一场飓风摧毁{1}房子,导致了{1}的损失"

public class Demo7{

public static void mian(String[] args){
ResourceBundle bundle = ResourceBundle.getBundle(cn.itcast.resource.MessageResource,Locale.CHINA);
String pattern = bundle.getString(message);
MessageFormat format = new MessageFormat(pattern,Locale.CHINA);
Object[] params = {new Date(),99,1000000};
String message = format.format(params);
System.out.println(message);

}
}

 

posted @ 2017-04-23 00:22  N神3  阅读(184)  评论(0编辑  收藏  举报