<message-resources parameter="com/vaannila/ApplicationResource"/>
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.
|