Internationalization-Localization using JSF-Richfaces-SEAM

Implementing Internationalization-Localization using JSF-Richfaces-SEAM with less coding effort – the processes of adaptation of web applications for diversity of 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.

Internationalization-Localization using JSF-Richfaces-SEAM

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 is less coding effort and a configuration.

Yes, we are going to implement Internationalization with less coding and configuration effort. No manual coding effort is required in terms of bean and processing Locale.

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


Steps to do

Here we will go through step-by-step approach to achieve the article goal. If you have doubts, please free to post it via comment.

Step 1

Define a Locale configuration in  faces-config.xml for example consider following Locale codes.

Step 2

Create various messages properties as per your requirement for each Locale. Properties file name consists of two portion prefix and suffix. Following standard naming convention, we will use messages as prefix and Locale string as suffix. Of-course file extension of .properties will be appended at the end.

For example:

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

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 create a .xhtml file and utilizing the messages from the properties which we created from earlier steps. Ensure XML namespace xmlns:f="http://java.sun.com/jsf/core referenced in the XHTML.

Step 4

Of-course we will be placing this tag f:loadBundle in global or template file, whichever the name you call; something like header.xhtml or resources.xhtml.  Include a tags like below-

f:loadBundle is dynamically loads the messages for different countries based on Locale value localeSelector.localeString from the framework.

Explanation of tag and attributes used-

  • f:loadBundle tag, it loads mentioned properties file from classpath
  • basename attribute, name of the properties file
  • localeSelector.localeString function – localeSelector is a SEAM component, it provides the locale code string via localeString attribute.
    • Let’s taken an example Locale as a Japan. Then this method returns ja_JP, so basename attribute dynamically become a value of  product_messages_ja_JP and user_messages_ja_JP
  • var="prodmsg" name of the variable for accessing properties key value in the XHTML files

Step 5

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

Using Product and User messages in XHTML/HTML or JSF:






Conclusion

Now we are good to go, we have successfully achieved the internationalization-localization in JSF-Richfaces-SEAM with less coding effort!.