JSF 2 internationalization example

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.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 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="http://www.w3.org/1999/xhtml"   
    	<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 :

About the Author

Founder of Mkyong.com and HostingCompass.com, 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.


  • Pingback: link()

  • Pingback: loan payment plan()

  • Pingback: I was reading this()

  • Pingback: water ionizer()

  • Pingback: alkaline water()

  • Pingback: alkaline water()

  • Pingback: philadelphia electricians union()

  • Pingback: electrician tools()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrków trybunalski()

  • Pingback: a plumbers resume()

  • Pingback: no title()

  • Pingback: locksmiths prices()

  • Pingback: my website()

  • Pingback: electrician career path()

  • Pingback: try this out()

  • Pingback: paypal loans()

  • Pingback: water ionizer loans()

  • Pingback: alkaline water brands()

  • Pingback: car parking()

  • Pingback: parking()

  • Pingback: laan penge nu uden renter()

  • Pingback: water ionizers()

  • Pingback: water ionizer()

  • Pingback: fue.mobi()

  • Pingback: DIRECTV packages()

  • Pingback: streaming movies()

  • Pingback: streaming movies()

  • Pingback: Blue Coaster33()

  • Pingback: I18n in JSF2 | Aayush Jain's Blog()

  • samir

    thanks a lottttttttt
    I’v been using your site always:) you saved me several times.
    i love u mkyong:)

  • tolga

    i tried it with my project but the change on dropdown list does not fire the countryLocaleCodeChanged method. I clearly spoored all the things but it doesnt work. only if i change default properties file in faces-config. any idea?

  • Pingback: JSF odkazy | BRAIN SNIPPETS()

  • Pingback: errors in internationalization in JSF | BlogoSfera()

  • nikh

    Error :Validation Error: Value is not valid

    your sample code is giving plain error.
    can you help in this ?

  • Issam

    Why don’t you do :


    (without the loop) ?

  • Pingback: JSF2 internationalization example | Code and Programming()

  • Pepe

    I want this example to display the language of the combos in the actual language selected.
    Could somebody point me how this can be done?

  • caau dung ngok

    i cannot change locale on jsf implement is myface2
    when i change select item the alway is Happy learning JSF 2.0.but is show the FacesContext.getCurrentInstance().getViewRoot().getLocale() was change.
    please help me.

  • http://ghamary.ir mohammad hosein

    thanks a lot
    I Love You…

  • Juan Felipe Narvaez


    I like so much this post, i have a question, how can i for that the internationalization keep in each page, because i have an error for this, when i press the indicate button that changes the language in that page but when i pass throught other page it back to the default. How can i do for that this keep the language always?


    • http://- wasabi

      The same. Help me.

  • kshitij

    Hello Friends

    I Have Problem To Making Site In Turkish Language.

    This Is My Property File For Turkish
    username1=kullan?c? ismi
    help1=yard?m etmek

    But when I am Printing This Property In My JSF page It Is Printing Something Like Below.
    kullanıcı ismi
    yardım etmek

    It Is Printing Some Extra Characters. I am Using UTF-8 encoding.

    I have Also Implemented Spanish, French, Portugese, And German There Is Not Problem With It.

    Just Problem In Turkish Language.

    IF Any body Can Help.


    • tolga

      try write it in eclipse properties editor. it automatically converts your characters as \u…

  • http://selcukyasar.net Selcuk

    Thanks for the example, but if i refresh the page, the locale is set again to the default language.

    my bean is session scoped in faces-config.

    whats the matter with it.

    • AA

      I have also facing the same problem, Have you find clue? please share if you got it. thanks

      • AA

        I got the solution. In jsf tag which has local attribute you can set all pages language through this tag.
        first set the country code and assign the value to local attribute. tag should be top of the page.

        • alex

          What he meant:
          In the you can set a locale (not local) attribute. Something like

          &lt;f:view locale=&quot;language.locale&quot;&gt;

          after the . You obviuosly need to implement a getLocale() method in the LanguageBean for that.

          Here’s my source with a different example (I think its easier to understand):

          • alex

            Ah stupid editor ate my tags. Correction:
            In the “f:view” tag … after the “html” tag.

  • qaa


    nice tutorial for the beginner. This example work for one page when user click on other page it doesn’t work. when user select one language how it can b sett for all other pages?


  • azend

    And what about the nations that the Locale not contains? E.g.: Mother Russia? :)

    • Sridhar

      Can any one help me:

      While I am trying the above example, I a getting the below exception:

      An Error Occurred:
      javax.el.PropertyNotFoundException: Property ‘localeCode’ not found on type com.igatepatni.LanguageBean


      Caused by:
      javax.el.PropertyNotFoundException – Property ‘localeCode’ not found on type com.igatepatni.LanguageBean
      at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214)

      /default.xhtml at line 25 and column 65 value=”#{language.localeCode}”


  • hgo

    Thank you for example but i have a question. ViewRoot’s locale isn’t only applicable for the current request? If we need a locale info for all session, what to do?