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"> 在服务器中运行以上页面,可看到在中文系统下展现效果为: ![]() 在英文系统下展现效果为: ![]() |
||
| [上一页] [下一页] | ||
JSF常规国际化支持
<f:loadBundle basename="demo.LocalStrings" var="msgs"/>
<w:page title="Calculator">
<w:form id="calc">
<layout:panelGrid columns="3">
<h:outputLabel for="first" value="
#{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>
