天天看點

[轉]JSF的國際化

Jessiec: 經過試驗,頁面會根據faces-config.xml中的設定去讀取相應的資源檔案,如果沒有則預設

  <application>

        <locale-config>

                <default-locale>ck</default-locale>

        </locale-config>

  </application> 

比如,現有資源檔案

ApplicationMessages.properties

ApplicationMessages_en.properties

ApplicationMessages_zk.properties

則找不對應ck對應的資源檔案,則用預設的ApplicationMessages.properties。

再者,也可以在頁面中設定,如<f:view locale="ck">

JSF的國際化(Internnationalization)訊息處理是基于Java對國際化的支援,您可以在一個訊息資源檔案中統一管理訊息資源,資源檔案的名稱是.properties,而内容是名稱與值的配對,例如:

messages.properties

titleText=JSF Demo

 hintText=Please input your name and password

 nameText=name

 passText=password

 commandText=Submit

  資源檔案名稱由basename加上語言與地區來組成,例如:

* basename.properties

    * basename_en.properties

    * basename_zh_TW.properties

  沒有指定語言與地區的basename是預設的資源檔名稱,JSF會根據浏覽器送來的Accept-Language header中的内容來決定該使用哪一個資源檔名稱,例如:

Accept-Language: zh_TW, en-US, en

  如果浏覽器送來這些header,則預設會使用繁體中文,接着是美式英文,再來是英文語系,如果找不到對應的訊息資源檔案,則會使用預設的訊息資源檔案。

  由于訊息資源檔案必須是ISO-8859-1編碼,是以對于非西方語系的處理,必須先将之轉換為Java Unicode Escape格式,例如您可以先在訊息資源檔案中寫下以下的内容:

messages_zh_TW.txt

titleText=JSF示範

 hintText=請輸入名稱與密碼

 nameText=名稱

 passText=密碼

 commandText=送出

  然後使用JDK的工具程式native2ascii來轉換,例如:

native2ascii -encoding Big5 messages_zh_TW.txt messages_zh_TW.properties

  轉換後的内容會如下:

messages_zh_TW.properties

titleText=JSF/u793a/u7bc4

 hintText=/u8acb/u8f38/u5165/u540d/u7a31/u8207/u5bc6/u78bc

 nameText=/u540d/u7a31

 passText=/u5bc6/u78bc

 commandText=/u9001/u51fa

  接下來您可以使用<f:loadBundle>卷标來指定加載訊息資源,一個例子如下:

index.jsp

index.jsp

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

 <%@page contentType="text/html;charset=UTF8"%>

 <f:view>

 <f:loadBundle basename="messages" var="msgs"/>

 <html>

 <head>

 <title><h:outputText value="#{msgs.titleText}"/></title>

 </head>

 <body>

    <h:form>

        <h3><h:outputText value="#{msgs.hintText}"/></h3>

        <h:outputText value="#{msgs.nameText}"/>:

                <h:inputText value="#{user.name}"/><p>

        <h:outputText value="#{msgs.passText}"/>:

                <h:inputSecret value="#{user.password}"/><p>

        <h:commandButton value="#{msgs.commandText}"

                        actionListener="#{user.verify}"

                        action="#{user.outcome}"/>

   </h:form>

 </body>

 </html>

 </f:view>

  如此一來,如果您的浏覽器預設接受zh_TW語系的話,則頁面上就可以顯示中文,否則預設将以英文顯示,也就是messages.properties的内容,為了能顯示多國語系,我們設定網頁編碼為UTF8。

  <f:view>可以設定locale屬性,直接指定所要使用的語系,例如:

<f:view locale="zh_TW">

 <f:loadBundle basename="messages" var="msgs"/>

  直接指定以上的話,則會使用繁體中文來顯示,JSF會根據<f:loadBundle>的basename屬性加上<f:view>的locale屬性來決定要使用哪一個訊息資源檔案,就上例而言,就是使用 messages_zh_TW.properties,如果設定為以下的話,就會使用messages_en.properties:

<f:view locale="en">

 <f:loadBundle basename="messages" var="msgs"/>

  您也可以在faces-config.xml中設定語系,例如:

<faces-config>

    <application>

        <local-config>

            <default-locale>en</default-locale>

            <supported-locale>zh_TW</supported-locale>

        </local-config>

    </application>

 .....

 </faces-config>

  在<local-config>一定有一個<default-locale>,而<supported-locale>可以有好幾個,這告訴JSF您的應用程式支援哪些語系。

  當然,如果您可以提供一個選項讓使用者選擇自己的語系會是更好的方式,例如根據user這個Bean的locale屬性來決定頁面語系:

<f:view locale="#{user.locale}">

 <f:loadBundle basename="messages" var="msgs"/>

  在頁面中設定一個窗體,可以讓使用者選擇語系,例如設定單選鈕:

<h:selectOneRadio value="#{user.locale}">

     <f:selectItem itemValue="zh_TW"

                   itemLabel="#{msgs.zh_TWText}"/>

     <f:selectItem itemValue="en"

                   itemLabel="#{msgs.enText}"/>

 </h:selectOneRadio>