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


Finally, we can use resource-bundle var.

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


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" 
<html xmlns="http://www.w3.org/1999/xhtml"   
        <h:outputText value="#{msg['welcome.jsf']}" />
      <h:panelGrid columns="2">
        Language : 
        <h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
             <f:selectItems value="#{language.countriesInMap}" /> 

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;
public class UserBean implements Serializable{
  private static final long serialVersionUID = 1L;
  private String localeCode;
  private static Map<String,Object> countries;
    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()) {



The following code is from welcome.properties.

welcome.jsf = Happy learning JSF 2.0

Download Internationalization.zip


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.