struts2 tags - Message Resource Files (i18n)

 

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即可

   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿发你好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值