Struts实现多国语言切换
来源:互联网 发布:16易建联隐藏数据 编辑:程序博客网 时间:2024/04/27 16:47
转载自:http://blog.csdn.net/lw371496536/article/details/6953908
通过下拉框里各个语言(中文,日本语,English)的选择,切换jsp页面文字。
tyrone1979 发表于 2005-08-26 13:27:19
作者:tyrone1979 来源:blog.csdn.net/tyrone1979
作者:tyrone1979 来源:blog.csdn.net/tyrone1979
1 准备资源文件。
资源文件命名格式:filename_language_country.properties.
中文文件名为index_zh_CN.properties。
日文文件名为 index_ja_JP.properties。
英文文件名为 index_en.properties。
英文文件内容:
中文文件内容:
日文文件内容:
/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
3 jsp
<bean:message key="index.jsp.welcome"/>引用资源文件的index.jsp.welcome属性
SelectLanguageAction.do调用Action实现语言转换。
4 Action
结果
1 IE默认语言为中文:
启动画面
切换为日语
切换为英文
资源文件命名格式: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里配置资源文件
<message-resources parameter="resources.config.index" />
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>
<bean:message key="index.jsp.welcome"/>引用资源文件的index.jsp.welcome属性
SelectLanguageAction.do调用Action实现语言转换。
4 Action
package com.nova.colimas.web.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessages;//import org.apache.struts.upload.FormFile;import com.nova.colimas.web.form.SelectLanguageForm;import org.apache.struts.Globals;import java.util.Locale;public class SelectLanguageAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ SelectLanguageForm myform=(SelectLanguageForm)form; String lan=myform.getLanguage(); switch((new Integer(lan)).intValue()){ case 0 : request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA); break; case 1: request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.JAPAN); break; case 2: request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH); break; default: request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH); break; } return mapping.findForward("index"); }}Form/** Created on 2005/06/18** TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/package com.nova.colimas.web.form;import org.apache.struts.action.ActionForm;/*** @author tyrone**/public class SelectLanguageForm extends ActionForm { private String language; public void Reset() { this.language=""; return; } /** * @return Returns the Language. */ public String getLanguage() { return language; } /** * @param language The Language to set. */ public void setLanguage(String property1) { this.language = property1; }}
结果
1 IE默认语言为中文:
启动画面
切换为日语
切换为英文
这种方式可以根据客户端情况自动将语言转换为客户浏览器语言版本,但如果客户要根据自己事情需求将语言转换成自己所需语言版本就显得不够灵活了
建议
自己定义Resource文件,根据客户指定的语言来选择不同的资源文件,并显示出来
ResourceBundle bundle = (ResourceBundle)pageContext.getAttribute(name);
0 0
- Struts实现多国语言切换
- Struts实现多国语言切换
- Struts实现多国语言切换
- Struts实现多国语言切换
- Qt实现多国语言切换
- APP,实现多国语言动态切换
- Qt实现多国语言动态切换
- struts多国语言支持
- 可切换多国语言
- 程序实现多国语言的动态切换解决方案
- 程序实现多国语言的动态切换解决方案
- c++实现动态多国语言的动态切换
- QT实现多国语言的动态切换
- ecshop2.7.2中调用语言切换,实现多国语言
- C#实现多国语言的界面切换
- 程序实现多国语言的动态切换解决方案
- QT实现多国语言的动态切换
- 程序实现多国语言的动态切换解决方案(转)
- 培训-从学员的角度思考问题
- View之Button
- 关于LayoutInflater的一些梗
- 自定义ANDROID中EDITTEXT中的HINT文本的大小
- RESTful API 设计指南
- Struts实现多国语言切换
- 状态栏的使用详解
- How to add or list certificates from keystore or trustStore in Java - Keytool Example Read more: ht
- Android属性动画
- 【 bzoj 2127 】happiness - 最小割
- draw9patch详解
- 教你写Android ImageLoader框架
- linux下MTD驱动
- memcache的配置及使用