Message Resource Files (i18n)
http://struts.apache.org/2.x/docs/message-resource-files.html
1. - 基本使用 -
1.1 建立同名文件
TestAction.properties
TestAction_zh.properties
放于Action相同位置
struts2框架会自动加载这些resource bundles(【注】properties文件还可以放在其他位置 )
1.2 添加项
for TestAction.properties 添加:
age=12
for TestAction_zh.properties添加:
age=十二
1.3 在VIEW中使用 <s:text> 标签
<s:text name="age"></s:text>
1.4 请求Action时的加上request_locale参数
http://localhost:8080/locale/test.action?request_locale=zh
【注】
- ActionSupport类带了一个getLoale()函数,返回值正是在url中带的参数
Locale l = this.getLocale();
- Properties里保存的是key - value,显然key不能重复
- 默认locale为zh_CN, zh为语言, CN为国家简称
- 就近查找,如果没有zh_CN,则zh也就可以用的
- 是不是每次都配置文件中读的呢?效率未知。可能一次性的把properties加载到map中。
2. - 嵌套变量 -
s:text的属性的name其实还是动态组成的,举例说明之。
2.1 properties
创建4个property
2.2 使用动态的name参数
<s:text name="status_text[%{status}]"></s:text>
即,在name里使用%{status}来取得下标,先从Action里取得变量status的数值,然后拼凑成i18n的key名
例如,当status值为1时,该行显示 "上架",为2时,该行就显示为"准备下架"
3.- properties文件的位置 -
(Tag Guides | s:text | -> http://struts.apache.org/2.x/docs/localization.html )
ActionClass.properties
Interface.properties (every interface and sub-interface)
BaseClass.properties (all the way to Object.properties)
ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
package.properties (of the directory where class is located and every parent directory all the way to the root directory)
search up the i18n message key hierarchy itself
global resource properties
注意第5#条,一般使用该条来统一设计properties即可