Struts2 国际化实例分析

第一节:国际化简介

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

 

第二节:Struts2 国际化设置

<constant name="struts.custom.i18n.resources" value="java1234"></constant>
<s:text name=""></s:text> 访问国际化资源

 

struts.xml:

 

<?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="i18n"></constant>
    
</struts>

 

上面取的value为‘i18n’,默认加载的资源文件名字为i18n.properties;  默认语言采用中文;

中文加载的资源文件为:i18n_zh_CN.properties;

英文加载的资源文件为:i18n_en_US.properties;

i18n.properties:

中文放在properties中使用unicode编码

{0}指的是第0个位置,参数的占位符。

 

在浏览器中选择语言为中文,就会显示出中文的用户名这些东东,选择英文就会出现userName,默认显示中文

i18n_zh_CN.properties:

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

i18n_en_US.properties:

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

 

login.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>

 

welcome.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>
<s:text name="welcomeInfo">
    <s:param>Jack</s:param>
</s:text>
</body>
</html>

 

中文输出:

 

 

英文输出:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值