Multiple Message Resource Bundle in Struts 1

Target Audience: Web Developer, Web Component Developer

What you should know already: Struts Web Framework, MVC Pattern, Knowledge of struts-config.xml file

Introduction

The message resource class in Struts allows the developer internationalizing web application easy and fast. The user can put labels of fields or description text in a central file and can access them later in the JSP File. The advantage is that you can reuse labels like error messages, titles in multiple JSP files and you can provide the ressource files in multiple languages.

message-resource Element

You might have already seen the message-resource element used in the file struts-config.xml

<message-resources parameter="ApplicationResource" null="false" />

However, it is not often recognized that you can use more than one of these elements in struts-config.xml file. For example, this is a perfectly legal configuration:
<message-resources parameter="ApplicationResrc_English" null="false" />
<message-resources parameter="ApplicationResrc_French" key="fr" />
<message-resources parameter="ApplicationResrc_German" key="gr" />

The first of these – without a key attribute – becomes the default resource set. So, all your JSPs and servlets have access to that set of resources through the servlet context.

Content of ApplicationResrc_English.properties is:
label.register=register
Content of ApplicationResrc_German.properties is:
label.register=registrieren

How to Access?

Your JSP page must indicate a resource if they want to use the non-default set (in this case ApplicationResrc_German). To do this, you will need to use the message tag, part of the bean tag library. So, assume that,

  • you have associated the bean tag library with bean prefix
  • you have got a property in the ApplicationResrc_German bundle with the key label.register, then

<bean:message bundle="gr" key="label.register" />
This will print registrieren

If you use without the bundle attribute, it will pick from the default resource set which from ApplicationResrc_English.properties file.

<bean:message key="label.register" />
This will print register

Thus, by configuring multiple message resource bundles in Struts 1, website can easily be internationalized.

Permanent link to this article: https://blog.openshell.in/2010/11/multiple-message-resource-bundle-in-struts-1/

Leave a Reply

Your email address will not be published.