Struts 1 > Internationalizing Struts Application

Internationalizing Struts Application

Internationalization also known as I18N is used for displaying content specific to the locale based on their languages, currencies and formatting conventions.

Resource bundle is the file that contains the key/value pairs for the default language of your application. The naming format for this file is

For example, if you have a bundle named ApplicationResource for the English language in the Unites States for the Windows platform, then your properties file name would be

Lets take a senario in which the user can see the same page in four different languages like English, French, German and Italian. The jsp page gets displayed according to the language selected by the user.

Our default resource bundle is file. To link the resouce bundle with the application the following tag should be added in the struts-config.xml file.

<message-resources parameter="com/w3javaguide/ApplicationResource"/>

The file contains the following key/value pairs.

label.user = User
label.password = Password
label.button = Login

The next step is to create file specific to the french language.

French -

label.user = Usager
label.password = Mot de passe
label.button = Entrer

In this example we will see how to internationalize Struts application according to the language selected in the browser.

Here we don't specify any language specific values in the jsp page, instead we specify them in the file and display them using <bean:message> tag. The index.jsp page contains the following code.

Strus - I18N
            <bean:message key="label.user" />
            <input type="text" name="user" />
            <bean:message key="label.password" />
            <input type="password" name="pass" />
        <td colspan="2" align="center">
            <input type="button" value='<bean:message key="label.button" />' />

According to the language selected in the browser, the corresponding properties file will be used to fetch the key values. If the language is "en" then the key values will be taken from the file. If the language is "fr" then the key values will be taken from the file

By default the language selectd in the browser is English ("en"), so when we run the example the following page is displayed.

Lets change the Language to French ("fr"). Go to Internet Explorer -> Tools -> Internet Options -> Click the Languages button -> Add the French language -> Move the French language to the first position.

Refresh the screen. Now all the user messages will be displayed in the French language.

You can download the source code of the Struts Internationalization example by clicking on the Download link below.

Source :Download
Source + Lib :Download
Contact Us  |  Copyright © 2012