struts2国际化问题(自我见解笔计)

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)