一. 创建三个资源文件:
crms_zh_CN.properties
crms_en_US.properties
crms_zh_TW.properties
二. struts2的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 拦截struts的扩展名 -->
<constant name="struts.action.extension" value="action,"/>
<!-- 开发模式 -->
<constant name="struts.devModel" value="true"/>
<!-- 国际化 -->
<constant name="struts.custom.i18n.resources" value="crms"/>
<package name="web" namespace="/web" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result>/login.jsp</result>
</action>
<action name="login" class="userinfoAction">
<result name="success">
/WEB-INF/jsp/index.jsp
</result>
<result name="error">
/login.jsp
</result>
</action>
<!--国际化-->
<action name="i18n" class="userinfoAction">
<result name="success">
/WEB-INF/jsp/header.jsp
</result>
</action>
</package>
</struts>
三.jsp文件[语言切换的超链接]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>header</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">
<!-- JQuery-->
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var lang=$("#currLanguage").val();
if (lang=="en_US") {
$("#en_US").hide();
$("#title").removeClass("title_cn");
$("#title").addClass("title_en");
}else if(lang=="zh_TW"){
$("#zh_TW").hide();
}else if(lang=="zh_CN"){
$("#zh_CN").hide();
}
//单击选择语言时,隐藏先择的语言的超链接/刷新相应的页面
$("#language div").click(function(){
//DIV的单再比<a/> 超链接优先执行,用此语句来掉换一下优先的顺序
window.location.href="web/i18n!i18n?request_locale="+this.id;
//单击时,先显示隐藏的DIV
$('div:hidden').show();
//再把单击的DIV隐藏掉
$(this).hide();
//刷新页面
window.parent.header.location.reload();
window.parent.left.location.reload();
window.parent.body.location.reload();
window.parent.footer.location.reload();
//根据语言来改变样式
if (lang=="en_US") {
$("#en_US").hide();
}else if(lang=="zh_TW"){
$("#zh_TW").hide();
}else if(lang=="zh_CN"){
$("#zh_CN").hide();
}
});
});
</script>
</head>
<body >
<div id="title" class="title_cn"><s:property value="getText('web_header_title')"/></div>
<div id="loginInfo">
<div id="language">
<div id="changeLanguage"><s:property value="getText('web_header_language')"/>:</div>
<div id="en_US" ><a href="web/i18n!i18n?request_locale=en_US"><s:property value="getText('web_header_en')"/></a></div>
<div id="zh_TW" ><a href="web/i18n!i18n?request_locale=zh_TW"><s:property value="getText('web_header_gbk')"/></a></div>
<div id="zh_CN" ><a href="web/i18n!i18n?request_locale=zh_CN"><s:property value="getText('web_header_gb2312')"/></a></div>
<input type="hidden" id="currLanguage" value='<s:property value="locale"/>'/>
</div>
</div>
<!--以下是测试代码,,以下两种标签均可以显示国际化内容-->
<div id="title" class="title_cn"><s:property value="getText('web_header_title')"/></div>
<div id="title_1"><s:text name="web_left_service"/> </div>
</body>
</html>
四.显示效果
在页面上只显示两种语言的超链接和当前使用的一种语言,当用户单击来切换语言时,页的将显示为该语言,并隐藏该语的超链接.