JSF常规国际化支持

首先来看看在标准的JSF中,是如何为应用程序提供多语言支持的。

假设我们现在有以下页面:

<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
  xmlns:w="http://www.apusic.com/jsf/widget" xmlns:layout="http://www.apusic.com/jsf/layout"
  renderKitId="AJAX" xmlns:h="http://java.sun.com/jsf/html">
  <w:page title="Calculator">
    <w:form id="calc">
      <layout:panelGrid columns="3">
        <h:outputLabel for="first" value="数值一"/>
        <w:textField id="first"/>
        <h:message for="first"/>
        <h:outputLabel for="second" value="数值二"/>
        <w:textField id="second"/>
        <h:message for="second"/>
        <h:outputLabel for="result" value="结果"/>
        <h:outputText id="result"/>
      </layout:panelGrid>
      <br/>
      <layout:panelGrid columns="4">
        <w:button id="add" value="+"/>
        <w:button id="subtract" value="-"/>
        <w:button id="multiply" value="*"/>
        <w:button id="divide" value="/"/>
      </layout:panelGrid>
    </w:form>
  </w:page>
</f:view>

为了提供多语言支持,需要在应用的/WEB-INF/classes目录下(或/WEB-INF/lib目录下的jar包中),提供以下LocalString文件:

/WEB-INF/classes/demo/LocalStrings_en_US.properties :

#demo.LocalStrings_en_US.properties
first.label=First:
second.label=Second:
result.label=Result:
add.label= +
subtract.label= -
multiply.label= *
divide.label = /

/WEB-INF/classes/demo/LocalStrings_zh_CN.properties :

#demo.LocalStrings_zh_CN.properties
first.label=数值一
second.label=数值二
result.label=结果
add.label= 加
subtract.label= 减
multiply.label= 乘
divide.label = 除

其中,LocalStrings_zh_CN.properties需要使用native2ascii工具转换为ascii编码。如果是使用Apusic Studio进行设计,也可以直接使用它的properties文件编辑器来编写多语言资源文件,它将会在保存时自动进行转换。

为了使用上述资源文件,我们的页面需要改成:

<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
  xmlns:w="http://www.apusic.com/jsf/widget" xmlns:layout="http://www.apusic.com/jsf/layout"
  renderKitId="AJAX" xmlns:h="http://java.sun.com/jsf/html">
  1<f:loadBundle basename="demo.LocalStrings" var="msgs"/>
  <w:page title="Calculator">
    <w:form id="calc">
      <layout:panelGrid columns="3">
        <h:outputLabel for="first" value="2#{msgs['first.label']}"/>
        <w:textField id="first"/>
        <h:message for="first"/>
        <h:outputLabel for="second" value="#{msgs['second.label']}"/>
        <w:textField id="second"/>
        <h:message for="second"/>
        <h:outputLabel for="result" value="#{msgs['result.label']}"/>
        <h:outputText id="result"/>
      </layout:panelGrid>
      <br/>
      <layout:panelGrid columns="4">
        <w:button id="add" value="#{msgs['add.label']}"/>
        <w:button id="subtract" value="#{msgs['subtract.label']}"/>
        <w:button id="multiply" value="#{msgs['multiply.label']}"/>
        <w:button id="divide" value="#{msgs['divide.label']}"/>
      </layout:panelGrid>
    </w:form>
  </w:page>
</f:view>
1 引入多语言资源文件。按照JSF1.2规范,basename属性中指定的资源应是以/WEB-INF/classes(或/WEB-INF/lib)为根的Java包形式表述。例子中的demo.LocalStrings对应文件/WEB-INF/classes/demo/LocalStrings.properties文件。var属性指定了在页面中引用此资源的标识符。
2 使用统一EL表达式引用资源中的键值。msgs可看作与资源文件对应的一个Map对象。根据统一EL表达式的语法,可以使用以下三种形式引用:
  1. #{msg.get('资源键值')};

  2. #{msg['资源键值']};

  3. #{msg.资源键值} (本例由于键值中带有小数点分隔符,因此不能使用此形式)

在服务器中运行以上页面,可看到在中文系统下展现效果为:

在英文系统下展现效果为:

[上一页] [下一页]