In JSF application, you can change your application locale programmatically like this :

//this example change locale to france
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale('fr');

It makes JSF support for internationalization or multiple languages easily.

Complete JSF internationalization example

In this tutorial, we show you a JSF 2.0 web application, which display a welcome page, retrieve a welcome message from properties file, and change the welcome message dynamically based on the selected language.

1. Project Folder

Directory structure for this example.


2. Properties file

Here’s two properties files to store English and Chinese message.

welcome.jsf = Happy learning JSF 2.0

welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0
For UTF-8 or non-English characters, for example Chinese , you should encode it with native2ascii tool.

3. faces-config.xml

Include above properties file into your JSF application, and declared “en” as your default application locale.


<?xml version="1.0" encoding="UTF-8"?>

4. Managed Bean

A managed bean, which provide language selection list , and a value change event listener to change the locale programmatically.

LanguageBean .java

package com.mkyong;
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 LanguageBean 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;
	//value change event listener
	public void countryLocaleCodeChanged(ValueChangeEvent e){
		String newLocaleValue = e.getNewValue().toString();
		//loop country map to compare the locale code
                for (Map.Entry<String, Object> entry : countries.entrySet()) {

5. JSF Page

A JSF page to display a welcome message from properties file, and attach a value change event listener to a dropdown box.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""   
    	<h1>JSF 2 internationalization example</h1>
		<h:outputText value="#{msg['welcome.jsf']}" />
	<h:panelGrid columns="2">
		Language : 
		<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
   			<f:selectItems value="#{language.countriesInMap}" /> 

6. Demo

URL : http://localhost:8080/JavaServerFaces/faces/default.xhtml

Default, locale English is display.


If user changes the dropdown box language, it will fire a value change event listener and change the application locale accordingly.


Download Source Code


  1. Resource bundle in JSF 2.0
  2. Create a Locale (Oracle Tutorial)
  3. Spring MVC internationalization example
  4. W3C charset
Tags :
Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Related Posts

Popular Posts