一、I18N
建立相应的app_zh_CN.properties(可以使用JDK中的native2ascii进行手工转换编码)
welcome.msg=\u6B22\u8FCE
测试类
publicclass Test {
publicstaticvoid main(String[]args) {
ResourceBundleres = ResourceBundle.getBundle("app", Locale.CHINA);
System.out.println(res.getString("welcome.msg" ));
}
}
对于中文问题的处理:我使用的是Myeclipse 8.5里面内嵌了Myeclipse Properties Editor,如果使用之前的版本要自行下载 Properties Editor。
二、Struts中的I18N资源文件(参考struts2_3200_BBS2009_08_I18N)
1) action级别(很少使用)
将*.properties放在相应的action目录下面。名称为XXXaction_zh_CN.properties
2) package级别(很少使用)
将*.properties放在action目录下面。名称为package_zh_CN.properties
3) app级别
将*.properties放在src目录,名称为XXX_zh_CN.properties.
需要在struts.xml中配置相应资源文件的前缀名
<constant name="struts.custom.i18n.resources" value="bbs2009"></constant>
在页面中使用
<s:property value="getText('login.username')"/>
getText()方法在ActionSupport中。
4) 处理资源文件的参数
资源文件配置:
welcome.msg=welcome:{0}
{0}为占位符,表示第一个参数。
页面中传递参数
<s:text name="welcome.msg">
<s:param value="username"></s:param>
</s:text>
三、动态语言切换
创建LangAction控制跳转
publicclass LangAction extends ActionSupport {
@Override
public String execute() throws Exception {
returnsuper.execute();
}
}
页面中加入下面链接
<a href="admin/lang?request_locale=en_US">en</a>
<a href="admin/lang?request_locale=zh_CN">cn</a>