JSF Tutorial - JSF Internationalization Example








The following sections show how to use Internationalization in JSF.

Internationalization is a technique we can use to display status messages, GUI component labels, currency, date in different language.

Text being displayed are not hardcoded in the program instead they are stored outside the source code in resource bundles and loaded dynamically.

Steps to use Internationalization

First, we need to create a properties file which contains the messages. Create properties file for each locale. Usually one locale is for one language.

Name of the properties file should be in <file-name>_<locale>.properties format.

Default locale can be omitted in file name.

The following is from the messages.properties file.

greeting=Hello World!

The following is from messages_fr.properties file. fr is for French.

greeting=Bonjour tout le monde!

Then, update faces-config.xml

<application>
   <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>fr</supported-locale>
   </locale-config>
   <resource-bundle>
      <base-name>com.java2s.messages</base-name>
      <var>msg</var>
   </resource-bundle>
</application>

Finally, we can use resource-bundle var.

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




Example

The following code is from welcome_zh_CN.properties.

welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0

The following code is from 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>

The following code is from UserBean.java.

package com.java2s.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());
            
          }
        }

  }

}

The following code is from welcome.properties.

welcome.jsf = Happy learning JSF 2.0


Download Internationalization.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml