Struts2国际化:实现中英文转换

项目结构图
这里写图片描述
简单实现登录页面的中英文转换
1.index.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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标签里的东西相当于中文的用户名,密码,登录,中国,name要和properties文件里面的一致  -->
<s:text name="uname"/>:<input type="text"><br>
<s:text name="upwd"></s:text>:<input type="password"><br>
<input type="submit" value='<s:text name="login"></s:text>'>
<a href="i18nAction_change?request_locale=zh_CN"><s:text name="cn"></s:text></a>|<a href="i18nAction_change?request_locale=en_US"><s:text name="en"></s:text></a>
</body>
</html>

2.zs_zh_CN.properties代码(里面的编码不用管,输入法直接输入用户名、密码等,编码自动生成)

uname=\u7528\u6237\u540D
upwd=\u5BC6\u7801
login=\u767B\u5F55
cn=\u4E2D\u6587
en=\u82F1\u6587

3.zs_en_US.properties代码

uname=UserName
upwd=Password
login=Login
cn=Chinese
en=English

4.I18nAction代码

package com.zs.action;

import com.opensymphony.xwork2.ActionSupport;

//国际化
public class I18nAction extends ActionSupport{
    //国际化改变语言的方法
    public String change() {
        return "i18n";
    }

}

5.struts.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!--开启国际化的使用  -->
    <!--name必须是这样写,调用它自带的资源,value和Properties的文件前缀一致,zs_en_US.properties -->
    <constant name="struts.custom.i18n.resources" value="zs"></constant>
    <package name="default" extends="struts-default" namespace="/">
        <!--设置默认的启动页面 -->
        <default-action-ref name="login">
        </default-action-ref>
        <action name="login">
            <result type="redirect">index.jsp</result>
        </action>
        <action name="i18nAction_*" class="com.zs.action.I18nAction" method="{1}">
            <result name="i18n">/index.jsp</result>
        </action>
    </package>

</struts>

备注:struts2的环境搭建要建好
其他语言的转换也可以http://www.lingoes.cn/zh/translator/langcode.htm

说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值