欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2国际化

程序员文章站 2022-05-24 23:47:04
...
一,国际化的流程:1,获得Locale对象 2,使用getBundle()方法 3,查找匹配资源子类或资源文件

命名规范: basename_language_country_variant 其中basename是资源文件的基本名,language必须是java支持的语言。country必须是java支持的域。variant表示特定的开发商或者浏览器 例如 win 代表windows。一般不使用该项。
public class LocalList {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Locale[] list =Locale.getAvailableLocales();
for(int i = 0; i < list.length; i++){
System.out.println(list[i].getDisplayCountry()+ "-->" + list[i].getCountry());
System.out.println(list[i].getDisplayLanguage()+ "-->" + list[i].getLanguage());
}

Locale locale=Locale.getDefault();
ResourceBundle resourcesbundle=ResourceBundle.getBundle("MessagesResources", locale);
System.out.println(resourcesbundle.getString("userName"));
System.out.println(resourcesbundle.getString("userPassword"));
String userName=resourcesbundle.getString("userName");
System.out.println(MessageFormat.format(userName,"HAHAHA" ,new Date()));
}

}

可以查看支持的语言和区域有哪些。
二,资源文件的配置,大概有3种方式
1,使用struts.xml
<constant name="struts.custom.i18n.resources" value="MessagesResources" />
其中MessagesResources为basename。
2,使用struts.properties Key-Value
struts.custom.i18n.resources=MessagesResources
3,使用web.xml
<init-param>
<param-name>struts.custom.i18n.resources</param-name>
<param-value>MessagesResources</param-value>
</init-param>

三,例子
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp2.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>

<h4><s:text name="Login"/></h4>
<s:form action="ListAction!abc.action">
<s:textfield name="userName" key="userName"/>
<s:textfield name="userPassword" key="userPassword"/>
<s:submit key="loginButton"/>
</s:form>
<s:url id="url" action="ListAction">
<s:param name="request_locale">zh_CN</s:param>
</s:url>
<s:a href="%{url}"><s:text name="Chinese"/></s:a>

<s:url id="url" action="ListAction">
<s:param name="request_locale">en_US</s:param>
</s:url>
<s:a href="%{url}"><s:text name="English"/></s:a>
</body>
</html>


action
package com.pageDemo.Action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.pageDemo.bean.bean;
import com.pageDemo.dao.SelectAll;
import com.pageDemo.model.Pager;
import com.pageDemo.model.pageModel;

public class ListAction extends ActionSupport {

/*private pageModel pm;
private Pager pager;

public void setPager(Pager pager) {
this.pager = pager;
}

public Pager getPager() {
return pager;
}

public pageModel getPm() {
return pm;
}

public void setPm(pageModel pm) {
this.pm = pm;
}*/
private String userName;
private String userPassword;


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getUserPassword() {
return userPassword;
}


public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}


public String execute() throws Exception
{
return SUCCESS;
}

public String abc() throws Exception
{


return "ok";
}
}

配置文件
<?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>

<constant name="struts.custom.i18n.resources" value="MessagesResources" />

<package name="struts2" extends="struts-default">

<action name="ListAction" class="com.pageDemo.Action.ListAction">
<result >/MyJsp2.jsp</result>
<result name="ok">/MyJsp3.jsp</result>
</action>
</package>


</struts>

两个资源问价
userName=\u59D3\u540D
userPassword=\u5BC6\u7801
loginButton=\u767B\u9646
Login=\u7528\u6237\u767B\u5F55
Chinese=\u4E2D\u6587
English=\u82F1\u6587

userName=Name
userPassword=Password
loginButton=Login
Login=User Login
Chinese=Chinese
English=English

其中两个资源文件都在src目录下。文件名为MessagesResources_zh_CN.properties和MessagesResources_en_US.properties