国际化
国际化
就是根据自己电脑的语言环境页面将会有语言上的变化。
本地化对象java.util.Locale:
可以根据本地化对象Locale获取当前使用的电脑的语言环境信息,Locale.getDefaultLocale 可自行查看api。
获取资源包对象java.util.ResourceBundle:
根据本地化对象取想对象的资源包,getBundle(String baseName,Locale locale)。
资源包取名规则:
基础名_语言简称_国家简称.properties
基础名.properties -default。
国际化分为静态国际化和动态国际化。
静态国际化:
package cn.i18n.fbi;
import java.util.Locale;
import java.util.ResourceBundle;
public class StaticI18N2 {
public static void main(String[] args) {
Locale locale = Locale.getDefault();
System.out.println(locale.getLanguage());
Locale localeUS = Locale.US;
//ystem.out.println(locale.getCountry());
//System.out.println(locale.getDisplayCountry());
//System.out.println(locale.getLanguage());
//System.out.println(locale.getDisplayLanguage());
ResourceBundle bundleUS = ResourceBundle.getBundle("cn.i18n.fbi.msg", localeUS);
System.out.println(bundleUS.getString("title"));
System.out.println(bundleUS.getString("user"));
System.out.println(bundleUS.getString("password"));
Locale localeChina = Locale.CHINA;
//ystem.out.println(locale.getCountry());
//System.out.println(locale.getDisplayCountry());
//System.out.println(locale.getLanguage());
//System.out.println(locale.getDisplayLanguage());
ResourceBundle bundleChina = ResourceBundle.getBundle("cn.i18n.fbi.msg", localeChina);
System.out.println(bundleChina.getString("title"));
System.out.println(bundleChina.getString("user"));
System.out.println(bundleChina.getString("password"));
}
}
动态国际化
package cn.i18n.fbi;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.zip.DataFormatException;
public class DynamicI18N {
public static void main(String[] args) throws ParseException {
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
String string =numberFormat.format(1000);
System.out.println(string);
Number number = numberFormat.parse(string);
System.out.println(number);
NumberFormat numberFormat2 = NumberFormat.getNumberInstance(Locale.CHINA);
String string2 =numberFormat2.format(1000);
System.out.println(string2);
Number number2 = numberFormat2.parse(string2);
System.out.println(number2);
NumberFormat numberFormat3 = NumberFormat.getPercentInstance(Locale.CHINA);
String string3 =numberFormat3.format(1000);
System.out.println(string3);
Number number3 = numberFormat3.parse(string3);
System.out.println(number3);
int dateStyle = DateFormat.SHORT;
int timeStyle = DateFormat.SHORT;
DateFormat df = DateFormat.getDateTimeInstance(dateStyle,timeStyle,Locale.getDefault());
df.format(new Date());
System.out.println(df.format(new Date()));
}
}
jsp上的使用
<%@page import="java.util.ResourceBundle"%>
<%@page import="java.util.Locale"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
Locale locale = request.getLocale();
ResourceBundle r = ResourceBundle.getBundle("cn.i18n.fbi.msg", locale);
String title = r.getString("title");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%--设置本地化对象 --%>
<%--设置工具类 --%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=title%></title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><%=r.getString("user") %></td>
<td>
<input id = "username" name="username" type="text" class="inputbox" style="width:160px;hight:20px">
</td>
</tr>
<tr>
<td><%=r.getString("password") %></td>
<td>
<input id = "password" name="password" type="password" class="inputbox" style="width:160px;hight:20px">
</td>
</tr>
</table>
</body>
</html>
使用jstl
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%--引入国际化标签库与格式化标签库 --%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%--设置本地化对象 --%>
<fmt:setLocale value="${pageContext.request.locale}"/>
<%--设置工具类 --%>
<fmt:setBundle basename="cn.i18n.fbi.msg" var="bundle" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><fmt:message key = "title" bundle="${bundle}"></fmt:message></title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><fmt:message key = "user" bundle="${bundle}"/></td>
<td>
<input id = "username" name="username" type="text" class="inputbox" style="width:160px;hight:20px">
</td>
</tr>
<tr>
<td><fmt:message key = "password" bundle="${bundle}"/></td>
<td>
<input id = "password" name="password" type="password" class="inputbox" style="width:160px;hight:20px">
</td>
</tr>
</table>
</body>
</html>
格式化数值和日期
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>格式化</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <body> <!-- 格式化金额 格式: 0.00 保留2为小数,会自动补0 #.## 保留2为小数,不自动补0 --> <fmt:formatNumber pattern="0.00" value="100" ></fmt:formatNumber> <br> <fmt:formatNumber pattern="#.##" value="100" ></fmt:formatNumber> <br> <fmt:formatDate pattern="yyyyMMdd" value="<%=new Date() %>"/> <br> <% request.setAttribute("date", new Date()); %> <fmt:formatDate pattern="yyyy-MM-dd" value="${date }"/> <br> </body></html> |

浙公网安备 33010602011771号