1. 程式人生 > 實用技巧 >【JavaWeb】i18n 國際化

【JavaWeb】i18n 國際化

i18n 國際化

什麼是 i18n

國際化(Internationalization)指的是同一個網站可以支援多種不同的語言,以方便不同國家,不同語種的使用者訪問。

希望相同的一個網站,不同人訪問的時候可以根據使用者所在的區域顯示
不同的語言文字,但是網站的佈局樣式等不發生改變。

相關要素

國際化三要素

  • Local 物件 表示不同的時區,位置,語言
    • zh_CN 中國 中文
    • en_US 美國 英文
  • Properties 屬性配置檔案 配置檔案命名規則為:baseName_local.properties,例如
    • 中文的配置檔名是:i18n_zh_CN.properties
    • 英文的配置檔名是:i18n_en_US.properties
  • ResourceBundle 資源包
    • ResourceBundle.getBundle() 根據給定的 baseName 和 Local 讀取相應的配置檔案,得到檔案內容資訊
    • ResourceBundle.getString(key) 得到不同國家的語言資訊

國際化資源使用

i18n_en_US.properties 檔案

username=username
password=password
sex=sex
age=age
regist=regist
boy=boy
email=email
girl=girl
reset=reset
submit=submit

i18n_zh_CN.properties 檔案

username=使用者名稱
password=密碼
sex=性別
age=年齡
regist=註冊
boy=男
girl=女
email=郵箱
reset=重置
submit=提交
public class I18nTest {
    @Test
    public void testLocale(){
        // 獲取你係統預設的語言。國家資訊
        // Locale locale = Locale.getDefault();
        // System.out.println(locale);
        // for (Locale availableLocale : Locale.getAvailableLocales()) {
        // System.out.println(availableLocale);
        // }
        // 獲取中文,中文的常量的 Locale 物件
        System.out.println(Locale.CHINA);
        // 獲取英文,美國的常量的 Locale 物件
        System.out.println(Locale.US);
    }

    @Test
    public void testI18n(){
        // 得到我們需要的 Locale 物件
        Locale locale = Locale.CHINA;
        // 通過指定的 basename 和 Locale 物件,讀取 相應的配置檔案
        ResourceBundle bundle = ResourceBundle.getBundle("i18n", locale);
        System.out.println("username:" + bundle.getString("username"));
        System.out.println("password:" + bundle.getString("password"));
        System.out.println("Sex:" + bundle.getString("sex"));
        System.out.println("age:" + bundle.getString("age"));
    }
}

通過請求頭國際化頁面

<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		// 從請求頭中獲取 Locale 資訊(語言)
		Locale locale = request.getLocale();
		System.out.println(locale);
		// 獲取讀取包(根據 指定的 baseName 和 Locale 讀取 語言資訊)
		ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
	%>
	<a href="">中文</a>|
	<a href="">english</a>
	<center>
		<h1><%=i18n.getString("regist")%></h1>
		<table>
		<form>
			<tr>
				<td><%=i18n.getString("username")%></td>
				<td><input name="username" type="text" /></td>
			</tr>
			<tr>
				<td><%=i18n.getString("password")%></td>
				<td><input type="password" /></td>
			</tr>
			<tr>
				<td><%=i18n.getString("sex")%></td>
				<td><input type="radio" /><%=i18n.getString("boy")%>
					<input type="radio" /><%=i18n.getString("girl")%></td>
			</tr>
			<tr>
				<td><%=i18n.getString("email")%></td>
				<td><input type="text" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="reset" value="<%=i18n.getString("reset")%>" />&nbsp;&nbsp;
				<input type="submit" value="<%=i18n.getString("submit")%>" /></td>
			</tr>
			</form>
		</table>
		<br /> <br /> <br /> <br />
	</center>
	國際化測試:
	<br /> 1、訪問頁面,通過瀏覽器設定,請求頭資訊確定國際化語言。
	<br /> 2、通過左上角,手動切換語言
</body>
</html>

通過顯示的選擇語言型別進行國際化

<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		// 通過顯示的選擇語言型別進行國際化
		// 從請求頭中獲取 Locale 資訊(語言)
		Locale locale = null;
		String country = request.getParameter("country");
		if ("cn".equals(country)) {
			locale = Locale.CHINA;
		} else if ("usa".equals(country)) {
			locale = Locale.US;
		} else {
			locale = request.getLocale();
		}
		System.out.println(locale);
// 獲取讀取包(根據 指定的 baseName 和 Locale 讀取 語言資訊)
		ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
	%>

	<a href="i18n.jsp?country=cn">中文</a>|
	<a href="i18n.jsp?country=usa">english</a>
	<center>
		<h1><%=i18n.getString("regist")%></h1>
		<table>
		<form>
			<tr>
				<td><%=i18n.getString("username")%></td>
				<td><input name="username" type="text" /></td>
			</tr>
			<tr>
				<td><%=i18n.getString("password")%></td>
				<td><input type="password" /></td>
			</tr>
			<tr>
				<td><%=i18n.getString("sex")%></td>
				<td><input type="radio" /><%=i18n.getString("boy")%>
					<input type="radio" /><%=i18n.getString("girl")%></td>
			</tr>
			<tr>
				<td><%=i18n.getString("email")%></td>
				<td><input type="text" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="reset" value="<%=i18n.getString("reset")%>" />&nbsp;&nbsp;
				<input type="submit" value="<%=i18n.getString("submit")%>" /></td>
			</tr>
			</form>
		</table>
		<br /> <br /> <br /> <br />
	</center>
	國際化測試:
	<br /> 1、訪問頁面,通過瀏覽器設定,請求頭資訊確定國際化語言。
	<br /> 2、通過左上角,手動切換語言
</body>
</html>

JSTL 標籤庫實現國際化

  • <%--1 使用標籤設定 Locale 資訊--%> <fmt:setLocale value="" />

  • <%--2 使用標籤設定 baseName--%> <fmt:setBundle basename=""/>

  • <%--3 輸出指定 key 的國際化資訊--%> <fmt:message key="" />

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%--1 使用標籤設定 Locale 資訊--%>
	<fmt:setLocale value="${param.locale}" />
	<%--2 使用標籤設定 baseName--%>
	<fmt:setBundle basename="i18n"/>

	<a href="i18n_fmt.jsp?locale=zh_CN">中文</a>|
	<a href="i18n_fmt.jsp?locale=en_US">english</a>
	<center>
		<h1>><fmt:message key="regist" /></h1>
		<table>
		<form>
			<tr>
				<td><fmt:message key="username" /></td>
				<td><input name="username" type="text" /></td>
			</tr>
			<tr>
				<td>><fmt:message key="password" /></td>
				<td><input type="password" /></td>
			</tr>
			<tr>
				<td><fmt:message key="sex" /></td>
				<td><input type="radio" /><fmt:message key="boy" />
					<input type="radio" /><fmt:message key="girl" /></td>
			</tr>
			<tr>
				<td><fmt:message key="email" /></td>
				<td><input type="text" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="reset" value="<fmt:message key="reset" />" />&nbsp;&nbsp;
				<input type="submit" value="<fmt:message key="submit" />" /></td>
			</tr>
			</form>
		</table>
		<br /> <br /> <br /> <br />
	</center>
</body>
</html>

練習和總結