OperaMasks 2.0的神奇魔力之二:国际化
#demo.LocalStrings_en_US.properties
first.label=First:
second.label=Second:
result.label=Result:
add.label= +
subtract.label= -
multiply.label= *
divide.label = /
#demo.LocalStrings_zh_CN.properties
first.label=数值一:
second.label=数值二:
result.label=结果:
add.label= +
subtract.label= -
multiply.label= *
divide.label = /
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">
<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" value="#{msgs['second.label']}"/>
<h:outputLabel for="second"/>
<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>
<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"/>
<w:textField id="first"/>
<h:message for="first"/>
<h:outputLabel for="second"/>
<w:textField id="second"/>
<h:message for="second"/>
<h:outputLabel for="result"/>
<h:outputText id="result"/>
</layout:panelGrid>
<br/>
<layout:panelGrid columns="4">
<w:button id="add"/>
<w:button id="subtract"/>
<w:button id="multiply"/>
<w:button id="divide"/>
</layout:panelGrid>
</w:form>
</w:page>
</f:view>
#demo.LocalStrings_en_US.properties
CalcBean.first.label=First:
CalcBean.second.label=Second:
CalcBean.result.label=Result:
CalcBean.add.label= +
CalcBean.subtract.label= -
CalcBean.multiply.label= *
CalcBean.divide.label = /
#demo.LocalStrings_zh_CN.properties
CalcBean.first.label=数值一:
CalcBean.second.label=数值二:
CalcBean.result.label=结果:
CalcBean.add.label= +
CalcBean.subtract.label= -
CalcBean.multiply.label= *
CalcBean.divide.label = /
#demo.LocalStrings.properties
CalcBean.first.description=Please input the first number
CalcBean.add.description=Add these numbers
#demo.LocalStrings_zh_CN.properties
CalcBean.first.description=请输入第一个数值
CalcBean.add.description=将数值相加
#demo.LocalStrings_zh_CN.properties
CalcBean.add.minWidth=30
CalcBean.subtract.minWidth=30
CalcBean.multiply.minWidth=30
CalcBean.divide.minWidth=30
#demo.LocalStrings_en_US.properties
CalcBean.add.minWidth=60
CalcBean.subtract.minWidth=60
CalcBean.multiply.minWidth=60
CalcBean.divide.minWidth=60
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" />
<w:textField id="first" />
<h:message for="first" />
<h:outputLabel for="second" />
<w:textField id="second" />
<h:message for="second" />
<layout:cell colspan="3" rowspan="1">
<h:outputText id="resultLabel" />
</layout:cell>
</layout:panelGrid>
<br />
<layout:panelGrid columns="4">
<w:button id="add" />
<w:button id="subtract" />
<w:button id="multiply" />
<w:button id="divide" />
</layout:panelGrid>
</w:form>
</w:page>
</f:view>
@ManagedBean(scope = ManagedBeanScope.REQUEST)
public class CalcBean {
@Bind
private double first = 22.0;
@Bind
private double second = 7.0;
@Bind
private String resultLabel;
/**
* 注入资源文件
*/
@LocalString
private Map<String,String> messages;
/**
* 用来保存计算结果
*/
private double result = 0;
/**
* 用来保存用户的操作符
*/
private String operator = "";
@Action
public void add() {
result = first + second;
operator = "+";
}
@Action
public void subtract() {
result = first - second;
operator = "-";
}
@Action
public void multiply() {
result = first * second;
operator = "*";
}
@Action
public void divide() {
result = first / second;
operator = "/";
}
@BeforeRender
private void beforeRender(boolean isPostBack) {
resultLabel = MessageFormat.format(messages.get("resultLabel"), first, operator, second, result);
}
}
#demo.LocalStrings_en_US.properties
CalcBean.resultLabel=Number {0} {1} number {2} equals {3}
#demo.LocalStrings_zh_CN.properties
CalcBean.resultLabel=数值 {0} {1} 数值 {2} 等于 {3}