国际化
一、什么是国际化
一款软件可以为不同国家的来访者提供不同语言的界面,那么这个软件就是国际化的。
这需要为每种上语言提供资源包(很多语言包),程序通过来访者的国家和语言来定位资源包。
基本名称_语言_国家.properties
例如:res_zh_CN.properties、res_en_US.properties
二、Struts2国际化文件分类
1、全局国际化文件:整个程序都可以使用(最常用)
2、特定包中可以使用(一个包中所有Action可以使用);
3、特定Action中可以使用(一个Action可以使用);
4、临时信息文件(JSP中i18n标签中可以使用)
三、国际化信息的应用场景
1、JSP页面中使用国际化信息;
2、Action中使用国际化信息;
3、配置文件中使用国际化信息。
四、全局国际化
res_zh_CN.properties
#用户名
username=\u7528\u6237\u540D
#用户名不能为空
error.username=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
1. 指定资源文件路径:
在struts.xml文件中指定资源文件的路径:
<constantname="struts.custom.i18n.resources" value="res/">
这说明在src目录下需要有res_语言_国家.properties文件。
<constantname="struts.custom.i18n.resources" value="cn.hcx.action.res/">
这说明在src/cn/hcx/action目录下需要有res_语言_国家.properties文件。
2. 在JSP中使用<s:text>标签获取资源文件信息
在JSP中使用
<s:fielderror />
<form action="<c:url value='Demo1Action.action'/>" method="post">
<s:text name="username"/>:<input type="text" name="username"/><br/>
<input type="submit" value="提交"/>
</form>
在Action中使用
public class Demo1Action extends ActionSupport {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception {
System.out.println(this.getText("error.username"));
return NONE;
}
}
在配置文件中使用,例如在校验文件中使用。
<validators>
<field name="username">
<field-validator type="requiredstring">
<message key="error.username" />
</field-validator>
</field>
</validators>
package cn.hcx.d_i18n;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
@Override
public String execute() throws Exception {
String name = getText("name",new String[]{"haha","heihei"});
System.out.println(name);//从国际化资源文件中取值
return SUCCESS;
}
}
name=defaultTom
name=zhTom
name=USTom
<struts>
<!-- 配置全局国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="cn.hcx.d_i18n.i18n"></constant>
<package name="i18n" namespace="/i18n" extends="struts-default" >
<action name="Demo1Action" class="cn.hcx.d_i18n.Demo1Action">
<result>/i18n/demo1.jsp</result>
</action>
</package>
</struts>
jsp:
<body>
<!-- 在页面中取 -->
<s:text name="name"></s:text>
<hr>
<!-- 要求使用临时国际化资源文件 -->
<s:i18n name="cn.hcx.d_i18n.temp">
<s:text name="name"></s:text>
</s:i18n>
</body>
五、局部国际化文件
全局国际化文件是整个程序中都可以使用的,文件的位置需要在struts.xml文件中指定。
而局部国际化文件不需要在struts.xml文件中指定,但文件的名称,以及位置是固定的。
局部国际化文件分为两种:特定Action中可以使用,以及特定包及子包中可以使用。
Action国际化文件
例如想提供一个只能在cn.hcx.action.TestAction中使用的国际化文件,那么就需要在cn.hcx.action包下创建TestAction_zh_CN.properties文件,
这个文件的基本名称必须与Action的名称相同。
包国际化文件
例如想提供一个只能在cn.hcx.action包中使用的国际化文件,那么就需要在cn.hcx.action包下创建package_zh_CN.properties文件。
这个文件也可以在cn.hcx.action包的子包中被使用。例如有Actoin的命名为:cn.hcx.action.demo1.Demo1Action,
在这个Action中也可以cn.hcx.action.package_zh_CN.properties文件。
properties文件名字与action名字一样 就是专门为该action设置的文件
properties:
name=Demo1ActionDefaultTom
name={0}Demo1ActionCNTom{1} <!--占位符-->
name=Demo1ActionUSTom
action:
package cn.hcx.d_i18n;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
@Override
public String execute() throws Exception {
String name = getText("name",new String[]{"haha","heihei"});
System.out.println(name);//从国际化资源文件中取值
return SUCCESS;
}
}
六、临时国际化文件
临时国际化文件是在<s:i18n>标签中使用。
<s:i18n name="cn.hcx.demo1.action.res">
<s:text name="username" />
</s:i18n>
<s:i18n>标签的name属性指定国际化文件的位置,这说明需要在src/cn.hcx.demo1/action/目录下需要有res_zh_CN.properties文件。
七、在国际化文件中使用占位符
在国际化文件中使用占位符,例如在国际化文件中有如下内容:
login.error={0}或{1}不能为空
其中{0}和{1}就是点位符,这需要指定值来替换占位符。
String s =this.getText(“login.error”, new String[]{“用户名”, “密码”});
System.out.println(s);//用户名或密码不能为空。