JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Internationalization in JSF

April 18, 2011 by Krishna Srinivasan Leave a 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:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

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
[code lang=”html”]<f:loadBundle var="msg" basename="ApplicationResource"/> [/code]
Using faces-config.xml for entire application
[code lang=”xml”]<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>[/code]

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.
[code lang=”html”]<f:loadBundle basename="ApplicationResource" var="mapValue" />[/code]
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.
[code]
# Resource bundle for English
username= Username
password= Password

# Resource bundle for Japanese

username= Shougou
password= aikotoba
[/code]
Shougou and aikotoba are the Japanese word for Username and password respectively.The unicode to view the japanese script is as follows.
[code]username= \u610F\u5473
password= \u89E3\u6790[/code]

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:
[code lang=”html”]<%@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>[/code]

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.
[code lang=”xml”]<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>ja</supported-locale>
</locale-config>
<message-bundle>ApplicationResource</message-bundle>
</application>[/code]

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.

Filed Under: JSF Tagged With: Internationalization

Implement Internationalization and Localization in JSF

July 24, 2008 by Krishna Srinivasan Leave a Comment

Internationalization and Localization are important features for an web based application. Internationalization is implementing the features in your application to support multiple languages. Localization is creating text in a specific language that is presented through an internationalized application

also read:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

The following example program demonstrates very simple program for implementing this concepts. if you look into the faces-config.xml file,locale-config element is used for specifying the default locale and supported locale information. In this sample application we have provided default as English and supported as French.

Use resource-bundle element to specify the name of the properties file to be used. For example if you create a resource bundle name as Messages, then all the language letters will be suffixed with the file name.In our case French locale is named as “Messages_fr”.The default will be “en”, it is not required to specify explicitly.

[code lang=”html”]
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
<body>
<f:view>
<h:form>
<h:commandButton value="#{msg.cancel}" action="fail"/>
<h:commandButton value="#{msg.submit}" action="success"/>
</h:form>
</f:view>
</body>
</html>

[/code]

faces-config.xml

[code lang=”xml”]
<?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</supported-locale>
<supported-locale>de</supported-locale>
</locale-config>
<resource-bundle>
<base-name>javabeat.net.messages.Messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>

[/code]

Messages.properties

[code]
cancel=Cancel
submit=Submit
[/code]

Messages_fr.properties

[code]
cancel=annuler
submit=envoyer
[/code]

Filed Under: JSF Tagged With: Internationalization

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved