Fork me on GitHub

Struts2(十三)国际化-internationalization

一、国际化是什么--I18N

 即internationalization

首字母i-结束字母n之间有18个字母

特征:在程序不做修改的情况下,可以根据不同的语言环境显示相应内容

二、Java内置国际化

 

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = welcome!

message_zh_CN.properties

demo = \u60A8\u597D\u4E16\u754C!
userName = \u7528\u6237\u540D
password = \u5BC6\u7801
welcome = \u6B22\u8FCE!

message.properties

demo = \u60A8\u597D\u4E16\u754C!
userName = \u7528\u6237\u540D
password = \u5BC6\u7801
welcome = \u6B22\u8FCE!

实现代码

<%@page import="java.util.ResourceBundle"%>
<%@page import="java.util.Locale"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>测试</title>
</head>
<body>
<h1>测试页面</h1>
<%
//获取当前已经安装的语言数组
Locale [] locals=Locale.getAvailableLocales();
//遍历数组
for(Locale local:locals){
    //国家+国家代码
    out.print("国家:    "+local.getDisplayCountry()+"     国家代码:"+local.getCountry()+"   ");
    //语言+语言代码
    out.println("语言:  "+local.getDisplayLanguage()+"   语言代码:"+local.getLanguage()+"<hr/>");
}
//引用语言            此处可以更改
Locale ls=Locale.getDefault();
//使用哪个资源文件
ResourceBundle rb=ResourceBundle.getBundle("message",ls);
//输出
out.println(rb.getString("demo"));
out.println(rb.getString("userName"));
out.println(rb.getString("password"));
out.println(rb.getString("welcome"));
%>
</body>
</html>

三、Struts2实现

 准备全局资源文件:通常至少三个文件

命名规则:前缀名_语言_国家.properties

内容格式:key=value

指定资源文件(设置常量)

1.struts.xml文件中:

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

2.struts.properties中:

struts.custom.i18n.resources=message

 

<s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" />

更改资源文件

message.properites

demo = 您好世界!
userName = 用户名
password = 密码
welcome = 欢迎!
submit =提交
reset=重置

message_zh_CN.properties

demo = 您好世界!
userName = 用户名
password = 密码
welcome = 欢迎!
submit =提交
reset=重置

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = welcome!
submit =submit
reset=reset
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>测试</title>
</head>
<body>
<s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" />
<s:form action="" method="post">
<s:textfield key="userName" />
<s:password  key="password"/>
<s:submit key="submit"/>
<s:reset key="reset" />
</s:form>
</body>
</html>

四、Action类实现国际化

message.properties

demo = 您好世界!
userName = 用户名
password = 密码
welcome = {0}欢迎!{1}
submit =提交
reset=重置
fail=登录失败
usermsg=用户名不能为空
usermsglength=用户名的长度为2-16位
pwdmsg=密码不能为空
pwdmsglength=密码的长度为2-16位

message_zh_CN.properties同上

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = {0}welcome!{1}
submit =submit
reset=reset
fail=login failed
usermsg=user name do not empty
usermsglength=user name length is 2 to 16!
pwdmsg=password do not empty
pwdmsglength=password length is 2 to 16!

struts.xml

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="message" />
    <package name="default" namespace="/" extends="struts-default">
    <action name="login" class="action.LoginAction" method="login">
    <result name="success">
    /demo.jsp
    </result>
    <result name="input">
    /demo.jsp
    </result>
    </action>
 </package>
</struts>

demo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>测试</title>
</head>
<body>
<%-- <s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" /> --%>
<s:form action="login.action" method="post">
<s:textfield name="username" key="userName" />
<s:password  name="password" key="password"/>
<s:submit key="submit"/>
<s:reset key="reset" />
</s:form>
<hr/><br/>
<s:actionmessage />
<s:debug/>
</body>
</html>

LogionAction

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String username;
    private String password;

    public String login() {
        if ("admin".equalsIgnoreCase(username)
                && "admin".equalsIgnoreCase(password)) {

            this.addActionMessage(this.getText("welcome", new String[] {
                    username,password}));
            return SUCCESS;
        }
        this.addActionMessage(this.getText("fail"));
        return INPUT;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

LogionAction.login()验证文件

LogionAction-login-validation.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("usermsg")}</message>
</field-validator>
<field-validator type="stringlength">
<param name="trim">true</param>
<param name="minLength">2</param>
<param name="maxLength">16</param>
<message>${getText("usermsglength")}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("pwdmsg")}</message>
</field-validator>
<field-validator type="stringlength">
<param name="trim">true</param>
<param name="minLength">2</param>
<param name="maxLength">16</param>
<message>${getText("pwdmsglength")}</message>
</field-validator>
</field>

</validators>

 

五、Jsp页面国际化传参数 

<!-- First Example -->
<s:i18n name="struts.action.test.i18n.Shop">
    <s:text name="main.title"/>
</s:i18n>

<!-- Second Example -->
<s:text name="main.title" />

<!-- Third Examlpe -->
<s:text name="i18n.label.greetings">
   <s:param >Mr Smith</s:param>
</s:text>

 

<s:text name="welcome">
<s:param>admin</s:param>
<s:param>ffff</s:param>
</s:text>

 六、资源查找顺序

七、在JSP中使用国际化

使用UI表单标志时,getText可以用来设置label属性,例如: <s:textfield name="name" label="%{getText('UserName')}"/>

八、资源文件里使用OGNL表达式

许多情况下,我们都需要在动行时(runtime)为国际化字符插入一些参数,例如在输入验证提示信息的时候。在Struts 2.0中,我们可以通过在资源文件的国际化字符串中使用OGNL做到这点: 格式为${表达式},例如:

validation.require=${fileName} is required

九、资源文件查找顺序

 

posted @ 2015-04-02 01:37  森林森  阅读(629)  评论(0编辑  收藏  举报