JavaWeb-09-Java的國際化---i18n

 

1:什麼是i18n

  • 國際化(Internationalization)指的是同一個網站可以支持多種不同的語言,以方便不同國家,不同語種的用戶訪問。
  • 關於國際化我們想到的最簡單的方案就是爲不同的國家創建不同的網站,比如蘋果公司,他的英文官網是:http://www.apple.com 而中國官網是 http://www.apple.com/cn
  • 蘋果公司這種方案並不適合全部公司,而我們希望相同的一個網站,而不同人訪問的時候可以根據用戶所在的區域顯示不同的語言文字,而網站的佈局樣式等不發生改變。
  •  於是就有了我們說的國際化,國際化總的來說就是同一個網站不同國家的人來訪問可以顯示出不同的語言。但實際上這種需求並不強烈,一般真的有國際化需求的公司,主流採用的依然是蘋果公司的那種方案,爲不同的國家創建不同的頁面。所以國際化的內容我們瞭解一下即可。
  •  國際化的英文 Internationalization,但是由於拼寫過長,老外想了一個簡單的寫法叫做 I18N,代表的是 Internationalization這個單詞,以 I 開頭,以 N 結尾,而中間是 18 個字母,所以簡寫爲 I18N。以後我們說 I18N 和國際化是一個意思。

2:國際化相關要素介紹 

3: 國際化資源 properties 測試

3.1:測試Locale得到國家語言代碼

package com.wkl.filter.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Description:
 * Date:       2020/6/19 - 下午 2:18
 * author:     wangkanglu
 * version:    V1.0
 */
public class I18nTest {

    public static void main(String[] args) {
        Locale aDefault = Locale.getDefault();
        System.out.println(""+aDefault);//默認的語言國家代碼:zh_CN

        Locale china = Locale.CHINA;
        System.out.println(""+china);//中文:zh_CN

        Locale us = Locale.US;
        System.out.println(""+us);//英文:en_US

    }

/**
     * 獲取所有的國家語言代碼
     */
    public static void getAllLocale(){
        for (Locale availableLocale : Locale.getAvailableLocales()) {
            System.out.println(""+availableLocale);
        }
    }

}

3.2:創建i18n的properties文件

3.3:在代碼中獲取國際化參數

 public static void i18nTest(){
        //設置特定國家語言的locale
        Locale us = Locale.US;
        //利用文件名和locale讀取配置文件,得到bundle,baseName寫項目的相對路徑
        ResourceBundle bundle = ResourceBundle.getBundle("i18n", us);
        System.out.println(""+bundle.getString("username"));
        System.out.println(""+bundle.getString("password"));
    }

3.4:從請求頭中獲取瀏覽器語言

// 從請求頭中獲取 Locale 信息(語言)
Locale locale = request.getLocale();
System.out.println(locale);
// 獲取讀取包(根據 指定的 baseName 和 Locale 讀取 語言信息)
ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);

3.5:通過頁面鏈接方式進行國際化

// 從請求頭中獲取 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);

3.6:通過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>

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章