Internationalization in JSF

SHARE & COMMENT :

Introduction

In the current arena of Globalization, the user base of a web application has been extensively increasing day by day. With this huge increase in the user base, it is very essential that the applications are very well understood and interpreted by people of different cultures and different languages. It is then, the whole concept of Internationalization becomes very vital for a web application.

also read:

What is Internationalization?

Internationalization is a process of preparing a web application content compatible to a user/region’s preferred language and country. For example, an application with Japanese content will be better understood by Japanese people rather than people who do not understand Japanese. In other words, Internationalization makes it possible for a web application to support more than one language. Internationalization is also referred to as I18N.
The need for Internationalization is, in the current global scenario like developing a website or an application, there will be multiple service requests from multiple countries. So, we cannot use a common language and format to cater all these varied requests.Usually internationalization will be done for label text for UI components, messages and other static strings displayed on the page. The format of display of date, currency etc can be internationalized to suit a particular locale.

Localization is a process of adapting an internationalized application to support specific region or a locale. Locale is a representation of language and country combination. Typically a locale is represented as: <language code>_<country code> Although, Internationalization and Localization of applications plays a very important role in the scenario of web applications it can be extended to any of the Client user interfaces.

Internationalization using JSF

JSF provides a support to achieve Internationalization and Localization. In JSF for supporting more than one language, we need to provide all required locale specific messages in a .properties file. These locale based .properties files are called Resource Bundle in JSF. The locale specific resource bundles are supposed to have a naming convention as follows:Filename_language_country.properties
Usually, when a request is sent the preferred language is taken from the request header and it is used to select the most nearest matching resource bundle. For example, if a request has ‘ja-JP’ as the preferred locale, and then JSF will try to find a resource bundle having _ja_JP in its name as per the above naming convention. If resource bundle with _ja_JP is not found, then it will try to select a resource bundle with _ja. If this bundle is also not available then it selects the default locale mentioned for the web application.

Steps for implementing Internationalization in JSF

Implementing I18N in JSF requires some simple steps which are as follows:

  • Create locale specific resource bundles (.properties file) with proper naming convention and place it inside the src folder.
  • Register the resource bundles created in faces-config.xml using <message-bundle>or using<f:loadBundle >

Eg: Using <f:loadBundle> in each view

<f:loadBundle   var="msg"     basename="ApplicationResource"/> 

Using faces-config.xml for entire application

<application>
    	<resource-bundle>
		<base-name>ApplicationResource</base-name>
    		<var>msg</var>
   	 </resource-bundle>
    	 <locale-config>
      		<default-locale>en_US</default-locale>
      		<supported-locale> ja_JP </supported-locale>
    	 </locale-config>
  </application>

Configure I18N support in the required JSF views

The JSF view should be configured to support the Internationalization. The loadBundle of the JSF core library should be defined, as follows.

<f:loadBundle basename="ApplicationResource" var="mapValue" />

Here, ‘basename’ is the name of the filename of the resource bundle, without the language and country code. ‘var’ is the identifier (a request scoped variable) that is to be used for fetching values from the selected bundle. A resource bundle contains key value pairs for all the required messages. The key will be used (with the identifier mentioned by ‘var’ of) to fetch the corresponding values.
A simple implementation of I18N in a web application is shown below.

Step 1: Create all the required resource bundle files

The resource bundle for English (default locale) and Japanese (supported local) having file name ApplicationResource.properties and ApplicationResource_ja_JP.properties respectively are shown below.

# Resource bundle for English
username= Username
password= Password


# Resource bundle for Japanese
 
username= Shougou
password= aikotoba

Shougou and aikotoba are the Japanese word for Username and password respectively.The unicode to view the japanese script is as follows.

username= \u610F\u5473
password= \u89E3\u6790

Step 2: Usage of resource bundles in the different views in the JSF application

To use the resource bundle in the view, we use the load bundle tag as shown in the Login.jsp page as shown below:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Login JSP</title></head>
    <f:view>
<f:loadBundle basename="ApplicationResource" var="localeValue" />
    <body>
      <h:outputText value="#{localeValue.username}" /><h:inputText value=”#{login.username}” />
      <h:outputText value="#{localeValue.password}"/> <h:inputText value=”#{login.password}” />
</body></f:view>
</html>

Step 3: Configuring it in the faces-config.xml

If we want to internationalize the entire web application we can also load the respective resource bundle files in the faces-config.xml, configuration file. The resource bundle is mapped in faces-config.xml as shown below.

<application>
        <locale-config>
               <default-locale>en</default-locale>
               <supported-locale>ja</supported-locale>
       </locale-config>
               <message-bundle>ApplicationResource</message-bundle>
         </application>

Step 4:Output of the Application

In EnglishIn

Japanese

Conclusion:

In this article, I have explained the importance of Internationalization and Localization of web applications. Also I have discussed about making a JSF web application support for different locales with the help of code snippet.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better