图示:
首先写三份不同国家语言的 properties 文档
message_en_GB.properties 文档
title=Welcome You Access
headline=Struts demo 1
message_zh_CN.properties 文档
title=\u6B22\u8FCE\u4F60\u7684\u767B\u9646
headline=\u8FD9\u662FStruts2\u6F14\u793A\u6848\u4F8B
message_zh_HK.properties 文档
title=\u6B61\u8FCE\u4F60\u7684\u767B\u9678
headline=\u9019\u662FStruts2\u6F14\u793A\u6848\u4F8B
SetLocalAction Action类
package action;
import java.util.Locale;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SetLocalAction extends ActionSupport {
private String language;
private String location;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String execute() throws Exception {
if(null!=language&&null!=location){
ActionContext.getContext().setLocale(new Locale(language, location));
}else{
ActionContext.getContext().setLocale(this.getLocale());
}
return SUCCESS;
}
}
配置 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"struts-2.3.dtd">
<struts>
<!--配置国际化文件 -->
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="setLocal" class="action.SetLocalAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
index.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>使用国际化</title>
</head>
<body>
<a href="setLocal.action">恢复默认</a>
<br />
<a href="setLocal.action?language=zh&location=CN">设置大陆中文</a>
<br />
<a href="setLocal.action?language=en&location=GB">设置美国英文</a>
<br />
<a href="setLocal.action?language=zh&location=HK">设置香港繁体</a>
<br />
<h4><s:text name="title" /></h4>
<p><s:text name="headline" /></p>
</body>
</html>
效果图: