java--I18N

Java.util.ResourceBundle類,用於管理和Locale相關的資源的功能。
ResourceBundle類提供了兩個方法,用於創建ResourceBundle對像的靜態工廠方法:
        getBundle(String baseName)—通過此方法獲取資源文件的名稱
        getBundle(String baseName,Locale locale);
參數中的baseName是資源文件的名稱,資源文件通常以properties爲擴展名。
資源文件的命名規則如下:
        默認資源文件:resources.properties
        英文資源文件:resources_en_US.properties
        中文資源文件:resources_zh_CN.properties
ResourceBundle示例:
步驟如下:
1、在classpath目錄下建立一個資源文件爲message.properties。
這裏寫圖片描述
2、內容如下:name=Jack
3、書寫一個main方法的類,並輸入以下代碼:
4、ResourceBundle b = ResourceBundle.getBundle(“message”);
String name = b.getString(“name”);
System.err.println(name);
此時你看到的輸出是Jack.
5、在classpath目錄下,再建立一個文件message_zh_CN.properties
並輸入以下內容:name=傑克 (注意轉碼)
這裏寫圖片描述
6、重新執行第4步的代碼,你將會看到輸出的結果變成了:傑克。
7、這是因爲它使用本地語言查找相關的資源文件,如果找不到就使用默認的,如果找到就使用本地的資源文件。
這裏寫圖片描述

MessageFormat類:
MessageFormat類,也是重要的國際化信息的類。它與ResourceBundle的區別是:
        ResourceBundle類只可以使用靜態文本,即在編譯時已經確定的文本。
        而MessageFormat可以使用動態文本。如String str = MessageFormat.format(“Hello {0}”,”Jack”);
        System.err.println(str);
MessageFormat類,不能讀取資源文件,它只能藉助於ResourceBundle已經讀取的信息再進行格式化輸出。
示例:資源文件中的信息爲greeting=Hello {0}
        ResourceBundle b = ResourceBundle.getBundle(“message”);
        String greeting = b.getString(“greeting”);
        greeting = MessageFormat.format(greeting,”北京”);
        System.err.println(greeting);
JSTL的國際化標籤:
< fmt:setLocale/> - 設置Locale關將它保存在某個範圍內,默認爲page範圍。
        如:< fmt:setLocale value=“zh_CN” scope=“session”/>它的作用相當於:
        Locale locale = new Locale(“zh”,”CN”);
        Session.setAttribute(“javax.servlet.jsp.jstl.fmt.locale.session”,locale);
< fmt:setBundle/>
       用於設置要使用的資源文件。如:
       < fmt:setBundle basename=“message” var=“變量名” scope=“session”/>
< fmt:message/>
       讀取資源文件中key所對應的值。
設置支持國際化的網頁1:
第一步:建立三個資源文件爲:
msg.properties
msg_zh_CN.properties
msg_en_US.properties
第二步:使用jstl標籤。
< fmt:setLocale value=“${param.locale}” scope=“session”/>
< fmt:setBundle basename=“message” scope=“session”/>

<%@ page language="java" import="java.util.*,cn.hncu.domain.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!-- prefix前綴,相當於xml的命名空間,名字可以隨便取,一般用c -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>JSTL用法演示</title>
  </head>

  <body>
    <span>
        內容格式:<br/>
        歡迎你,xxx<br/>
        現在時間是:2016-10-18 10:27<br/>
    </span><br/>
    <font color='red' size='6'>I18N--國際化技術演示1</font><br/>
    <font style="font-weight: bold;">1,設置本地信息(哪個國家的什麼語言)</font><br/>
    <!-- 國語
        <fmt:setLocale value="zh-CN"/>
     -->
    <!-- 美式英語
        <fmt:setLocale value="en-US"/>
     -->
    <!-- 默認語言
     -->
    <fmt:setLocale value="e"/>

    <font style="font-weight: bold;">2,指定資源文件名</font><br/>
    <fmt:setBundle basename="msg" scope="session"/>

    <font style="font-weight: bold;">3,顯示信息</font><br/>
    <fmt:message key="welcom"></fmt:message>,xxx<br/>
    <fmt:message key="time"></fmt:message>:2016-10-18 10:27
  </body>
</html>

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述
設置支持國際化的網頁2:
多文件(src下的resource資源文件夾中)
這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述
第三步:切換。
< a href=”index.jsp?locale=zh_CN”>中文< /a>
< a href=”index.jsp?locale=en_US”>English< /a>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!-- prefix前綴,相當於xml的命名空間,名字可以隨便取,一般用fmt -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>JSTL用法演示</title>
  </head>

  <body>
    <span>
        內容格式:<br/>
        歡迎你,xxx<br/>
        現在時間是:2016-10-18 10:27<br/>
    </span><br/>
    <font color='red' size='6'>I18N--國際化技術演示2</font><br/>
    <a href="?bd=no_NO">默認版</a>&nbsp;&nbsp;
    <a href="?bd=zh_CN">國語版</a>&nbsp;&nbsp;
    <a href="?bd=en_US">美式英語版</a><br/>
    <font style="font-weight: bold;">1,設置本地信息(哪個國家的什麼語言)</font><br/>
    <fmt:setLocale value="${param.bd}"/>

    <font style="font-weight: bold;">2,指定資源文件名</font><br/>
    <fmt:setBundle basename="msg" scope="session"/>
    <!--指定第2個及之後的資源文件,含路徑的資源名,第2個及之後的資源一定要取別名  -->
    <fmt:setBundle basename="cn.resources.a" scope="session" var="aa"/>

    <font style="font-weight: bold;">3,顯示信息</font><br/>
    <fmt:message key="welcom"></fmt:message>,xxx<br/>
    <fmt:message key="time"></fmt:message>:2016-10-18 10:27<br/>
    <!-- 第2個及之後的資源,要指定別名 -->
    <fmt:message key="address" bundle="${aa}"></fmt:message><br/>
    <br/><br/>
    <a href="a.jsp">轉到另一個頁面</a>
    <!-- 把數據存儲在session中,所以另一個頁面也會和本頁面一樣的語言風格 -->
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!-- prefix前綴,相當於xml的命名空間,名字可以隨便取,一般用fmt -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>顯示用戶信息</title>
  </head>

  <body>

    <fmt:message key="welcom"></fmt:message>,xxx<br/>
    <fmt:message key="time"></fmt:message>:2016.10.16 17:33 <br/>


    <fmt:message key="address" bundle="${aa}"/><br/>
  </body>
</html>

這裏寫圖片描述

這裏寫圖片描述
這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

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