您的位置:首页 > 编程语言 > Java开发

Struts2国际化

2014-07-17 14:08 169 查看
使用Struts2的国际化机制能够将不同语言版本的字符保存在属性文件中,在不需要重新开发应用的前提下,实现不同语言版本的应用。

1、视图中的文本

2、视图中标签的属性

如果采用JSP文件,如register.jsp

<body>
<s:text name="register.info"></s:text><br>
<s:form action="Customer">
<s:textfield name="custname" label="Input your custname"></s:textfield>
<s:password name="pwd" label="Input your password"></s:password>
<s:textfield name="age" label="Input your age"></s:textfield>
<s:textfield name="address" label="Input your address"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form>
</body>
3、Action类中的文本

Action类如果需要使用国际化的文本,往往继承ActionSupport类,调用ActionSupport类中的getText方法即可获得国际化资源。

4、使用校验框架的配置信息

创建messageResource.properties文件

#The Text

register.info=Please input your register Info:

#The Form

custname.label=Input your name

pwd.label=Input your password

age.label=Input your age

address.label=Input your address

register.button=Register New Customer
创建struts.properties文件(在src目录创建,是一个属性文件,文件中都是以key=value姓氏定义的键值对),配置资源文件的名字

struts.custom.i18n.resources=messageResource
修改register.jsp文件

<body>
<s:text name="register.info"></s:text><br>
<s:form action="Customer!register">
<s:textfield name="custname" key="custname.label"></s:textfield>
<s:password name="pwd" key="pwd.label"></s:password>
<s:textfield name="age" key="age.label"></s:textfield>
<s:textfield name="address" key="address.label"></s:textfield>
<s:submit name="tijiao" key="register.button"></s:submit>
</s:form>
</body>


使用多个国际化资源文件

为了支持多个语言版本的国际化资源文件,定义资源文件的命名规则:

资源文件基础名_语言版本缩写_国家代码缩写.properties
例支持中文的资源文件:messageResource_zh_CN.properties

#The Text
register.info=请输入您的注册信息:
#The Form
custname.label=输入用户名:
pwd.label=输入密码:
age.label=输入年龄:
address.label=输入地址
register.button=注册新用户


使用native2ASCII messageResource_zh_CN.proterties temp.properties

将生成文件temp.properties中的内容拷贝至messageResource_zh_CN.proterties

#The Text
register.info=\u8bf7\u8f93\u5165\u60a8\u7684\u6ce8\u518c\u4fe1\u606f\uff1a
#The Form
custname.label=\u8f93\u5165\u7528\u6237\u540d\uff1a
pwd.label=\u8f93\u5165\u5bc6\u7801\uff1a
age.label=\u8f93\u5165\u5e74\u9f84\uff1a
address.label=\u8f93\u5165\u5730\u5740
register.button=\u6ce8\u518c\u65b0\u7528\u6237


支持美国英语的资源文件:messageResource_en_US.properties

#The Text
register.info=Please input your register Info:
#The Form
custname.label=Input your name
pwd.label=Input your password
age.label=Input your age
address.label=Input your address
register.button=Register New Customer


配置文件struts.properties

struts.custom.i18n.resources=messageResource_zh_CN
#struts.custom.i18n.resources=messageResource_en_US
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: