JSF - 國際化



國際化是一種技術,其中狀態訊息、GUI元件標籤、貨幣、日期等不會硬編碼在程式中。相反,它們儲存在原始碼之外的資源包中,並動態檢索。JSF 提供了一種非常方便的方式來處理資源包。

國際化 JSF 應用程式需要以下步驟。

步驟 1:定義屬性檔案

為每個區域設定建立屬性檔案。名稱應為 <檔名>_<區域設定>.properties 格式。

預設區域設定可以在檔名中省略。

messages.properties

greeting = Hello World!

messages_fr.properties

greeting = Bonjour tout le monde!

步驟 2:更新 faces-config.xml

faces-config.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:使用資源包變數

home.xhtml

<h:outputText value = "#{msg['greeting']}" />

示例應用程式

讓我們建立一個測試 JSF 應用程式來測試 JSF 中的國際化。

步驟 描述
1 建立一個名為 *helloworld* 的專案,放在 *com.tutorialspoint.test* 包下,如 *JSF - 第一個應用程式* 章節中所述。
2 在 *src → main* 資料夾下建立 *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-INF* 資料夾中建立 *faces-config.xml*,如下所述。
8 在 *com.tutorialspoint.test* 包下建立 *UserData.java*,如下所述。
9 按如下所述修改 *home.xhtml*。保持其餘檔案不變。
10 編譯並執行應用程式以確保業務邏輯按要求工作。
11 最後,將應用程式構建為 war 檔案,並將其部署到 Apache Tomcat Web 伺服器。
12 使用最後一步中說明的相應 URL 啟動您的 Web 應用程式。

messages.properties

greeting = Hello World!

messages_fr.properties

greeting = Bonjour tout le monde!

faces-config.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>

UserData.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());         
         }
      }
   }
}

home.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 - 第一個應用程式章節中一樣編譯並執行應用程式。如果您的應用程式一切正常,這將產生以下結果。

JSF Internationalization Result

從下拉列表中更改語言。您將看到以下輸出。

JSF Internationalization Result1
廣告
© . All rights reserved.