JSF - 国际化
国际化是一种状态消息、GUI 组件标签、货币、日期不在程序中进行硬编码的技术。相反,它们存储在源代码外部的资源包中并动态检索。JSF 提供了一种非常方便的方法来处理资源捆绑。
内部化 JSF 应用程序需要执行以下步骤。
第 1 步:定义属性文件
为每个区域设置创建属性文件。名称应采用 <文件名>_<区域设置>.properties 格式。
文件名中可以省略默认区域设置。
消息.属性
greeting = Hello World!
messages_fr.properties
greeting = Bonjour tout le monde!
第 2 步:更新 faces-config.xml
面孔配置.xml
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.tutorialspoint.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
步骤 3:使用资源包 var
首页.xhtml
<h:outputText value = "#{msg['greeting']}" />
应用示例
让我们创建一个测试 JSF 应用程序来测试 JSF 中的国际化。
| 步 | 描述 |
|---|---|
| 1 | 按照JSF - 第一个应用程序章节中的说明,在com.tutorialspoint.test包下创建一个名为helloworld的项目。 |
| 2 | 在src → mai文件夹下创建resources文件夹。 |
| 3 | 在src→main→resources文件夹下创建com文件夹。 |
| 4 | 在src→main→resources→com文件夹下创建tutorialspoint文件夹。 |
| 5 | 在src→main→resources→com→tutorialspoint文件夹下创建messages.properties文件。按如下所述进行修改。 |
| 6 | 在src→main→resources→com→tutorialspoint文件夹下创建messages_fr.properties文件。按如下所述进行修改。 |
| 7 | 在较旧的WEB-INFf中创建faces-config.xml,如下所述。 |
| 8 | 在com.tutorialspoint.test包下创建UserData.java,如下所述。 |
| 9 | 按如下所述修改home.xhtml 。其余文件保持不变。 |
| 10 | 编译并运行应用程序以确保业务逻辑按照要求运行。 |
| 11 | 最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Web服务器中。 |
| 12 | 使用适当的 URL 启动您的 Web 应用程序,如下面最后一步中所述。 |
消息.属性
greeting = Hello World!
messages_fr.properties
greeting = Bonjour tout le monde!
面孔配置.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<faces-config
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version = "2.0">
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.tutorialspoint.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
用户数据.java
package com.tutorialspoint.test;
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 = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String locale;
private static Map<String,Object> countries;
static {
countries = new LinkedHashMap<String,Object>();
countries.put("English", Locale.ENGLISH);
countries.put("French", Locale.FRENCH);
}
public Map<String, Object> getCountries() {
return countries;
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
//value change event listener
public void localeChanged(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());
}
}
}
}
首页.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">
<h:head>
<title>JSF tutorial</title>
</h:head>
<h:body>
<h2>Internalization Language Example</h2>
<h:form>
<h3><h:outputText value = "#{msg['greeting']}" /></h3>
<h:panelGrid columns = "2">
Language :
<h:selectOneMenu value = "#{userData.locale}" onchange = "submit()"
valueChangeListener = "#{userData.localeChanged}">
<f:selectItems value = "#{userData.countries}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
</h:body>
</html>
准备好完成所有更改后,让我们像在 JSF - 第一个应用程序章节中所做的那样编译并运行该应用程序。如果您的应用程序一切正常,这将产生以下结果。
从下拉菜单中更改语言。您将看到以下输出。
