struts2 国际化

[quote]
相关类
ResourceBundle
Locale
MessageFormat

一、底层是如何运作的
1、如何获得当前JDK系统已经提供的国家语言的支持
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales)
{
//System.out.println(locale.getDisplayCountry() + " " + locale.getCountry());
System.out.println(locale.getDisplayLanguage() + " " + locale.getLanguage());
}

2、属性配置文件
命名规则:baseName_language_country.properties
如:hellofile_en_US.properties
hellofile_zh_CN.properties
例子:
hellofile_zh_CN.properties中内容:
hello=\u4F60\u597D
hellofile_en_US.properties中内容:
hello=world

例:
Locale locale = Locale.getDefault(); //获得当前默认Locale

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
String value = bundle.getString("hello");
System.out.println(value);

bundle = ResourceBundle.getBundle("hellofile",Locale.US);
value = bundle.getString("hello");
System.out.println(value);

结果:
你好
world

3、
hellofile_en_US.properties中内容:
hello = world : {0}
hellofile_zh_CN.properties中内容:
hello = \u4F60\u597D : {0}

例:
Locale locale = Locale.getDefault();

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
String value = bundle.getString("hello");
System.out.println(value);
String result = MessageFormat.format(value, new Object[]{"上海"}); //用上海填充{0}
System.out.println(result);

bundle = ResourceBundle.getBundle("hellofile",Locale.US);
value = bundle.getString("hello");
System.out.println(value);
result = MessageFormat.format(value, new Object[]{"北京"}); //用北京填充{0}

System.out.println(result);

二、struts2的国际化

1、页面中的国际化
(1)
<s:text name=""></s:text>
name中的值对应配置文件中的key

例:
message_zh_CN.properties中内容:
addUser = \u589e\u52a0\u7528\u6237
页面中使用:
<s:text name="addUser"></s:text>

(2)form表单中内容国际化theme属性不能为simple
例:
<s:textfield key=""></s:textfield>

(3)临时国际化
<center>
<s:i18n name="temp">
<s:text name="hello"></s:text>
</s:i18n>
</center>
配置文件:temp_zh_CN.properties
hello = \u4f60\u597d

(4)传参
<s:i18n name="temp">
<s:text name="hello">
<s:param>didi</s:param>
</s:text>
</s:i18n>
配置文件:temp_zh_CN.properties
hello =\u4F60\u597D,{0}


2、validate中的国际化
通过getText方法获得Key对应的值
例:
message_zh_CN.properties中内容:
username.invalid = \u7528\u6237\u540d\u586b\u5199\u4e0d\u6b63\u786e
validate中使用方法:
this.addActionError(this.getText("username.invalid"));

例:
message_zh_CN.properties中内容:
username.invalid = \u7528\u6237\u540d "{0}" \u586b\u5199\u4e0d\u6b63\u786e
validate中使用方法:
List list = new ArrayList();
list.add(username);
this.addActionError(this.getText("username.invalid",list));

this.addActionError(this.getText("username.invalid",new String[]{username}));

3、校验框架XML中的国际化
<message key=""></message>
key对应配置文件中的key

例:
校验XML中
<message key="username.xml.invalid"></message>
message_zh_CN.properties中内容:
username.xml.invalid = \u6821\u9a8c\u6846\u67b6\u63d0\u793a\u7528\u6237\u540d\u4e0d\u6b63\u786e

4、国际化资源文件的存活范围
struts2提供的各个级别的文件名命名规则
包级别:package_en_US.properties package_zh_CN.properties
类级别:class_en_US.properties (class用具体类名(action类))
全局:globalMessages_en_US.proterties(需要在struts.properties 配置
struts.custom.i18n.resources=globalMessages)

优先级:类级别>包级别>全局

原文来自:http://blog.csdn.net/dyc87112/archive/2009/02/17/3899026.aspx

转载请著名出处。


[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值