java web--國際化 i18n

1. 什麼是國際化和本地化:html

            I. 本地化:一個軟件在某個國家或地區使用時,採用該國家或地區的語言,數字,貨幣,日期等習慣。
           II. 國際化:軟件開發時,讓它能支持多個國家和地區的本地化應用。使得應用軟件可以適應多個地區的語言和文化風俗習慣
           III. 本地敏感數據: 隨用戶區域信息而變化的數據稱爲本地信息敏感數據。例如數字,貨幣, 日期,時間等數據java

2. 相關的 API:web

            I. DateFormat 和 SimpleDateFormat √.
            II. NumberFormat
            III. MessageFormat
            IV. ResourceBundle
            V. Localesession

3. 關於國際化資源文件:app

              I. properties 文件格式
              II. 必須提供 基名.properties 文件和 基名_語言代碼_國家代碼.properties 文件
              III. 相同的 基名 的資源文件必須有相同的 key.
              IV. 可能須要使用 native2ascii 工具把非 asc 碼轉爲 asc 碼.框架

4. WEB 的國際化jsp

                 I. 可使用 request.getLocale() 獲取 Locale 對象
                II. 可使用 JSTL 的 fmt 標籤完成的國際化. 後面使用框架提供的標籤完成.
                III. 實現 "中文" "英文" 的切換:ide

                  > 提供兩個超簡潔. 攜帶不一樣的變量值
                  > 根據變量值肯定對應的 Locale 對象
                  > 把 Locale 對象放入到 session 中
                  > 綁定 Locale 對應的資源文件. 工具

                 IV. 其餘 fmt 標籤能夠參考 standard-examples.war 中的例子.ui

5.代碼區

package com.atguigu.i18n;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;

public class I18nTest {
    
    /*
    @Test
    public void testMessageFormat2(){
        String str = "Date: {0}, Salary: {1}";
        
        Locale locale = Locale.CHINA;
        Date date = new Date();
        double sal = 12345.12;
        
        StringBuffer result = new StringBuffer();
        FieldPosition fieldPosition = new FieldPosition(0);
        
        MessageFormat messageFormat = new MessageFormat(str, locale);
        messageFormat.format(date, result, fieldPosition);
        
        System.out.println(result); 
    }
    */
    
    /**
     * ResourceBundle: 資源包類.
     * 
     * 1. 在類路徑下須要有對應的資源文件: baseName.properties. 其中 baseName 是基名.
     * 2. 可使用 基名_語言代碼_國家代碼.properties 來添加不一樣國家或地區的資源文件. i18n_zh_CN.properties
     * 3. 要求全部基名相同的資源文件的 key 必須徹底一致. 
     * 4. 可使用 native2ascii 命令來獲得 漢字 對一個的 asc 碼. Eclipse 內置了工具
     * 5. 能夠調用 ResourceBundle 的 getBundle(基名, Locale 實例) 獲取獲取 ResourceBundle 對象
     * 6. 能夠調用 ResourceBundle 的 getString(key) 來獲取資源文件的 value 字符串的值. 
     * 7. 結合 DateFormat, NumberFormat, MessageFormat 便可實現國際化. 
     * 
     */
    @Test
    public void testResourceBundle(){
        Locale locale = Locale.CHINA;  
        ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale);
        //日期    工資
        System.out.println(resourceBundle.getString("date"));
        System.out.println(resourceBundle.getString("salary"));
        
        String dateLabel = resourceBundle.getString("date");
        String salLabel = resourceBundle.getString("salary");
        
        String str = "{0}:{1}, {2}:{3}";
        
        Date date = new Date();
        double sal = 12345.12;
        
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        String dateStr = dateFormat.format(date);
        
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        String salStr = numberFormat.format(sal);
        
        String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
        //日期:2018-1-18, 工資:¥12,345.12
        System.out.println(result); 
    }
    
    /**
     * MessageFormat: 能夠格式化模式字符串
     * 模式字符串: 帶佔位符的字符串: "Date: {0}, Salary: {1}"
     * 能夠經過 format 方法會模式字符串進行格式化
     */
    @Test
    public void testMessageFormat(){
        String str = "Date: {0}, Salary: {1}";
        
        Locale locale = Locale.CHINA;
        Date date = new Date();
        double sal = 12345.12;
        
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        String dateStr = dateFormat.format(date);
        
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        String salStr = numberFormat.format(sal);
        
        String result = MessageFormat.format(str, dateStr, salStr);
        //Date: 2018-1-18, Salary: ¥12,345.12     定義信息格式
        System.out.println(result); 
    }
    
    /**
     * NumberFormat: 格式化數字到數字字符串, 或貨幣字符串的工具類
     * 1. 經過工廠方法獲取 NumberFormat 對象
     * NumberFormat.getNumberInstance(locale); //僅格式化爲數字的字符串
     * NumberFormat.getCurrencyInstance(locale); //格式爲貨幣的字符串
     * 
     * 2. 經過 format 方法來進行格式化
     * 3. 經過 parse 方法把一個字符串解析爲一個 Number 類型. 
     */
    @Test
    public void testNumberFormat() throws ParseException{
        double d = 123456789.123d;
        Locale locale = Locale.FRANCE;
        NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
        String str = numberFormat.format(d);
        //123 456 789,123 
        System.out.println(str); 
        
        NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
        str = numberFormat2.format(d);
        //123 456 789,12 €
        System.out.println(str); 
        
        str = "123 456 789,123";
        d = (Double) numberFormat.parse(str);
        //1.23456789123E8
        System.out.println(d); 
        
        str = "123 456 789,12 €";
        d = (Double) numberFormat2.parse(str);
        //1.2345678912E8
        System.out.println(d);
        
    }
    
    @Test
    public void testDateFormat2() throws ParseException{
        String str = "1990-12-12 12:12:12";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //Wed Dec 12 00:12:12 CST 1990
        Date date = dateFormat.parse(str);
        System.out.println(date); 
    }
    
    /**
     * DateFormat: 格式化日期的工具類. 
     * DateFormate 自己是一個抽象類. 
     * 
     * 1. 若只但願經過 DateFormat 把一個 Date 對象轉爲一個字符串, 則能夠經過 DateFormat 的工廠方法來獲取 DateFormat 對象
     * 2. 能夠獲取只格式化 Date 的 DateFormat 對象: getDateInstance(int style, Locale aLocale) 
     * 3. 能夠獲取只格式化 Time 的 DateFormat 對象: getTimeInstance(int style, Locale aLocale) 
     * 4. 能夠獲取既格式化 Date, 也格式化 Time 的 DateFormat 對象: 
     * getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 
     * 5. 其中 style 能夠取值爲: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale 則爲表明國家地區的 Locale 對象
     * 6. 經過 DateFormat 的 format 方法來格式化個 Date 對象到字符串. 
     * 
     * 7. 如有一個字符串, 如何解析爲一個 Date 對象呢 ? 
     * I. 先建立 DateFormat 對象: 建立 DateFormat 的子類 SimpleDateFormat 對象
     * SimpleDateFormat(String pattern). 
     * 其中 pattern 爲日期, 時間的格式, 例如: yyyy-MM-dd hh:mm:ss
     * II. 調用 DateFormat 的 parse 方法來解析字符串到 Date 對象.  
     * 
     */
    @Test
    public void testDateFormat(){
        Locale locale = Locale.CHINA;
        
        Date date = new Date();
        //Thu Jan 18 14:29:48 CST 2018
        System.out.println(date); 
        
        //獲取 DateFormat 對象    2018年1月18日 14:30:29
        DateFormat dateFormat = 
                DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
        String str = dateFormat.format(date);
        System.out.println(str); 
        
    }

    /**
     * Locale: Java 中表示國家或地區的類. JDK 中提供了不少常量.
     * 也能夠經過 Locale(languageCode, countryCode) 的方式來建立 
     * 在 WEB 應用中能夠經過 request.getLocale() 方法來獲取. 
     */
    @Test
    public void testLocale(){
        Locale locale = Locale.CHINA;
        // 中國   zh
        System.out.println(locale.getDisplayCountry());
        System.out.println(locale.getLanguage()); 
        // 美國   en
        locale = new Locale("en", "US");
        System.out.println(locale.getDisplayCountry());
        System.out.println(locale.getLanguage()); 
    }
    
}
I18nTest
date=Date
salary=Salary
i18n_en_US.properties
date=\u65E5\u671F
salary=\u5DE5\u8D44
i18n_zh_CN.properties
date=Date
salary=Salary
i18n.properties
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>i18n</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml
<%@page import="java.util.Locale"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
<!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>Insert title here</title>
</head>
<body>
     
    <% 
        Date date = new Date();
        request.setAttribute("date", date);
        request.setAttribute("salary", 12345.67);
    %>
    
    <%-- 
    <fmt:bundle basename="i18n">
        <fmt:message key="date"></fmt:message>: 
        <fmt:formatDate value="${date }"/>,
        <fmt:message key="salary"></fmt:message>:
        <fmt:formatNumber value="${salary }"></fmt:formatNumber>
    </fmt:bundle>
    <br><br>
    --%>
    
    <% 
        String code = request.getParameter("code");
    
        if(code != null){
            if("en".equals(code)){
                session.setAttribute("locale", Locale.US);
            }else if("zh".equals(code)){
                session.setAttribute("locale", Locale.CHINA);
            }
            
        }
    %>
    
    <c:if test="${sessionScope.locale != null }">
        <fmt:setLocale value="${sessionScope.locale }"/>
    </c:if>
    
    <fmt:setBundle basename="i18n"/>
    
    <fmt:message key="date"></fmt:message>: 
    <fmt:formatDate value="${date }" dateStyle="FULL"/>,
    <fmt:message key="salary"></fmt:message>:
    <fmt:formatNumber value="${salary }" type="currency"></fmt:formatNumber>
    <br><br>
    
    <a href="index.jsp?code=en">English</a>
    <a href="index.jsp?code=zh">中文</a>
    
</body>
</html>
index.jsp

 

 

相關文章
相關標籤/搜索