好记性不如赖笔头…………
JAVA本身提供 了Locale、ResourceBundle类来实现国际化,下面提供一份范例:
1、创建资源包,在资源下创建两个文件,分别为resource_en_US.properties、resource_zh_CN.properties(命名规则为:自定义名称语言代号国家代号.properites)
2、在创建的两个文件中分别输入内容
resource_en_US.properties 文件代码如下:
---------------------------------------
key=Global resource package file
userName=userName
password=password
checkCode=checkCode
submit=submit
===========================
resource_zh_CN.properties文件代码如下:
----------------------------------------------
key=\u5168\u5C40\u8D44\u6E90\u5305\u6587\u4EF6
userName=\u6635\u79F0
password=\u5BC6\u7801
checkCode=\u9A8C\u8BC1\u7801
submit=\u63D0\u4EA4
图片如下 :
3、编写测试代码:
package com.ckinghan.i18nTest;
import java.util.Locale;
import java.util.ResourceBundle;
public class TestI18n {
public static void main(String[] args) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("com/ckinghan/resource/resource",Locale.CHINA);
System.out.println(resourceBundle.getString("key"));
}
}
测试结果如下:
全局资源包文件
将locale.CHINA改为Locale.US,测试结果如下 :
Global resource package file
====================================我是分隔线====================================
Struts2的国际化测试案例如下:
1、编写i18n.jsp文件,文件内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%-- 引入Struts2的标签 --%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>测试Struts2的国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<%-- 使用JAVA代码的方式实现国际化 --%>
<%
Locale locale = request.getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle("com.ckinghan.resource.resource", locale);
%>
<body>
<h1> 国际化:在JSP页面使用JAVA代码实现国际化(不建议)</h1>
<form action="">
<%=resourceBundle.getString("userName") %>:<input name="userName" type="text"/><br/>
<%=resourceBundle.getString("password") %>:<input name="password" type="password"/><br/>
<%=resourceBundle.getString("checkCode") %>:<input name="checkCode" type="text"/><br/>
<input type="submit" value="<%=resourceBundle.getString("submit") %>"/><br/>
</form>
<hr/>
<h1>全局变量的国际化资源包</h1>
<s:text name="key"></s:text>
<hr/>
<h1>在jsp页面通过Struts2的标签指定资源包</h1>
<s:i18n name="com.ckinghan.package">
<s:text name="key"></s:text>
</s:i18n>
<hr/>
<h1>如果没有动作类的国际化资源包,正常情况下,会加载下面的数据(加载顺序:动作类资源>包资源>全局资源)</h1>
<s:i18n name="com.ckinghan.resource.resource">
<s:text name="key"></s:text>
</s:i18n>
</body>
</html>
2、创建并编写Struts.xml文件,Struts.xml的代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts-devMode" value="true"></constant>
<!-- 全局变量:指定国际化资源包 -->
<constant name="struts.custom.i18n.resources" value="com.ckinghan.resource.resource"></constant>
<package name="i18nTest" extends="struts-default">
<action name="i18n" class="com.ckinghan.web.action.I18nAction">
<result>/i18n.jsp</result>
</action>
</package>
</struts>
3、编写I18nAction文件,代码如下:
package com.ckinghan.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class I18nAction extends ActionSupport {
@Override
public String execute() throws Exception {
return super.execute();
}
}
4、访问Localhost:8080/Struts_I18n/i18n.action结果如下:
Struts2的国际化资源包查找顺序如下:
Resource Bundle Search Order
Resource bundles are searched in the following order:
1、ActionClass.properties
2、Interface.properties (every interface and sub-interface)
3、BaseClass.properties (all the way to Object.properties)
4、ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
5、package.properties (of the directory where class is located and every parent directory all the way to the root directory)
6、search up the i18n message key hierarchy itself
7、global resource properties