---
title: "jsf-國際化訊息 "
author: "zhouxing"
date: "2008-05-22T12:39:00Z"
tags: []
slug: "jsf"
draft: false
lastmod: "2008-05-22T12:39:00Z"
id: 894221
url: "https://jwebee.iteye.com/blog/894221"
---

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

```

 

接下來您可以使用&lt;f:loadBundle&gt;標籤來指定載入訊息資源，一個例子如下：

- 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=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>

```

 

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

 

&lt;f:view&gt;可以設定locale屬性，直接指定所要使用的語系，例如：

```
 

<f:view locale="zh_TW">

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

```

 

直接指定以上的話，則會使用繁體中文來顯示，JSF會根據&lt;f:loadBundle&gt;的basename屬性加上&lt;f:view&gt;的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>

```

 

在&lt;local-config&gt;一定有一個&lt;default-locale&gt;，而&lt;supported- locale&gt;可以有好幾個，這告訴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>

```