How to config internationalization in struts

本文介绍如何在Struts框架中实现应用的多语言支持,通过创建资源文件并利用Struts提供的标签来显示不同语言的内容。

1. In path/src folder, create ApplicationResources.properties and ApplicationResources_zh_CN.properties. The resource file name should be ended with .properties, and different language resouce files should be end with like _zh_CN. The chinese resouce file should be changed to Unicode. We can use command native2ascii (which is a java command) to help us. But first we need to create a file ApplicationResources_zh.properties which contains Chinese characters. The command is: native2ascii -encoding GB2312 ApplicationResources_zh.properties ApplicationResources_zh_CN.properties;
2. Confirm that the <message-resource> attribute is configured correctly in struts-config.xml;
3. Using <bean:message key="xxx" /> in .jsp files.

A example:
1. create resoucres files.
Create ApplicationResources.properties in Eclipse.
--------------------------------------------------
ApplicationResources.properties

#login resources
#infos
title.login 
= Login Page
label.username 
= Username
label.password 
= Password
item.submit 
= Submit
item.reset 
= Reset
message.success 
= You are successful to login in. Now you've entered the main page.
message.failure 
= You failed to login in. You need to register your information first.

Create ApplicationResources_zh.properties using NoteBook.
--------------------------------------------------
ApplicationResources.properties

#一个简单的属性文件

title.login 
= 登录页面
label.username 
= 姓名
label.password 
= 密码
item.submit 
= 登录
item.reset 
= 重置
message.success 
= 你已经成功登陆。现进入了主页
message.failure 
= 你登录失败。现进入注册页面,请注册你的信息

--------------------------------------------------

Using command:

native2ascii -encoding GB2312 ApplicationResources_zh.properties ApplicationResources_zh_CN.properties

, we get ApplicationResources_zh_CN.properties.

#/u4e00/u4e2a/u7b80/u5355/u7684/u5c5e/u6027/u6587/u4ef6
title.login = /u767b/u5f55/u9875/u9762
label.username = /u59d3/u540d
label.password = /u5bc6/u7801
item.submit = /u767b/u5f55
item.reset = /u91cd/u7f6e
message.success = /u4f60/u5df2/u7ecf/u6210/u529f/u767b/u9646/u3002/u73b0/u8fdb/u5165/u4e86/u4e3b/u9875
message.failure = /u4f60/u767b/u5f55/u5931/u8d25/u3002/u73b0/u8fdb/u5165/u6ce8/u518c/u9875/u9762/uff0c/u8bf7/u6ce8/u518c/u4f60/u7684/u4fe1/u606f

2. Confirm <message-resouce> in struts-config.xml

<message-resources parameter="ApplicationResources"/>

3. Using <bean:message key="xxx" /> in .jsp files.
--------------------------------------------------
login.jsp

------------------------------
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
<%@ page contentType="text/html; charset=gb2312"%>
<html:html>
<head>
<title><bean:message key="title.login"/></title>
</head>
<body>
<html:form action="loginAction" method="post">
  
<bean:message key="label.username"/> <html:text property="username" size="15" />
 
<font color="red"><html:errors property="name" /></font>
 
<br/>
 
<br/>
  
<bean:message key="label.password"/> <html:password property="password" size="15" />
 
<font color="red"><html:errors property="pwd" /></font>
 
<br />
 
<br />
 
<html:submit property="submit" >
  
<bean:message key="item.submit" />
 
</html:submit>

</html:form>
</body>
</html:html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值