Struts2框架使用(八)之struts2的国际化

国际化(Internationlization),通俗地讲,就是让软件实现对多种语言的支持;

想要软件国际化需要设置配置文件,例如:

首先创建一个mrlv.properties文件,这个文件是默认使用的语言。编写所需要国际化的字段:

这里的value是Unicode编码。{0}是一个可变参数。

mrlv.properties

userName=\u7528\u6237\u5040d
password=\u5bc6\u7801
login=\u767b\u5f55
welcomeInfo=\u6b22\u8fce{0}

然后创建一个mrlv_en_US.properties文件,这个是英文。

mrlv_en_US.properties

userName=userName
password=password
login=login
welcomeInfo=welcome{0}

再创建一个mrlv_en_US.properties文件,这个是中文。

mrlv_zh_CN.properties

userName=\u7528\u6237\u540d
password=\u5bc6\u7801
login=\u767b\u5f55
welcomeInfo=\u6b22\u8fce{0}

注意,这里的命名是有规范的,***_zh_CN、***_en_US等等。

然后再struts.xml开启国际化。value填入的是默认语言

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.custom.i18n.resources" value="mrlv"></constant>
</struts>

<s:text name=""></s:text> 访问国际化资源,如下,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>Insert title here</title>
</head>
<body>
<table>
    <tr>
        <td><s:text name="userName"></s:text></td>
        <td>
            <input type="text"/>
        </td>
    </tr>
    <tr>
        <td><s:text name="password"></s:text></td>
        <td>
            <input type="text"/>
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="<s:text name='login'></s:text>"/>
        </td>
    </tr>
</table>
</body>
</html>

以及

<%@ 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>Insert title here</title>
</head>
<body>
<!-- 访问国际化资源 -->
<s:text name="welcomeInfo">
    <s:param>Jack</s:param>
</s:text>
</body>
</html>

中文环境下:

英文环境下:

 

 

如此便是struts2支持的国际化

posted @ 2017-12-21 14:53  糸色望  阅读(246)  评论(0编辑  收藏  举报