struts1 国际化资源文件

1. 名称规范:

引用
[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


2. 选择顺序:

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

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

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

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


3. 使用资源文件

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}" />

猜你喜欢

转载自czj4451.iteye.com/blog/1701223