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.

jsf2-internationalization-folder

2. Properties file

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

welcome.properties


welcome.jsf = Happy learning JSF 2.0

welcome_zh_CN.properties


welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0
Note
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.

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>
     	   </locale-config>
	   <resource-bundle>
		<base-name>com.mkyong.welcome</base-name>
		<var>msg</var>
	   </resource-bundle>
     </application>
</faces-config>

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;
 
@ManagedBean(name="language")
@SessionScoped
public class LanguageBean 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;
	}

	//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()) {
        
        	   if(entry.getValue().toString().equals(newLocaleValue)){
        		
        		FacesContext.getCurrentInstance()
        			.getViewRoot().setLocale((Locale)entry.getValue());
        		
        	  }
               }
	}

}

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" 
"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>
    	
    	<h1>JSF 2 internationalization example</h1>
		
     <h:form>
			
	<h2>
		<h:outputText value="#{msg['welcome.jsf']}" />
	</h2>
			
	<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>

6. Demo

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

Default, locale English is display.

jsf2-internationalization-example-1

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

jsf2-internationalization-example-2

Download Source Code

Reference

  1. Resource bundle in JSF 2.0
  2. Create a Locale (Oracle Tutorial)
  3. Spring MVC internationalization example
  4. W3C charset

About the Author

author image
mkyong
Founder of Mkyong.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.

Comments

avatar
32 Comment threads
11 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
31 Comment authors
RolandNavin IsraniAlejandroBauke ScholtzRubens Sertage Recent comment authors
newest oldest most voted
Alejandro
Guest
Alejandro

I have a doubt, to see if someone can answer it. What happens if i want to update a page that contains a form, but i don’t want that the values of the view will be update, but only the literals ??? i guess that manually it’s possible to achieve that by a function that catches all values within a form and then restore it after the language was changed; but i inquire if there are some way to do it much more straightforward, using some function in JSF or even with some external library, cause it’s possible that it… Read more »

Navin Israni
Guest
Navin Israni

I have a doubt here: what’s difference between oonchange vs valueChangeListener? Which one will be executed first?

Where does the submit() method come from for onchange in this example?

Viju
Guest
Viju

Hi Mkyong,
I’ve got this tutorial working but I got a new problem which is: this only takes effect on the actual page. If I change pages then the previous language selected or the basic one remains. How can I change that please???

Pankaj
Guest
Pankaj

this is gr8 for language change…can u tell how to do locale change(date format , amounts and currency)

Juan Fandiño
Guest
Juan Fandiño

Hi Mkyon
I have this problem: My application web has many xhtml and When I executed FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale); change language properly but when click to another xhtml, FacesContext.getCurrentInstance().getViewRoot().getLocale()); changes to default whithout executing the set method

¿What happens?

Thanks

raky
Guest
raky

Thanks Mkyong , i have finally made it working the issue were properties file names and Locale name- messages_en_GB.properties ,messages_fr_FR.properties and countries.put(“French”,Locale.FRANCE);
countries.put(“UK”, Locale.UK); //label, value

raky
Guest
raky

Hello Mkyong, Your effeorts for software community is great and cannot be matched with anybody. but i am afraid unfortnately i have spend many times on your tutorials and never but most of them work as like this, it does not work JSf2. I am sorry but soemtime it looks like it is pure time waste of time running ur code.

Please accept my apologies.

Thanks

Rocky
Guest
Rocky

Excuseme mykong. How can put spanish how default languaje and english, german supported languaje in:
private static Map countries;
static{
countries = new LinkedHashMap();
countries.put(“English”, Locale.ENGLISH); //label, value
countries.put(“Chinese”, Locale.SIMPLIFIED_CHINESE);
}

rocky
Guest
rocky

countries.put(“English”, Locale.ENGLISH); //label, value
countries.put(“Chinese”, Locale.SIMPLIFIED_CHINESE)
Hi, how can use “English”, from properties in this method
for example ResourceBundle rs=new ??. Thanks

Lovely
Guest
Lovely

Thank you :)

trackback
I18n in JSF2 | Aayush Jain's Blog

[…] Here is a nice link : http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ […]

chrisperro
Guest
chrisperro

Thank you! This was very helpful!

samir
Guest
samir

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

tolga
Guest
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?

raky
Guest
raky

Please see my above message. i had same issue and working now, please do let me knwo more details i shall try to help u cheers

trackback
JSF odkazy | BRAIN SNIPPETS

[…] i18n – Mkyong […]

trackback
errors in internationalization in JSF | BlogoSfera

[…] I working according the below example.. http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ […]

nikh
Guest
nikh

Error :Validation Error: Value is not valid

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

Issam
Guest
Issam

Why don’t you do :

FacesContext.getCurrentInstance().getViewRoot().setLocale(countries.get(newLocaleValue));

(without the loop) ?

Yanyd
Guest
Yanyd

There something wrong .
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(newLocaleValue));

trackback
JSF2 internationalization example | Code and Programming
Pepe
Guest
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
Guest
caau dung ngok

Hi,
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.

mohammad hosein
Guest
mohammad hosein

thanks a lot
I Love You…

Juan Felipe Narvaez
Guest
Juan Felipe Narvaez

Hi,

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?

Regards

wasabi
Guest
wasabi

The same. Help me.

kshitij
Guest
kshitij

Hello Friends

I Have Problem To Making Site In Turkish Language.

This Is My Property File For Turkish
/*
login1=giri?
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
giriÅŸ
*/

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.

Regards
kshitij

tolga
Guest
tolga

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

Selcuk
Guest
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
Guest
AA

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

AA
Guest
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
Guest
alex

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

<f:view locale="language.locale"> 

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):
http://www.softwarepassion.com/java-ee-6-jsf-2-0-internationalization/

alex
Guest
alex

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

qaa
Guest
qaa

Hi,

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?

Regards,

azend
Guest
azend

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

Sridhar
Guest
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

viewId=/default.xhtml
location=D:\sridhar\JSF_Pratice\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Jsf_internationalization\default.xhtml
phaseId=RENDER_RESPONSE(6)

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}”

Regards,
M.sridhar.

hgo
Guest
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?

Nahla
Guest
Nahla

for all session , you ought use in the templates for all your pages

Bauke Scholtz
Guest
Bauke Scholtz

Poor example. The is completely overlooked. For a proper example, look here: http://stackoverflow.com/q/4830588/157882.

Rubens Sertage
Guest
Rubens Sertage

To complement: To keep the locale in session you can put

in the pages you want to show with the seleted language.
And of course keep a variable of localeSelect in your languageBean.

Viju
Guest
Viju

Hi Mkyong,
I’ve got this tutorial working but I got a new problem which is: this only takes effect on the actual page. If I change pages then the previous language selected or the basic one remains. How can I change that please???