1.struts2所指的国际化是指在action范围内;而且不能是从定向视图
{
public void ActionContext.getContext().setLocale(Locale locale)
Sets the Locale for the current action.
Parameters:
locale - the Locale for the current action. //api所说只是当前action之内
}
2.要向所有action都享用一个国际化值
ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale); //locale 是本地语言对象或者
ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE",locale);
3.example
{
String u1="en"; //英文language --中文 key:zh value:CN
String u2="US";
Locale loc = new Locale(u1,u2);
ActionContext.getContext().setLocale(loc);
}
4.locale解释
Locale.CHINA 就等于一个locale对象;打印结果是zh_CN
Locale.CHINESE 就是一个key值而己;打印结果是zh
Locale.US 就等于一个locale对象;打印结果是en_US
Locale.ENGLISH 就是一个key值而己;打印结果是en
5.配置文件
1.xml配置
首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties
Messageresource配置
在messageResource_en_US.properties加入以下内容
label.helloWorld=hello,world
在messageResource_zh_CN.properties加入以下内容
label.helloWorld=你好,世界
jsp页面的国际化
通过使用标签<s:text name="label.helloWorld"/>输出国际化
label.helloWorld为资源文件中定义的key
总结:
1.如果要实现国际化;要么根据浏览器的默认语言;struts2会自动去匹配语言选项
2.如果要动态设置;就必须在struts2的作用域(action)中;然后通过ActionContext.getContext().setLocale(loc);这样设置
本文详细介绍了Struts2框架中的国际化实现方法,包括如何在action范围内设置国际化,如何在所有action中共享国际化值,以及如何配置资源文件和在jsp页面中使用国际化。通过示例展示了设置本地语言对象并应用到action中的过程,并阐述了如何在配置文件中指定资源文件以实现不同语言的国际化。
2532

被折叠的 条评论
为什么被折叠?



