JSP中国际化问题

[b] 1.HTML中的字符实体[/b]
HTML 中的字符实体和XML的语言保持一致,它定义了用特定的字符序列显示单字符的功能,这种字符序列成为字符实体,它以"&"开头,以";"结束.

[b] 2.Unicode[/b]
Unicode字符标准是一个字符编码系统,它支持互相转换、处理和显示现在世界上用不同语言写的文本。Unicode由Unicode协会创建。
在Java语言中,字符、字符串和标识符在内部使用16位的Unicode2.0字符集表示。Unicode使用"\uxxxx"表示一个字符,前256个Unicode字符和iso-8859-1标准的256个字符一致。

[b] 3.使用配置文件实现国际化[/b]
下面看看property文件开发一个能显示多国语言的JSP页面。总共有一个JSP页面和4个properties配置文件。

<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.*"%>
<%
String languageDefault = Locale.getDefault().getLanguage();
String countryDefault = Locale.getDefault().getCountry();

Local defaultLocale = null;
String charset = null;

if( languageDefault = "en" || languageDefault().equals("en")){
defaultLocale = Locale.ENGLISH;
charset = "iso-8859-1";

}else if( countryDefault == "CN" || countryDefault.equals("CN")){
defaultLocale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";

}else if( countryDefault == "TW" || countryDefault.equals("TW")){
defaultLocale = Locale.TRADITIONAL_CHINESE;
charset = " big5";
}else{

System.out.println( "Unknown Language" );
}
if(session.isNew()){
session.setAttribute( "locale" , defaultLocale);
session.setAttribute( "charset" , charset);

}else{
String language = request.getParameter( "language" );
if( language == null || language.equals(null) ){
session.setAttribute( "locale", defaultLocale );
session.setAttribute( "charset", charset);

}else{
Locale locale = null;
if(language.equals( "Locale.SIMPLIFIED_CHINESE" )||
language.equals( "Locale.SIMPLIFIED_CHINESE")){

locale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";
}else if(language.equals( "Locale.TRADITIONAL_CHINESE" )|| language.equals( "Locale.TRADITIONAL_CHINESE" ) ){
locale = Locale.TRADITIONAL_CHINESE;
charset = "big5";
}else if(language.equals( "Locale.ENGISH" )||
language.equals( "Locale.ENGLISH ")){
locale = Locale.ENGLISH;
charset = "iso-8859-1";
}
session.setAttribute( "locale", locale );
session.setAttribute( "charset", charset );
}
}
Locale locale = (Locale)session.getAttribute( "locale" );
ResourceBundle messagesBundle = ResourceBundle.getBundle( "uni_property", locale );
String localeCountry = locale.getCountry();
String localLanguage = locale.getLanguage();
System.out.println( "locale:" +locale.toString() );
System.out.println( "localeCountry:" +localeCountry );
System.out.println( "localeLanguage:" +localeLanguage );

if(localeLanguage == "en"){
response.setContentType( "text/html;charset = ISO8895-1" );
}else if(localeLanguage == "CN"){
response.setContentType( "text/html;charset = gb2312");
}else if(localeLanguage == "TW"){
response.setContentType( "text/html;charset = big5" )
}
%>
<html>
<head>
<meta name = "GENERATOR" content="Micrsoft FromPage4.0">
<meta name = "ProgId" content="FrontPage.Editor.Document">
<title>Internationzational by propery file</title>
<base target = "contents"><>
</head>
<body topmargin = "0" leftmargin = "0">
<table border= "0" width= "796" style= "font-size:10pt" cellspacing= "0" cellpadding= "0" height= "60">
<tr rowspan=5>
<td colspan=3></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td width="196" height="68"></td>
<td width="470" height="64">
<p align="center">
<a href="#" onclick="href='uni_property.jsp?language=Locale.SIMPLIFIED_CHINESE'">
<%=messagesBundle.getString("simplifiedChinese")%></a>|
<a href="#" onclick="href='uni_property.jsp?language=Locale.TRADITIONAL_CHINESE">
<%=messagesBundle.getString("traditionalChinese")%></a>|
<a href="#" onclick="href='uni_property.jsp?language=Locale.ENGLISH">
<%=messagesBundle.getString("english")%></a></td>
<td width="130" height="56">
<p align="center">
<%=messagesBundle.getString("username")%>
</td>
</tr>
</body>
</html>

此外在Web-inf目录下配置几个properties文件
默认配置文件(uni_property.properties)
username=JSPDeveloper
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English

默认简体中文配置文件(uni_property_zh_CN.properties)
username=JSP\u5f00\u53d1\u8005
simplifiedChinese=\u7b80\u4f53\u4e2d\u6587
traditionalChinese=\u7e4\u4f53\u4e2d\u6587
english=\u82f1\u6587

默认繁体中文配置文件(uni_property_zh_TW.properties)
username=JSP\u5f00\u53d1\u8005
simplifiedChinese=\u7b80\u4f53\u4e2d\u6587
traditionalChinese=\u7e4\u4f53\u4e2d\u6587
english=\u82f1\u6587

默认英文配置文件(uni_property_en.properties)
username=JSP Developer
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值