JSF 国际化示例
2018-02-22 13:54 更新
JSF教程 - JSF国际化示例
以下部分显示如何在JSF中使用国际化。
国际化是一种我们可以用来显示状态消息,GUI组件标签,货币,日期用不同语言的技术。
显示的文本不是在程序中硬编码,而是存储在资源束中的源代码之外并动态加载。
使用国际化的步骤
首先,我们需要创建一个包含消息的属性文件。为每个区域设置创建属性文件。通常一个语言环境用于一种语言。
属性文件的名称应为<file-name> _ <locale> .properties格式。
默认区域设置可以在文件名中省略。
以下是来自messages.properties文件。
greeting=Hello World!
以下是来自messages_fr.properties文件。 fr是法语。
greeting=Bonjour tout le monde!
然后,更新faces-config.xml
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.w3cschool.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
最后,我们可以使用resource-bundle var。
<h:outputText value="#{msg["greeting"]}" />
例子
以下代码来自welcome_zh_CN.properties。
welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0
以下代码来自demo.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:body>
<h:form>
<h3>
<h:outputText value="#{msg["welcome.jsf"]}" />
</h3>
<h:panelGrid columns="2">
Language :
<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
valueChangeListener="#{language.countryLocaleCodeChanged}">
<f:selectItems value="#{language.countriesInMap}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
</h:body>
</html>
下面的代码来自UserBean.java。
package cn.w3cschool.common;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name="language")
@SessionScoped
public class UserBean implements Serializable{
private static final long serialVersionUID = 1L;
private String localeCode;
private static Map<String,Object> countries;
static{
countries = new LinkedHashMap<String,Object>();
countries.put("English", Locale.ENGLISH); //label, value
countries.put("Chinese", Locale.SIMPLIFIED_CHINESE);
}
public Map<String, Object> getCountriesInMap() {
return countries;
}
public String getLocaleCode() {
return localeCode;
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}
public void countryLocaleCodeChanged(ValueChangeEvent e){
String newLocaleValue = e.getNewValue().toString();
for (Map.Entry<String, Object> entry : countries.entrySet()) {
if(entry.getValue().toString().equals(newLocaleValue)){
FacesContext.getCurrentInstance()
.getViewRoot().setLocale((Locale)entry.getValue());
}
}
}
}
以下代码来自welcome.properties。
welcome.jsf = Happy learning JSF 2.0下载 Internationalization.zip
运行
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
以上内容是否对您有帮助:

免费 AI IDE


更多建议: