在WEB应用中使用国际化标签库实现固定文本的国际化
1、国际化标签
<fmt:setLocale> //设置一个全局的地区代码
<fmt:requestEncoding> //设置统一的请求编码
<fmt:setLocale>
属性名 |
是否支持EL |
类型说明 |
属性描述 |
value |
true |
String或java.util.Locale |
指定用户的本地化信息,可以是一个字符串或java.util.Local实例对象。如果是字符串必须包含小写的语言名而后是大写的国家名:zh_CN |
variant |
true |
String |
指定创建Locale实例对象时设置的变量部分,它作用于标识开发商或特定浏览器为实现扩展功能而自定义的信息。 |
scope |
false |
String |
指定构造出来的Locale实例对象保存在哪个web作用域中。 |
实例:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<c:set var="todayValue" value="<%=new Date() %>"/>
中文:<fmt:setLocale value="zh_CN"/> <fmt:formatDate value="${todayValue}"/><br>
英文:<fmt:setLocale value="en_US"/> <fmt:formatDate value="${todayValue}"/>
</body>
</html>
页面输出:
2、信息显示标签
<fmt:bundle> //设置临时要读取的资源文件
<fmt:message> //通过key取得value
<fmt:setBundle> //设置一个要读取的全局的资源文件
<fmt:setBundle>
属性名 |
是否支持EL |
属性类型 |
属性描述 |
basename |
true |
String |
指定创建ResourceBundle实例对象的基名。 |
var |
false |
String |
指定将创建出来的ResourceBundle实例对象保存在web域中的属性名。 |
scope |
false |
String |
指定将创建出来的ResourceBundle实例对象保存在哪个web作用域中。 |
<fmt:message>
属性名 |
是否支持EL |
属性类型 |
属性描述 |
key |
true |
String |
指定要输出的信息的关键字。 |
bundle |
true |
LocalizationContext |
指定ResourceBundle对象在web域中的属性名称。 |
var |
false |
String |
用于指定格式化输出结果保存到某个web域中的属性名。 |
scope |
false |
String |
指定将格式化结果保存到哪个web域中。 |
实例:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>bundle test</title>
</head>
<body>
<fmt:bundle basename="dbconn">
数据库驱动程序名:<fmt:message key="driverName"/><br>
连接字符串:<fmt:message key="connString"/><br>
用户名:<fmt:message key="userName"/><br>
密码:<fmt:message key="password" var="password"/> <c:out value="${password}"/><br>
名字:<fmt:message key="name"/><br>
动态提示信息:<fmt:message key="messageTemp"/><br>
</fmt:bundle>
<!-- 修改.properties文件中某个键的动态值 -->
<c:set var="todayTemp" value="<%=new Date() %>"/>
<fmt:setBundle basename="dbconn"/>
动态提示信息:
<fmt:message key="messageTemp">
<fmt:param>张三</fmt:param>
<fmt:param value="${todayTemp}"></fmt:param>
</fmt:message>
</body>
</html>
其对应的读取文件为dbconn.properties(当然是放在src也就是web-inf/classes下了),内容为:
#SQL Server
driverName=com.microsoft.jdbc.sqlserver.SQLServerDriver
connString=jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=testDatabase
userName=sa
password=123456
name=\u5C0F\u5E73\u679C
messageTemp=myname is {0},today is {1,date}
页面输出:
解释一下其中的几个标签:
<fmt:bundle>标签用于绑定数据源.properties文件;
<fmt:bundle basename="源文件名,且不能带后缀哦,如上例就可以了" prefix=""> 语句,代码等 </fmt:bundle>
<fmt:message>标签用于从指定的资源文件中把指定的键值取出来;
<fmt:message key="" [var="varname"] [bundle=""] [scope="page|..."]/> 如果用到var的话就不会在页面直接输出,而需要用到<c:out>标签来进行页面的输出,如上例;
<fmt:message>标签可以配合<fmt:param>标签来进行设定<fmt:message>标签指向键的动态值,如上例;
<fmt:setBundle>标签用来设置默认的数据来源;
<fmt:setBundle basename="" [ var=""] [scope="" ] />
3、数字及日期格式化标签
<fmt:formatDate> //日期的格式化、把普通的日期显示格式化为标准的日期格式
<fmt:parseDate> //日期的反格式化、把已经格式化过后的标准日期格式转化为普通类型、例如字符串
<fmt:formatNumber> //数字的格式化、把普通的数字格式化为标准的数字格式
<fmt:parseNumber> //数字的反格式化、把已经格式化过后的数字反过来转换成普通的数字
<fmt:setTimeZone> //设置一个全局的时区
<fmt:timeZone> //设置一个临时的时区
3.1、<fmt:formatDate>标签:
type:指定要格式化的形式、比如说是只格式化日期、或者只格式化时间、或者是两者一起、默认为date;一般为both
datestyle:指定日期的显示样式,默认为default
timestyle:指定时间的显示样式,默认为default
pattern:在自定义日期显示格式的时候、需要指定的格式、例如:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒(要区分大小写、目的是为了不与同字母混淆)
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head><title></title></head>
<body>
<%
pageContext.setAttribute("dateref" , new Date()) ;
%>
<fmt:timeZone value="HST">
<fmt:formatDate value="${dateref}" type="both" dateStyle="full" timeStyle="full" var="date"/>
</fmt:timeZone>
<fmt:formatDate value="${dateref}" type="both" dateStyle="default" timeStyle="default" var="date"/>
<h3>default显示日期时间:${date}</h3>
<fmt:formatDate value="${dateref}" type="both" pattern="yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" var="date"/>
<h3>自定义格式显示日期时间:${date}</h3>
</body>
</html>
3.2、<fmt:formatNumber>标签:
maxIntegerDigits:可以显示的最大整数位
maxFractionDigits:可以显示的最大小数位
groupingUsed:是否在数字中加“,”
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head><title></title></head>
<body>
<fmt:formatNumber value="351989.356789" maxIntegerDigits="7" maxFractionDigits="3" groupingUsed="true" var="num"/>
<h3>格式化数字:${num}</h3>
<fmt:formatNumber value="351989.356789" pattern="##.###E0" var="num"/>
<h3>科学计数法:${num}</h3>
</body>
</html>
登录注册范例:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--导入国际化标签库 --%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML>
<html>
<head>
<title>国际化(i18n)测试</title>
</head>
<fmt:setBundle var="bundle" basename="me.gacl.i18n.resource.myproperties" scope="page"/>
<form action="">
<fmt:message key="username" bundle="${bundle}"/><input type="text" name="username"><br/>
<fmt:message key="password" bundle="${bundle}"/><input type="password" name="password"><br/>
<input type="submit" value="<fmt:message key="submit" bundle="${bundle}"/>">
</form>
</body>
</html>