Tuesday, August 6, 2013

Internationalizing Struts Application Using Browser Settings Tutorial

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
bundlename_language_country_variant.properties
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 ApplicationResource_en_US_WIN.properties.
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 ApplicationResource.properties file. To link the resouce bundle with the application the following tag should be added in the struts-config.xml file.
1.<message-resources parameter="com/vaannila/ApplicationResource"/>
The ApplicationResource.properties file contains the following key/value pairs.
1.label.user = User
2.label.password = Password
3.label.button = Login
The next step is to create ApplicationResource.properties file specific to the french language.
French - ApplicationResource_fr.properties
1.label.user = Usager
2.label.password = Mot de passe
3.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 ApplicationResource.properties file and display them using <bean:message> tag. The index.jsp page contains the following code.
01.<html>
02.<head>
03.<title>
04.Strus - I18N
05.</title>
06.</head>
07.<body>
08.<table>
09.<tr>
10.<td>
11.<bean:message key="label.user" />
12.</td>
13.<td>
14.<input type="text" name="user" />
15.</td>
16.</tr>
17.<tr>
18.<td>
19.<bean:message key="label.password" />
20.</td>
21.<td>
22.<input type="password" name="pass" />
23.</td>
24.</tr>
25.<tr>
26.<td colspan="2" align="center">
27.<input type="button" value='<bean:message key="label.button" />' />
28.</td>
29.</tr>
30.</table>
31.</body>
32.</html>
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 ApplicationResource.properties file. If the language is "fr" then the key values will be taken from the ApplicationResource_fr.properties 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.

No comments:

Post a Comment