首先定義我們的國際化屬性文件resource_en_US.properties。它的明明規範:resource是可以替換成其他的名字,後面加載該文件是會用到它。後面的_en_U是根據具體的Locale來定義的這裏用的是en_US(英語英國)。
我們在該文件中定義兩個按鈕:
btn_login=Login{0}{1}
btn_logout=Logout{0}{1}
這裏的{0}{1}是通過傳入的數組參數變化的。
然後定義一個spring的bean.xml添加如下bean標籤:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 定時刷新資源文件 -->
<!-- <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> -->
<property name="basenames">
<list>
<value>resource</value>
</list>
</property>
<!-- 5秒刷新資源 -->
<!-- <property name="cacheSeconds" value="5"></property> -->
</bean>
如果用org.springframework.context.support.ReloadableResourceBundleMessageSource的話id必須叫messageSource。basenames中就是我們要加載的國際化屬性文件,resource就只我門之前定義的文件前綴在這裏用到了。這裏我們國際化的部分就定義好了接下來是自定義標籤的,定義一個MessageTag類:
package com.tz.core.tag;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MessageTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String key;
private String locale;
// 傳入國際化的參數用逗號隔開
private String params;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getMessage() {
return params;
}
public void setParams(String params) {
this.params = params;
}
@Override
public int doStartTag() throws JspException {
// 獲取上下文
ServletContext context = this.pageContext.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
// 通過key讀取相應的國際化
String str = applicationContext.getMessage(key, params == null ? null : params.split(","), getLocale(locale));
try {
this.pageContext.getOut().print(str);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
/**
* 獲得Locale
*
* @param locale
* @return
*/
private Locale getLocale(String locale) {
Locale l = null;
if (!locale.isEmpty()) {
switch (locale.toLowerCase()) {
case "en":
l = Locale.US;
break;
default:
l = Locale.CHINA;
break;
}
}
return l;
}
}
再定義tld文件:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>my tag</display-name>
<description>my tag</description>
<tlib-version>1.0</tlib-version>
<short-name>tag</short-name>
<uri></uri>
<tag>
<description>國際化標籤</description>
<name>message</name>
<tag-class>com.tz.core.tag.MessageTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>params</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>locale</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
接下來就可以在我們的前臺調用定義的標籤了:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/WEB-INF/tz.tld" prefix="my"%>
<!DOCTYPE html >
<html>
<head>
<title>tz_case1</title>
<meta charset="utf-8" />
</head>
<body>
<input type="button" value="<my:message locale="en" params="參數1,參數2" key="btn_login" />"/>
<input type="button" value="<my:message locale="en" params="參數1,參數2" key="btn_logout" />"/>
</body>
</html>
運行結果如下: