首先定义我们的国际化属性文件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>
运行结果如下: