效果:
文件清单:
序号 |
文件名 |
1 |
ChangeLocaleAction.java |
2 |
ChangeLocaleForm.java |
3 |
struts-config.xml |
4 |
index.jsp |
5 |
application_en_US.properties |
6 |
application_zh_CN.properties |
7 |
application_zh_TW.properties |
清单1:
package com.coderdream.struts.action;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
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 com.coderdream.struts.form.ChangeLocaleForm;
public class ChangeLocaleAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ChangeLocaleForm clForm = (ChangeLocaleForm)form;
String language = clForm.getLanguage();
if (language != null) {
Locale locale;
if (language.equalsIgnoreCase("en")) {
locale = Locale.US;
request.getSession().setAttribute(
Globals.LOCALE_KEY, locale);
} else if (language.equalsIgnoreCase("tw")) {
locale = Locale.TAIWAN;
request.getSession().setAttribute(
Globals.LOCALE_KEY, locale);
} else {
locale = Locale.CHINA;
request.getSession().setAttribute(
Globals.LOCALE_KEY, locale);
}
}
return mapping.findForward("success");
}
}
清单2:
package com.coderdream.struts.form;
import org.apache.struts.action.ActionForm;
/**
*
* description:
*
* @author
*
*/
public class ChangeLocaleForm extends ActionForm{
public ChangeLocaleForm() {
super();
}
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
清单3:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="ChangeLocaleForm"
type="com.coderdream.struts.form.ChangeLocaleForm" />
</form-beans>
<action-mappings>
<action name="ChangeLocaleForm" path="/ChangeLocale"
scope="request"
type="com.coderdream.struts.action.ChangeLocaleAction"
validate="false">
<forward name="success" path="/index.jsp" />
</action>
</action-mappings>
<message-resources key="application"
parameter="com.coderdream.struts.resources.application" />
</struts-config>
清单4:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="org.apache.struts.Globals"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html>
<head>
<title>多语言测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<script language="javascript">
function onLanguage(){
var language = document.getElementsByName("language")[0].value;
if(language!=null&&language!=""){
document.forms[0].submit();
}
}
</script>
<body>
<html:form action="ChangeLocale.do">
<html:select property="language" styleId="height:18"
onchange="onLanguage();">
<option value="" title="">
--
<bean:message bundle="application" key="global.select.language" />
--
</option>
<option value="en" title="English">
English
</option>
<option value="cn" title="简体中文">
简体中文
</option>
<option value="tw" title="繁體中文">
繁體中文
</option>
</html:select>
</html:form>
<br>
<br>
<br>
<bean:message bundle="application" key="global.select.test" />
<%=request.getSession().getAttribute(Globals.LOCALE_KEY)%>
</body>
</html:html>
清单5:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
global.select.language = Language
global.select.test = Test
清单6:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
#BizException
global.select.language = \u8BED\u8A00
global.select.test = \u6D4B\u8BD5
清单7:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
global.select.language = \u8A9E\u8A00
global.select.test = \u6E2C\u8A66
此功能的关键是在Action里面将页面传过来的language信息得到,然后根据信息设置Locale,然后将新的Locale放到Session中。
源代码:
点击下载
posted on 2008-05-14 18:20
CoderDream 阅读(1007)
评论(0) 编辑 收藏 所属分类:
经验点滴