实现资源国际化

实现网站资源国际化,中英双语切换。

采用<fmt>标签,js页面通过ajax异步传递到后台处理,实现中英文切换。

1,action

package com.language.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 语言切换
* @author Junwei.Zeng
* @date 2016-8-25
*/
public class LanguageModAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private String language;
public void languageMod(){
System.out.println("当前语言:"+language);

ServletActionContext.getRequest().getSession().setAttribute("language", language);
}
//set && get
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}

 

2,js页面

<span id="Chinese">中文</span>
<span id="English">English</span><br>

<script type="text/javascript">
$("#Chinese").click(function(){
var url = window.location.href;
var p = "zh_CN";
$.ajax({

url : "languageModAction",
type : "GET",
data : {
"language" : p,
},
dataType : "text",
success : function(data) {
window.location=url;
}
});
});


$("#English").click(function(){
var url = window.location.href;
var p = "en_US";
$.ajax({
url : "languageModAction",
type : "GET",
data : {
"language" : p,
},
dataType : "text",
success : function(data) {
window.location=url;
}
});
});
</script>

 

<fmt:setLocale value="${language}" />
<fmt:bundle basename="globalMessages">
<fmt:message key="test" /><br>
<fmt:message key="username" /><br>
<fmt:message key="password" /><br>
<fmt:message key="cityLine" /><br>
<fmt:message key="WeChat" /><br>
<fmt:message key="FashionStreet" /><br>
</fmt:bundle>

 

posted on 2016-08-25 10:03  小俊不了情  阅读(217)  评论(0编辑  收藏  举报