大可制作:QQ群:31564239(asp|jsp|php|mysql)

JSF Gossip: 国际化讯息

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
<%@ 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>