Struts国际化处理 摘要: 通过下拉框里各个语言(中文,日本语,English)的选择,切换jsp页面文字。
本文Matrix永久镜像:http://www./resource/article/43/43663_Struts.html 说明:本文可能由Matrix原创,也可能由Matrix的会员整理,或者由 Matrix的Crawler在全球知名Java或者其他技术相关站点抓取并永久 保留镜像,Matrix会保留所有原来的出处URL,并在显著地方作出说明, 如果你发觉出处URL有误,请联系Matrix改正.
1 准备资源文件。
资源文件命名格式:filename_language_country.properties. 中文文件名为index_zh_CN.properties。 日文文件名为 index_ja_JP.properties。 英文文件名为 index_en.properties。
英文文件内容:
index.jsp.welcome=Colimas Library Management Systemindex.jsp.name=Nameindex.jsp.userid=User IDindex.jsp.pass=Password
中文文件内容:
index.jsp.welcome=\u4f60\u597dindex.jsp.name=\u59d3\u540dindex.jsp.userid=\u7528\u6237\u540dindex.jsp.pass=\u5bc6\u7801
日文文件内容:
index.jsp.welcome=\u3044\u3089\u3063\u3057\u3083\u3044\u307e\u305bindex.jsp.name=\u59d3\u540dindex.jsp.userid=\u30e6\u30fc\u30b6\u30fcIDindex.jsp.pass=\u30d1\u30b9\u30ef\u30fc\u30c9
\uxxxx是中文被转换后的ASCII码。可以使用native2ascii.exe工具转换。
2 struts-config.xml里配置资源文件
resources.config.index是classes目录下的resources/config子目录的index__xx_xx.properties文件. struts根据你的浏览器的语言设置调用不同语言的资源文件。 例如:如果你的IE默认语言为中文则。Struts将使用index_zh_CN.properties。而在struts-config.xml里只需写出“index”即可
ActionMapping
<form-beans>
<!--1 Multi-Lanuage support formbean-->
<form-bean name="SelectLanguageForm" type="com.nova.colimas.web.form.SelectLanguageForm"/>
</form-beans>
<!-- =========================================== Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward name="index" path="/pages/index.jsp"/>
</global-forwards>
<!-- =========================================== Action Mapping Definitions -->
<action-mappings>
<!-- 1 select language action -->
<action path="/SelectLanguageAction" type="com.nova.colimas.web.action.SelectLanguageAction" name="SelectLanguageForm" scope="request">
</action> … </action-mappings>
3 jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic">
<html:html><Title><bean:message key="index.jsp.welcome"/></Title>
<body>
<logic:present name="user">
<H3>Welcome <bean:write name="LoginForm" property="userID" />!</H3>
</logic:present><logic:notPresent scope="session" name="user">
<H3>Welcome Colimas!</H3></logic:notPresent>
<html:errors />
<html:form action="/SelectLanguageAction.do">
<html:select property="language">
<html:option value="0">中文</html:option>
<html:option value="1">日本語</html:option>
<html:option value="2">English</html:option>
</html:select>
<html:submit>Select</html:submit>
</html:form>
<html:form action="/LoginAction.do">
<p>
<bean:message key="index.jsp.userid"/>
<input type="text" name="userID" value="tyrone1979" /><br>
<bean:message key="index.jsp.pass"/>
<input type="password" name="password" value="197913"/><br>
<html:submit><bean:message key="index.jsp.login"/>
</html:submit>
</p>
</html:form>
</body>
</html:html>
|