实现原理
在Struts中,session中存放当前会话的Locale的key值为:Globals.LOCALE_KEY,更改session中的这个属性就可以同它关联的标签等实现国际化。
<bean:message>标签根据存储在session范围内的Locale实例,从默认的Resource Bundle中检索和Locale对应的资源文件,再从资源文件中读取key对应的消息字符串。同样的还有<html:errors />等,同时Struts的验证框架也是。Action类中访问资源文件,实现国际化很也简单,因为在Action基类中定义了getResources(requset)方法,代表当前应用模块使用的默认Resource Bundle,它可以返回默认的MessaageResources对象,得到了MessageResources对象后,就可以通过它的方法来访问消息文本。
例:MessageResources message = getResources(request);
String msg=messages.getMessage(local, "hell.message")
org.apacje.struts.util.MessageResources的getMessage()方法有好几种重载形式,下面列出常用的几种:
- 根据参数指定的Locale检索对应的资源文件,然后返回和参数key对应的消息文本:
getMessage(java.util.Locale locae, java.lang,String key) - 根据参数据定的Locale检索对应的资源文件,然后返回和参数key对应的消息文本,args参数用于替换复合消息文本的参数:
getMessage(java.util.Locale locae, java.lang,String key, java.lang.Object[] args) - 根据默认的Locale检索对应的资源文件,然后返回和参数key对应的消息文本:
getMessage(java.lang,String key)
下面通过一个简单的例子程序来展现在Struts1中如何实现国际化,具体的程序如下:
运行截图
刚开始进入,根据当前系统的语言环境默认选择:
切换语言提交后如下:
程序实现源码
1.视图页面index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts国际化支持</title>
</head>
<body>
<html:form action="/changeLanguage.do" method="post">
<bean:message key="sturts.i18n.choice_language" />
<html:select property="language">
<html:option value="english">English</html:option>
<html:option value="chinese">简体中文</html:option>
</html:select>
<html:submit><bean:message key="sturts.i18n.submit_button" /></html:submit>
</html:form>
<br />
<br />
<font size="14"><b><bean:message key="sturts.i18n.test_word" /></b></font>
</body>
</html>
2.Action Form:
package test.struts.i18n.form;
import org.apache.struts.action.ActionForm;
public class LanguageForm extends ActionForm {
private static final long serialVersionUID = 1L;
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
3.对应Action:
package test.struts.i18n.action;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 test.struts.i18n.form.LanguageForm;
public class ChangeLanguage extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LanguageForm lf = (LanguageForm)form;
String language = lf.getLanguage();
HttpSession session = request.getSession();
if(language!=null&&language.equalsIgnoreCase("english")) {
Locale locale = new Locale("en", "US");
session.setAttribute(Globals.LOCALE_KEY, locale);
} else {
Locale locale = new Locale("zh", "CN");
session.setAttribute(Globals.LOCALE_KEY, locale);
}
return mapping.findForward("success");
}
}
这里创建的Locale对象要根据国家编码与语言编码两个参数来创建,可以到林格斯的网站上查看对应的编码:
http://www.lingoes.net/zh/translator/langcode.htm
4.Struts配置文件:struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="languageForm" type="test.struts.i18n.form.LanguageForm" />
</form-beans>
<action-mappings>
<action path="/changeLanguage" scope="request"
type="test.struts.i18n.action.ChangeLanguage"
name="languageForm">
<forward name="success" path="/index.jsp" />
</action>
</action-mappings>
<message-resources parameter="application"/>
</struts-config>
5.资源文件
关联的英文资源文件application_en_US.properties:
sturts.i18n.choice_language=Language
sturts.i18n.submit_button=Choice
sturts.i18n.test_word=Hello\!\!
对应的中文资源文件application_zh_CN.properties
sturts.i18n.choice_language=语言
sturts.i18n.submit_button=确定
sturts.i18n.test_word=你好!!
还要建立一个默认的资源文件application.properties同上面的中文文件,由于properties文件不支持中文,注意转码。