Summary

Internationalization and Localization are the processes of adaptation of web applications for different languages and cultures. When a site is developed, the requirement may be to localize the site for United States of America, England, Germany, Japan, Chinese (Simplified/Traditional), etc. Language is not the only thing that undergoes the localization — dates, times, numbers, currencies, phone numbers, addresses, graphics, icons, colors, personal titles and even favorite sounds are also vary from country to country. It means that an internationalized application may have lots of different types of information, which is dynamic based on user location.

No surprise finding lot of articles on implementing the Internationalization in JSF/SEAM over surfing, but the crux which makes this article different from others is

Achieving internationalization by leveraging framework capabilities less coding effort, just configuration

Yes, We are going to implement Internationalization without any coding effort. No manual coding effort is required in terms of bean and processing locale’s.

There are several approaches of organizing the localization. You can use f:loadBundle – loads bundles into the request scope when page is being rendered / a4j:loadBundle – it allows to use reference to a particular bundle item during an Ajax update.


Steps to do

Step 1:
Define a locale configuration in faces-config.xml

en_US
en_US
en_UK
de_DE
ja_JP

Step 2:
Create various messages properties as per you need/requirement for each locale. Properties file name consists of two portion, prefix and suffix. As standard naming convention we will use “messages” as prefix and “locale string” as suffix. Of course file extension properties will be appended at the end.

For example:
Assume we are going to create two messages.properties one holding product related messages & another holding user messages for US,UK,DE & JP.

product_messages_en_US.properties
user_messages_en_US.properties

product_messages_en_UK.properties
user_messages_en_UK.properties

product_messages_de_DE.properties
user_messages_de_DE.properties

product_messages_ja_JP.properties
user_messages_ja_JP.properties

etc.

Place all the message properties which you have created into resources directory “src/main/resources”
or
you can keep these message properties in classpath as well. Either way is fine!

Step 3:
Now we will create a .xhtml file utilizing the messages from the properties we have created above. Ensure XML namespace “xmlns:f=”http://java.sun.com/jsf/core” refered in the XHTML

Step 4:
Of course we will be placing this tag f:loadBundle in centralized file, something like header.xhtml/resources.xhtml.

Include tag in xhtml

<f:loadBundle basename="product_messages_#{localeSelector.localeString}" var="prodmsg" />
<f:loadBundle basename="user_messages_#{localeSelector.localeString}" var="usermsg" />

f:loadBundle dynamically loads the messages for different countries based on locale.

Description:

  • f:loadBundle tag – loads mentioned properties file from classpath
  • basename attribute- name of the properties file
  • localeSelector.localeString function – localeSelector is SEAM component, it provides the locale string via localeString attribute. For example: lets take locale as Japan. This method returns “ja_JP”, so basename gets fulfilled with “product_messages_ja_JP” and “user_messages_ja_JP”
  • var=”messages” – variable name for accessing key value

Step 5:
Let’s use the message bundle in the pages, we have full access to localization key values

Using Product Messages in HTML / JSF:
HTML:

<div>${prodmsg['label.page.productLabel']} : </div>

JSF:

<h:outputText value="#{prodmsg['label.page.productLabel']}" />

Using User Messages in HTML / JSF:

HTML:

<div>${usermsg['label.user.welcomeGreetings']}</div>

JSF:

<h:outputText value="#{usermsg['label.user.welcomeGreetings']}" />

Result

Now we are good to go, we have achieved successfully; the internationalization/localization in JSF/SEAM! without any coding effort!.