JSF的國(guó)際化(Internnationalization)訊息處理是基於Java對(duì)國(guó)際化的支援,您可以在一個(gè)訊息資源檔中統(tǒng)一管理訊息資源,資源檔的名稱是.properties,而內(nèi)容是名稱與值的配對(duì),例如:
titleText=JSF Demo
hintText=Please input your name and password
nameText=name
passText=password
commandText=Submit
資源檔名稱由basename加上語(yǔ)言與地區(qū)來(lái)組成,例如:
- basename.properties
- basename_en.properties
- basename_zh_TW.properties
沒(méi)有指定語(yǔ)言與地區(qū)的basename是預(yù)設(shè)的資源檔名稱,JSF會(huì)根據(jù)瀏覽器送來(lái)的Accept-Language header中的內(nèi)容來(lái)決定該使用哪一個(gè)資源檔名稱,例如:
Accept-Language: zh_TW, en-US, en
如果瀏覽器送來(lái)這些header,則預(yù)設(shè)會(huì)使用繁體中文,接著是美式英文,再來(lái)是英文語(yǔ)系,如果找不到對(duì)應(yīng)的訊息資源檔,則會(huì)使用預(yù)設(shè)的訊息資源檔。
由於訊息資源檔必須是ISO-8859-1編碼,所以對(duì)於非西方語(yǔ)系的處理,必須先將之轉(zhuǎn)換為Java Unicode Escape格式,例如您可以先在訊息資源檔中寫下以下的內(nèi)容:
titleText=JSF示範(fàn)
hintText=請(qǐng)輸入名稱與密碼
nameText=名稱
passText=密碼
commandText=送出
然後使用JDK的工具程式native2ascii來(lái)轉(zhuǎn)換,例如:
native2ascii -encoding Big5 messages_zh_TW.txt messages_zh_TW.properties
轉(zhuǎn)換後的內(nèi)容會(huì)如下:
- 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
接下來(lái)您可以使用<f:loadBundle>標(biāo)籤來(lái)指定載入訊息資源,一個(gè)例子如下:
<%@ 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=UTF-8"%>
<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>
如此一來(lái),如果您的瀏覽器預(yù)設(shè)接受zh_TW語(yǔ)系的話,則頁(yè)面上就可以顯示中文,否則預(yù)設(shè)將以英文顯示,也就是messages.properties的內(nèi)容,為了能顯示多國(guó)語(yǔ)系,我們?cè)O(shè)定網(wǎng)頁(yè)編碼為UTF8。
<f:view>可以設(shè)定locale屬性,直接指定所要使用的語(yǔ)系,例如:
<f:view locale="zh_TW">
<f:loadBundle basename="messages" var="msgs"/>
直接指定以上的話,則會(huì)使用繁體中文來(lái)顯示,JSF會(huì)根據(jù)<f:loadBundle>的basename屬性加上<f:view>的locale屬性來(lái)決定要使用哪一個(gè)訊息資源檔,就上例而言,就是使用 messages_zh_TW.properties,如果設(shè)定為以下的話,就會(huì)使用messages_en.properties:
<f:view locale="en">
<f:loadBundle basename="messages" var="msgs"/>
您也可以在faces-config.xml中設(shè)定語(yǔ)系,例如:
<faces-config>
<application>
<local-config>
<default-locale>en</default-locale>
<supported-locale>zh_TW</supported-locale>
</local-config>
</application>
.....
</faces-config>
在<local-config>一定有一個(gè)<default-locale>,而<supported- locale>可以有好幾個(gè),這告訴JSF您的應(yīng)用程式支援哪些語(yǔ)系。
當(dāng)然,如果您可以提供一個(gè)選項(xiàng)讓使用者選擇自己的語(yǔ)系會(huì)是更好的方式,例如根據(jù)user這個(gè)Bean的locale屬性來(lái)決定頁(yè)面語(yǔ)系:
<f:view locale="#{user.locale}">
<f:loadBundle basename="messages" var="msgs"/>
在頁(yè)面中設(shè)定一個(gè)表單,可以讓使用者選擇語(yǔ)系,例如設(shè)定單選鈕:
<h:selectOneRadio value="#{user.locale}">
<f:selectItem itemValue="zh_TW"
itemLabel="#{msgs.zh_TWText}"/>
<f:selectItem itemValue="en"
itemLabel="#{msgs.enText}"/>
</h:selectOneRadio>