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.
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
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.
1 2 3 4 5 |
en_US en_US en_UK de_DE ja_JP |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 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-
1 2 3 4 |
<!-- Loading appropriate properties file with access name --> <f:loadBundle basename="product_messages_#{localeSelector.localeString}" var="prodmsg" /> <f:loadBundle basename="user_messages_#{localeSelector.localeString}" var="usermsg" /> |
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!.