Internationalizing Struts Application Using Browser Settings Tutorial
Join the DZone community and get the full member experience.Join For Free
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 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.
The ApplicationResource.properties file contains the following key/value pairs.
label.user = User label.password = Password label.button = Login
The next step is to create ApplicationResource.properties file specific to the french language.
French - ApplicationResource_fr.properties
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 ApplicationResource.properties file and display them using <bean:message> tag. The index.jsp page contains the following code.
<html> <head> <title> Strus - I18N </title> </head> <body> <table> <tr> <td> <bean:message key="label.user" /> </td> <td> <input type="text" name="user" /> </td> </tr> <tr> <td> <bean:message key="label.password" /> </td> <td> <input type="password" name="pass" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="button" value='<bean:message key="label.button" />' /> </td> </tr> </table> </body> </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.
You can download the source code of the Struts Internationalization example by clicking on the Download link below.
Source + Lib: Download
Opinions expressed by DZone contributors are their own.