Home Jsf JSF internationalization example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JSF internationalization example

Advertisement
In software development, internationalization is the process to make the application adaptable to various languages and regions.

JSF internationalization example

    

In software development, internationalization is the process to make the application adaptable to various languages and regions. This tutorial shows you how to develop an internationalization application in JSF.

In this tutorial, you will be demonstrated how this concept can be implemented in JSF.

You can download the code example discussed here form download link given at the bottom of this article.

faces-config.xml: In faces-config.xml file, "locale-config" element is used to specify default locale and supported locale information. The resource-bundle element is used to specify the base name of the properties file and to specify the variable name which will be used in JSP file to get the value of property. In our example resources is the name of the folder where properties files are available.

 <application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr_FR</supported-locale>
</locale-config>

<resource-bundle>
<base-name>resources.welcome</base-name>
<var>message</var>
</resource-bundle>
</application>

Managed Bean: This managed bean has method localeChanged() which is to be called when we select the language in jsp. This method gets the new locale and set it for the application.

package com.roseindia;

import java.util.*;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

public class LocaleBean{

private String locale;
private Map<String,Object> countryMap;

public LocaleBean(){
countryMap = new LinkedHashMap<String,Object>();
countryMap.put("English", new Locale("en"));
countryMap.put("French", new Locale("fr","FR"));
}


public Map<String, Object> getCountries() {
return countryMap;
}

public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}

public void localeChanged(ValueChangeEvent e){

String localeStr = e.getNewValue().toString();

for (Map.Entry<String, Object> entry : countryMap.entrySet()) {
if(entry.getValue().toString().equals(localeStr)){
Locale locale = (Locale)entry.getValue();
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
}
}

Managed Bean in faces-config.xml: Register the managed bean in faces-config.xml file

<managed-bean>
<managed-bean-name>language</managed-bean-name>
<managed-bean-class>com.roseindia.LocaleBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Complete faces-config.xml: After adding above requirements the faces-config.xml file looks like as below:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
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_1_2.xsd">

<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr_FR</supported-locale>
</locale-config>

<resource-bundle>
<base-name>resources.welcome</base-name>
<var>message</var>
</resource-bundle>
</application>

<managed-bean>
<managed-bean-name>language</managed-bean-name>
<managed-bean-class>com.roseindia.LocaleBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>

Properties file: The base name is used when creating properties file for different locales. The base name specified in faces-config.xml file is required to be the prefix of the name of properties file and then the name of the locale for which you want to create the file.

1. "welcome.properties" to store messages in English

my.text=Welcome to Locale Test

2. "welcome_fr_FR.properties" to store messages in French

my.text=Bienvenue à l'essai Locale

JSP page: This jsp page is used to let the user select the language and display the message for that locale.

The message variable name specified in faces-config.xml file is used for getting the value of the property. For example, message['my.text'] will take the value of my.text property from any one of the properties file according to the locale selected. By default value will be takes from the message.properties file. If we select the French language then the value will be takes from message_fr_FR.properties file. In this jsp file the user can select the language which will in turn calls localeChanged() method defined in managed bean. This method is responsible to change the locale to the new locale selected by the user.

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html>
<body>
<h:form>
<h:outputText value="#{message['my.text']}" />

<h:panelGrid columns="2">

Select Language
<h:selectOneMenu value="#{language.locale}" onchange="submit()"
valueChangeListener="#{language.localeChanged}">
<f:selectItems value="#{language.countries}" />
</h:selectOneMenu>

</h:panelGrid>
</h:form>
</body>
</html>
</f:view>

After successful compilation and building the application, you will see the output as below

When you change the language to French from the dropdown list. Then the welcome message is changed to french which is taken from the french properties file. You can see the output as below:

Download complete applicaton

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 17, 2011

Ask Questions?    Discuss: JSF internationalization example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
deepak
February 29, 2012
internationalization example fail to work

After selecting french, the form gets submitted and again form was loaded.This doesnot allow us, to select and stay on french and see the french version.
DMCA.com