Java多国语言问题解决方案
今天给大家分享的是由图老师小编精心为您推荐的Java多国语言问题解决方案,喜欢的朋友可以分享一下,也算是给小编一份支持,大家都不容易啊!
Java语言是一种面向对象的语言,所以可以生成一个国际化的软件包,使由系统自动根据区域及语言设置的属性进行判定成为可能。
解决方案主要有两种:
一是利用Java本身提供用于解决国际化问题的软件包当中的ResourceBundle及其子类,处理在Java类中或是property文件中定义的参数;二是自定义一个类进行存取,把相应参数放在数据库或是属性文件中。
一、 利用Java类包:
1、根据系统默认的语言设置读取参数值的类Resource:
package i18n.article;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
/**
* <p>Title:多国语言的后台实现类 </p>
* <p>Description: 根据属性文件及区域与语言设置,得到对应的语言</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Timer Corporation</p>
* @author:宋兆辉
* @version 1.0
*/
public class Resource {
private static final String HEAD_FILE = "ATTR"; //属性文件名的前缀
private static final String LAST_FILE=".properties"; //属性文件名的后缀
private static final String FILE_PATH="c://"; //定义属性文件存放的目录
public Resource() {
}
/*以下是根据传入的属性文件中的"键",而得到与区域与语言设置相对应的"值"*/
public synchronized String srcStr(String disStr) {
String ret = "";
try {
Locale locale = Locale.getDefault(); //获取系统的区域与语言默认设置
System.out.println("");
String baseName = new StringBuffer()
.append(HEAD_FILE).append("_").append(locale.toString())
.append(LAST_FILE).toString(); //根据local属性,前缀以及后缀生成文件名
String fileName = new StringBuffer(FILE_PATH).append(baseName)
.toString(); //获取文件的完整路径
InputStream is = new FileInputStream(fileName); //生成文件输入流
PropertyResourceBundle pr = new PropertyResourceBundle(is); //根据输入流构造PropertyResourceBundle的实例
ret = pr.getString(disStr);
if(locale.equals(Locale.CHINA)){
ret = new String(ret.getBytes("ISO-8859-1"),"GB2312");
} //假如是要显示中文,则要进行内码的转换
is.close();
return ret;
}
catch (Exception e) {
e.printStackTrace();
return disStr;
}
}
}
2、对应的properties文件:ATTR.properties和ATTR_zh_CN.properties
(1)ATTR_en_US.properties:
LoginName=login name
PassWord=password
Login=login
LoginTime=loginTime
LogOff=Logout
ChineseVersion=English Version
MainPage=home
SoftWare=softWare
Hardware=Hardware
YourLocationMainPage=YourLocationMainPage
MyCalendar=MyCalendar
MyMails=MyMails
R&D=R&D
Forum=Forum
DeveloperProcess=DeveloperProcess
TechnologyCommunion=TechnologyCommunion
(2)ATTR_zh_CN.properties:
LoginName=用户
Password=密码
Login=登陆
LoginTime=登陆时间
LogOff=注销登陆
ChineseVersion=中文版
MainPage=首页
SoftWare=软件
Hardware=硬件
YourLocationMainPage=当前位置:首页
MyCalendar=我的日程
MyMails=我的邮件
R&D=研发区
Forum=讨论区
DeveloperProcess=开发进程
TechnologyCommunion=技术交流
3、对应的jsp文件:
本例中有3个jsp页面:
(1)login.jsp:(登陆页面)
<%@ page contentType="text/Html; charset=GB2312" %>
<HTML>
<HEAD>
<TITLE></TITLE>
<link href="Css/style.css" rel="stylesheet" type="text/css">
<script language = javascript>
function focusIt() {
window.document.login.loginName.focus();
}
</script>
</HEAD>
<jsp:useBean id="resource" scope="application" class="i18n.article.Resource"/>
<BODY onload="javascript:focusIt();" bgcolor="#ffffff">
<form name="login" method="post" action="main.jsp">
<table border=0 cellpadding=0 cellspacing=0 align=center>
<tr>
<td height="150"></td></tr>
<tr>
<td width=262><img alt="" border=0 src="http://www.QQread.com/java/images/cover_r1_c1.jpg"></td>
<td> </td>
<td>
<table border=0 cellpadding=1 cellspacing=1>
<tr>
<td>
<p align=right><%=resource.srcStr("LoginName")%></p></td>
<td><input type=text name="loginName" ID=Text1></td></tr>
<tr>
<td>
<p align=right><%=resource.srcStr("Password")%></p></td>
<td><input type=password name="password" ID=Password1></td></tr>
<tr>
<td> </td>
<td align=center>
<input class="button" type=submit value=" <%=resource.srcStr("Login")%> " ID=Submit1></td></tr>
</table>
</td></tr>
<tr>
<td colspan=3><img alt="" border=0
src="http://www.qqread.com/java/images/cover_r2_c1.jpg"></td></tr>
<tr>
<td colspan=3><img alt="" border=0
src="http://www.qqread.com/java/images/cover_r3_c1.jpg"></td></tr>
<tr>
<td colspan=3 height=20> </td>
</tr>
<tr>
<td colspan=3 align=center><font color="#c0c0c0">Best Viewed In IE5.0 Or Up ,1024x768</font></td>
</tr>
<tr>
<td colspan=3><hr size=1 width="100%" color="#c0c0c0"></td>
</tr>
<tr>
<td colspan=3 align=center>© 2003 Corporation. All rights reserved.</td>
</tr>
</table>
</form>
</BODY>
</HTML>
(2)main.jsp:
<%@ page contentType="text/html; charset=GB2312" %>
<jsp:useBean id="resource" scope="application" class="i18n.article.Resource"/>
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
<LINK rel="stylesheet" href="css/style.css" type="text/css">
</HEAD>
<BODY topMargin=2 leftMargin=2 bgcolor="#ffffff">
<TABLE WIDTH="100%" HEIGHT="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0">
<tr>
<td height=80>
<%
String loginName = request.getParameter("loginName");
%>
<table width=100% height=100% cellpadding=0 cellspacing=0 border=0>
<tr>
<td width=163 rowspan=3><img src="http://www.qqread.com/java/images/system_Logo.jpg" border=0></td>
<td width=30 rowspan=3><img src="http://www.qqread.com/java/images/Logo_linked_corner.gif" border=0></td>
<td width=140><img src="http://www.qqread.com/java/images/Logo_extended_bar.jpg" border=0></td>
<td height=25 bgcolor="#95ADD3" align=right class="P1">
<%=resource.srcStr("LoginTime")%><%=new java.util.Date().toLocaleString()%>
<%=resource.srcStr("LoginName")%><%=loginName%>
<img src="http://www.qqread.com/java/images/Logout.gif" align="absMiddle" border=0>
<a href="login.jsp"><%=resource.srcStr("LogOff")%></a>
<img src="http://www.qqread.com/java/images/row_ender.jpg" align="absMiddle" border=0></td>
</tr>
<tr><td height=30 colspan=2><p>
<a href="#"><%=resource.srcStr("MainPage")%></a>
<a href="#"><%=resource.srcStr("SoftWare")%></a>
<a href="#"><%=resource.srcStr("Hardware")%></a> </p></td></tr>
<tr><td height=25 colspan=2<%=resource.srcStr("YourLocationMainPage")%>></td></tr>
</table>
</td>
</tr>
<tr>
<td>
<table width=100% height=100% cellpadding=0 cellspacing=0 border=0>
<tr>
<td width=165 valign=top>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr><td colspan=2><img src="http://www.qqread.com/java/images/left_menu_top.jpg" border=0></td></tr>
<tr><td width=35 align=left><img src="http://www.qqread.com/java/images/left_menu_colbar.jpg" border=0></td>
<td width=130 align=left valign=top>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("MyCalendar")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("MyMails")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("DeveloperProcess")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("TechnologyCommunion")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("Forum")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
<tr><td style="cursor:hand;"><p><%=resource.srcStr("R&D")%></p></td></tr>
<tr><td><hr size=1 width=100% color="#C6FF00"></td></tr>
</table>
</td>
</tr>
</table>
</td>
<td><IFRAME frameBorder=0 id=main name=main scrolling=auto src="version.jsp"
style="HEIGHT: 100%; VISIBILITY: inherit; WIDTH: 100%; Z-INDEX: 1"></IFRAME></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height=50 valign=top>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr><td align=center><hr size=1 width=80% color="#95ADD3"></td></tr>
<tr><td align=center>?2003 Corporation. All rights reserved.</td></tr>
</table>
</td>
</tr>
</TABLE>
</BODY>
</HTML>
(3)version.jsp:(主页面的中间部分)
<%@ page contentType="text/html; charset=GBK" %>
<jsp:useBean id="resource" scope="application" class="i18n.article.Resource"/>
<HTML>
<HEAD>
<TITLE></TITLE>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
</HEAD>
<BODY leftMargin="0" topMargin="0">
<table width="100%" height="100%" cellpadding=0 cellspacing=0 border=0>
<tr>
<td align=center valign=middle colspan=2>
<table align="center" border="0" cellspacing="3" cellpadding="5" style="filter:dropshadow(color=gray,offx=5,offy=5,positive=1);">
<tr>
<td width=400 height=100 bgcolor="#CBD3EA" align="center" style="border: 1px #0A246A solid;">
<p><font color="#5c6bac" style="font-size:30px;"><b><%=resource.srcStr("ChineseVersion")%></b></font></p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width=50% valign=top align=center> </td>
<td width=50% height=155 align=center><img src="http://www.qqread.com/java/images/internationalization.jpg"></td>
</tr>
</table>
</BODY>
</HTML>
二、 利用自定义类:
利用自定义的类,可以存取放在属性文件或是数据库中的对应的语言文字,实现方式大致与上述方法相同,不过是添加一些对属性文件流或是数据库存取的方法,对应的jsp页面可以不做改动。在这里不再赘述.
注:以上程序在windows2000英文版及中文版,resin2.1.6上调式通过.