1.Overview
支持国际化的模块:
- 1. the UI Tags
- 2. Messages and Errors from the ValidationAware interface (implemented by ActionSupport and ValidationAwareSupport)
- 3. Within action classes that extend ActionSupport through the getText() method
- 2.Resource Bundle Search Order(Resouce文件搜索规则)
- 1. ActionClass.properties
- 2. BaseClass.properties (all the way to Object.properties)
- 3. Interface.properties (every interface and sub-interface)
- 4. ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
- 5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
- 6. search up the i18n message key hierarchy itself
- 7. global resource properties
规则如下图
Package hierarchy
To clarify #5, while traversing the package hierarchy, Struts 2 will look for a file package.properties:
com/
acme/
package.properties
actions/
package.properties
FooAction.java
FooAction.properties
If FooAction.properties does not exist, com/acme/action/package.properties will be searched for, if not found com/acme/package.properties, if not found com/package.properties, etc.
3.Examples
[list]
[*]
<s:property value="getText('some.key')" />
可以嵌套在ui tags中
[*]
<s:text name="some.key" />
[*]
<s:i18n name="some.package.bundle" >
<s:text name="some.key" />
</s:i18n>
指定显示需要查找的resource bundle的文件.
[/list]
4.I18n Interceptor
该拦截器可以在运行过程中改变某个session对应的locale信息,和localization配合以实现internationlization.
5.Global Resources (struts.custom.i18n.resources) in struts.properties
6.由于struts2支持与action相关的resource的定义,导致可以造成会有多个重复的key的存在.
解决该问题的方法是,通过定义ActionSupport.properties in com/opensymphony/xwork2 and put it on your classpath.来实现所有action的key的统一定义.不过这样一来所有的action都必须继承自ActionSupport.
由以上启发,其实也可以自己写一个接口,当作标记接口,所有的action都实现该接口,并且在该接口的文件夹中加入interfacename.properties文件也可以解决该问题.
7.Formatting Dates and Numbers(日期和数字的格式化)
[list]
struts2其实通过i10n来实现数字和日期的格式化的.
定义日期或者数字显示的格式需要利用ognl表达式的特性.
例子
format.time = {0,time}
format.number = {0,number,#0.0##}
format.percent = {0,number,##0.00'%'}
format.money = {0,number,\u00A4##0.00}
通过s:text标签嵌套需要转化的数字或者日期的值来实现以上数据的格式化.
Localizing form data with getText代码示例如下
<s:textfield key="orderItem.price" value="%{getText('format.number',{'orderItem.price'})}" />
详细的说明请参考getText in ActionSupport代码中实际调用的方法为
textProvider.getText(String key, String[] args)
[/list]