struts1 国际化资源文件

[color=violet][size=medium]1. 名称规范:[/size][/color]

[quote][Name].properties // Common and origin name
[Name]_[Language]_[Country].properties // For specific language and country

//Samples
Labels.properties
Labels_zh.properties
Labels_zh_CN.properties
Labels_en.properties
[/quote]

[color=violet][size=medium]2. 选择顺序:[/size][/color]

a. 首先根据浏览器当前语言设置,寻找有当前语言和国家后缀的文件(如Labels_zh_CN.properties),如果有,读取并显示。

b. 如果a没有,寻找有当前语言后缀的文件(如Labels_zh.properties)。

c. 如果b也没有,寻找没有后缀的默认文件(如Labels.properties)。

注意:
改变浏览器语言后,需要重新打开浏览器才能看到效果。


[color=violet][size=medium]3. 使用资源文件[/size][/color]

a. 在src目录下创建com.john.resources包,在该包里新建Labels.properties文件,加入:

label.page=My page


b. 在struts-config.xml文件中加入指定资源文件的引用

<struts-config>
<!-- parameter指定资源文件的全限定名,key指定页面引用的名称 -->
<message-resources parameter="com.john.resources.Labels" key="Labels" />
</struts-config>


c. 在jsp文件中使用:

<!-- 引入struts的bean标签:-->
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>

<!-- key指定键,bundle指定资源文件的名称,和struts-config中的key一致 -->
<bean:message key="label.page" bundle="Labels" />


也可以使用jstl的fmt标签:

<!-- 引入jstl的fmt标签:-->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!-- basename指定资源文件的全限定名,而不是struts-config中的key -->
<fmt:bundle basename="com.john.resources.Labels">
<fmt:message key="label.username" />
</fmt:bundle>

<!-- Utilize setBundle tag when the resource would be used many places -->
<fmt:setBundle basename="com.john.resources.Labels" var="inflation" scope="page" />
<fmt:message key="label.username" bundle="${inflation}" />
<fmt:message key="label.standardise" bundle="${inflation}" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值