i18n 國際化(了解內容)
一、什么是 i18n 國際化?
二、國際化相關要素介紹
三、國際化資源 properties 測驗
配置兩個語言的組態檔: i18n_en_US.properties 英文:username=username password=password sex=sex age=agei18n_zh_CN.properties 中文:
username=用戶名 password=密碼 sex=性別 age=年齡國際化測驗代碼:
package com.zixue.i18n; import org.junit.Test; import java.util.Locale; import java.util.ResourceBundle; /** * @author Mr Guo * @create 2020-11-18 13:50 */ public class I18nTest { @Test public void testLocale(){ //獲取系統默認的語言國家資訊 // Locale locale = Locale.getDefault(); // System.out.println(locale);//zh_CN //獲取所有國家的語言資訊 // for (Locale availableLocale : Locale.getAvailableLocales()){ // System.out.println(availableLocale); // } //獲取中文,中文的常量的Locale物件 System.out.println(Locale.CHINA);//zh_CN //獲取英文,英文的常量的Locale物件 System.out.println(Locale.US);//en_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")%>" /> <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(); } //獲取資源包(根據指定的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")%>" /> <input type="submit" value="<%=i18n.getString("submit")%>" /></td> </tr> </form> </table> <br /> <br /> <br /> <br /> </center> 國際化測驗: <br /> 1、訪問頁面,通過瀏覽器設定,請求頭資訊確定國際化語言, <br /> 2、通過左上角,手動切換語言 </body> </html>

六、JSTL 標簽庫實作國際化
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ 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> <fmt:setLocale value="${param.locale}" /> <fmt:setBundle basename="i18n" /> <a href="i18n.jsp?locale=zh_CN">中文</a>| <a href="i18n.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" />" /> <input type="submit" value="<fmt:message key="submit" />" /></td> </tr> </form> </table> <br /> <br /> <br /> <br /> </center> 國際化測驗: <br /> 1、訪問頁面,通過瀏覽器設定,請求頭資訊確定國際化語言, <br /> 2、通過左上角,手動切換語言 </body> </html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/224424.html
標籤:Java
上一篇:網路編程之 TCP 實作簡單聊天
下一篇:感謝您的支持
