i18n(马士兵)
原理:
public static void main(String[] args) {
//很据名字找到对应的资源文件
//ResourceBundle res = ResourceBundle.getBundle("ryb",Locale.US);
//可以指定地域也可以使用默认的
ResourceBundle res = ResourceBundle.getBundle("ryb");
System.out.println(res.getString("ryb.hello"));
}
struts2的国际化分成3个等级,action。packet,app等级
action级:和action在目录中,命名为action类名_zh_US.properties
一般都用的是app级别的,
在src下面创建两个文件ryb_en_US.properties,ryb_zh_CN.properties
内容如下:
ryb.hello=hello ryb
login.username=username:
login.password=password:
login.login=login
welcome.msg=welcome:{0}
ryb.hello=/u4F60/u597D/uFF0C/u4EFB/u80B2/u5F6A/uFF01
login.username=/u7528/u6237/u540D/uFF1A
login.password=/u5BC6 /u7801/uFF1A
login.login=/u767B/u9646
welcome.msg=/u6B22/u8FCE/u4F60/uFF1A{0}
在struts.xml文件里面配置上资源文件的前缀名
<constant name="struts.custom.i18n.resources" value="ryb"></constant>
在页面里面这样使用
<s:property value="getText('login.username')" />
参数问题: (welcome.msg有一个参数)
<s:text name="welcome.msg">
<s:param value="getText('login.username')"></s:param>
</s:text>
动态语言切换:
struts的语言切换很简单的,只需要传一个参数request_locale,例如:
<a href="index2.action?request_locale=en_US">英文</a>
<a href="index2.action?request_locale=zh_CN">中文</a>
struts2国际化
最新推荐文章于 2024-04-27 18:59:44 发布