国际化:
baseName_language_country.properties[一般跟struts.xml文件放在一起]
baseName_language.properties
baseName.properties
其中baseName时资源文件的基本名,可以自定义,蛋language和country必须时java支持的语音和国家。如:
中国大陆:baseName_zh_CN.properties
美国:baseName_en_US.properties
现在为应用添加两个资源文件:
第一个存放中文:test_zh_CN.properties
内容为:welcome=欢迎来到java。
第二个存放英语(美国):test_en_US.properties
内容为:welcome=welcome to java
对于中文的属性文件,编写好后,应该使用jdk提供的native2asii命令把文件转换为Unicode编码的文件。命令的使用方式如下:
native2asii 源文件.properties 目标文件.properties
配置全局资源与输出国际化信息
准备好资源文件之后,可以在struts.xml中通过struts.custom.i18n.resources常量把资源文件定义为全局资源文件,如下:
<constant name = "struts.custom.i18n.resources" value = "test" />
后面就可以在页面或在action中访问国际化信息:
- 在JSP页面中使用标签输出国际化信息:,name为资源文件中的key
- 在action类中,可以继承ActionSupport,使用getText()方法得到国际化信息,该方法的第一个参数用于指定资源文件中的key。
在表单标签中,通过key属性指定资源文件中的key,如:
例子:【全局范围的国际化】
test_zh_CN.properties中:[跟struts.xml放同一个目录]
welcome=欢迎来到java。
test_en_US.properties中:
welcome=welcome to java
struts.xml:
<struts>
<constent name = "struts.custom.i18n.resources" value = "test" />
<package name = "person" namespace = "/person" extends = "struts-default">
<action name = "manage" class = "cn.gz.action.PersonManageAction">
<result name = "message">
/WEB-INF/page/message.jsp
</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<s:text name = "welcome" />
</body>
</html>
URL:http://localhost:8080/native/index.jsp
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{
public String execute() throws Exception{
ActionContext.getContext().put("message",this.getText("welcome");
return "message";
}
}
message.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>结果信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
${message}
</body>
</html>
URL:http://localhost:8080/native/person/manage.action
国际化-输出带占位符的国际化信息
资源文件中的内容如下:
welcome={0},欢迎来到国际化{1}
在jsp页面中输出带占位符的国际化信息
<s:text name = "welcome">
<s:param><s:property value = "realname"/></s:param>
<s:param>学习</s:param>
</s:text>
在action类中获取带占位符的国际化信息,可以使用getText(String key,String[] args)或getText(String aTextName,List args)方法。
例子:
test_zh_CN.properties中:[跟struts.xml放同一个目录]
welcome={0},欢迎来到java{1}
test_en_US.properties中:
welcome={0},welcome to java{1}
struts.xml:
<struts>
<constent name = "struts.custom.i18n.resources" value = "test" />
<package name = "person" namespace = "/person" extends = "struts-default">
<action name = "manage" class = "cn.gz.action.PersonManageAction">
<result name = "message">
/WEB-INF/page/message.jsp
</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<s:text name = "welcome">
<s:param>xiaohong</s:param>
<s:param>study</s:param>
</s:text>
</body>
</html>
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{
public String execute() throws Exception{
ActionContext.getContext().put("message",this.getText("welcome");
return "message";
}
}
url:http://localhost:8080/native/index.jsp
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{
public String execute() throws Exception{
ActionContext.getContext().put("message",this.getText("welcome",new String[]("xiaohong","study"));
return "message";
}
}
URL:http://localhost:8080/native/person/manage.action
配置包范围的国际化资源
国际化-包范围资源文件
在一个大型应用中,整个应用有大量的内容需要实现国际化,如果我们把国际化的内容都放置在全局资源属性文件中,显然会导致资源文件变得过于庞大,臃肿,不便于维护,这个时候我们可以针对不同模块,使用包范围类组织国际化文件。
方法如下:
在java的包下放置package_language_country.properties资源文件,package为固定写法,处于该包及子包下的action都可以访问该资源。当查找指定key的消息时,系统会先从package资源文件查找,当找不到对应的key时,才会从常量struts.custom.i18n.resources指定的资源文件中寻找。
例子:
package_zh_CN.properties中:[跟action放同一个目录]
welcome={0},欢迎来到java{1}
package_en_US.properties中:
welcome={0},welcome to java{1}
国际化-action范围资源文件
可以为某个action单独指定资源文件,方法如下:
在action类所在的路径,放置ActionClassName_language_country.properties资源文件,ActionClassName为action类的简单名称。
当查找指定key的消息时,系统会先从ActionClassName_language_country.properties资源文件查找,如果没有找到对应的key,然后沿着当前包往上查找基本名为package的资源文件,一直找到最顶层包。如果还没有找到对应的key,最后会从常量struts.custom.i18n.resources指定的资源文件中寻找。
例子:
PersonManageAction_zh_CN.properties中:[跟action放同一个目录]
welcome={0},欢迎来到java{1}
PersonManageAction_en_US.properties中:
welcome={0},welcome to java{1}
国际化-jsp中直接访问某个资源文件
struts2提供了标签,使用标签可以在类路径下直接从某个资源文件中获取国际化数据,而无需任何配置:
<s:i18n name = "test">
<s:text name = "welcome"/>
</s:i18n>
test为类路径下资源文件的基本名。
如果要访问的资源文件在类路径的某个包下,可以这样访问:
<s:i18n name = "cn/gz/action/*package*">
<s:text name = "welcome"/>
<s:param>小红</s:param>
</s:i18n>
上面访问cn.gz.action包下基本名为package的资源文件。
index.jsp:
访问的是PersonManageAction_en_US.properties资源文件:
<s:i18n name = "cn/gz/action/PersonManageAction">
<s:text name = "welcome"/>
<s:param>小红</s:param>
</s:i18n>