1.国际化

1、 国际化原理 ? 什么是国际化 ?

同一款软件 可以为不同用户,提供不同语言界面 ---- 国际化软件

需要一个语言资源包(很多properties文件,每个properties文件 针对一个国家或者语言 ,

通过java程序根据来访者国家语言,自动读取不同properties文件 )

2、 资源包编写

properties文件命名 : 基本名称_语言(小写)_国家(大写).properties

例如 :

messages_zh_CN.properties 中国中文

messages_en_US.properties 美国英文

3、 ResourceBundle 根据不同Locale(地域信息),读取不同国家 properties文件

ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);



----------------------------

struts2中国际化:

struts2中对国际化进行了封装,我们只需要根据其提供的API进行访问就可以。

问题1:在struts2中国际化时properties文件怎样定义?(怎样定义properties)

1.全局

需要通过一个常量来声明.

struts.custom.i18n.resources

在Struts.xml中配置常量

<constant name=" struts.custom.i18n.resources " value="basename" />

对于properties配置文件可以放置在任意位置,value是对应配置文件的基础名

比如message_zh_CN.properties的basename是message;



<constant name="struts.custom.i18n.resources" value=" message"> 代表message.properties在src下

<constant name="struts.custom.i18n.resources" value=" cn.itcast.i18n.resource.message"> 代表message.properties在cn.itcast.i18n.resource包下.



例子:当服务器访问action时,会调用execute方法。

struts.xml:

<constant name="struts.custom.i18n.resources"  value="com.demo.test4.resource.message"></constant>


message.properties:

name=hello world

Action:

public class i18nTestAction1 extends ActionSupport{



@Override

public String execute() throws Exception {

//可以用getText()方法获取配置文件中的值

System.out.println(this.getText("name"));

return null;

}

}

2.局部

1.针对于action类

位置:与action类在同一个包下.

名称:ActionClassName.properties.

这个配置文件只对当前action有效。

2.针对于package下所有action

位置:在指定的包下

   名称:package.properties



例子:

Action:

public class i18nTestAction2 extends ActionSupport{



@Override

public String execute() throws Exception {

System.out.println(this.getText("hello"));

return null;

}

}

配置文件在action所在的包下面

package.properties:

hello=hello world!

.jsp页面临时使用某一个properties文件.

  <s:i18n name="cn.itcast.action.package">

  <s:text name="名称">

  </s:i18n>

例子:

JSP:

一、这个会直接全局配置文件的name值

<s:text name="name"></s:text>

二、使用<s:i18n name="">来指定配置文件

<s:i18n name="com.demo.test4.action.package">

<s:text name="hello"></s:text>

</s:i18n>



问题2:在struts2中国际化操作可以在哪些位置使用?(在哪此位置上使用)

1.action类中使用

2.配置文件中使用<validation.xml>

3.在jsp页面上使用



问题3:怎样在struts2中操作国际化?(怎样使用)

1.在action类中使用

前提:action类要继承ActionSupport类。

     getText(String name) 就可以获取配置文件中对应名称的值。

2.在validation.xml文件中使用

  <message key="名称"/>

3.在jsp页面上使用

<s:text name="名称"> 如果没有使用<s:i18n name="">来指定,会从全局配置文件中获取。

如果要从某一个配置文件中获取,通过name属性来指定, 包名.配置文件名称 .

--------------------------------------------------------

在struts2中国际化配置文件中使用动态文本

1.action中怎样使用

配置文件:msg=hello world {0}

action类:this.getText("msg",new String[]{"tom"})

结果就是 hello world tom

可以是多个占位符 例如:

msg= hello {0} {1},

this.getText("msg",new String[]{"tom","!"});

2.jsp页面上怎样使用

配置文件:msg=hello world {0}

jsp:

<s:i18n name="com.demo.test4.action.package">

<s:text name="msg">

< s:param>张三</s:param>

</s:text>

</s:i18n>

结果就是 hello world 张三.

可以是多个占位符 例如:

msg= hello {0} {1},

<s:i18n name="com.demo.test4.action.package">

<s:text name="msg">

< s:param>张三</s:param>

< s:param >哈哈</s:param>

</s:text>

</s:i18n>



在struts2中国际化使用:例如中国和美国,浏览器会根据浏览器语言选择对应的国际化配置文件

全局:

资源文件:

message_zh_CN.properties:

hello=\u6B22\u8FCE\u60A8,{0}

message_en_US.properties

hello=hello world{0}

Struts.xml:

<constant name="struts.custom.i18n.resources" value="com.demo.test4.resource.message"></constant>



JSP:记得添加s标签

<%@ taglib prefix="s" uri="/struts-tags"%>

<s:text name="hello">

<s:param>冯瑶</s:param>

</s:text>

局部:

国际化配置文件可以放在任意包下

在JSP页面上使用 <s:i18n name=""> 来指定使用哪个国际化配置文件即可

例如:

<!-- 如果不用s:i18n会默认从全局的配置文件取 -->

<s:i18n name="com.demo.test4.action.package">

<s:text name="hello">

<s:param>小阿毛</s:param>

</s:text>

</s:i18n>