INTERNATIONALIZATION of Struts

Introduction

Multinational Corporations have their branches in various parts of the world. So, they must provide products and services to their clients and customers in their traditional way. Customers expect the product to work in their native languages, especially the date, time, currency etc. So, we should not make any assumptions about their clients region or language. If such assumptions become invalid, we have to re-engineer our applications.

Internationalization or I18N is the process of designing the software to support multiple languages and regions, thereby eliminating the need to re-engineer them. The applications should support every language or country.

Struts provides various locale sensitive JSP tags that can be used to make applications simpler. With this short introduction we shall see how to implement I18n in a simple JSP file of Struts. Refer code 1.

Code 1

g:\>md localedemo
g:\>cd localedemo
g:\localedemo>edit localedemo.jsp

// g:\localedemo\localedemo.jsp

<%@ page language="java" %>

<%@ taglib 
uri="/WEB-INF/struts-html.tld" 
prefix="html" %>
<%@ taglib
uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>
<%@ taglib
uri="/WEB-INF/struts-logic.tld"
prefix="logic" %>

<html:html
locale="true">
<body bgcolor=pink>

<bean:message key="index.info" />

</body>
</html:html>


Next, copy struts-blank.war to f:\tomcat41\webapps and start the tomcat with JAVA_HOME as jdk1.4. A folder named struts-blank will be created. Rename the folder as localedemo. Copy the above JSP file to f:\tomcat41\webapps\localedemo.

Now we have to edit the property files for various locales. The struts framework (struts1.1) provides a property file named application.properties. It is present in the folder f:\tomcat41\webapps\localedemo\web-inf\classes\resources. We have to add our own property file in this folder only. Our property file must be named along with the language code. For example the language code of:
1. German - de
2. Spanish - es
3. English - en
4. Korean - ko
5. French - fr
6. Italy - it

So, when we write I18n message in German language, it must be placed in the property file named application_de.properties and all the properties files must be present in the resources folder only. Also, when we write the property file of a particular language, it need not be of the same language. For example, we can create application_de.properties and write the message in French or English. In fact, the message does not depend on any language. It is a simple key value pair. The message we give for the key is just substituted. The property file to locate the value of key depends on the language settings of the browser. For this example, we will write four properties file as in code 2.

Code 2

f:\tomcat41\webapps\localedemo\web-inf\classes\resources\application_de.properties

index.info=GERMANY
------------------------------------------
f:\tomcat41\webapps\localedemo\web-inf\classes\resources\application_es.properties

index.info=SPAIN
------------------------------------------
f:\tomcat41\webapps\localedemo\web-inf\classes\resources\application_en.properties

index.info=ENGLISH
------------------------------------------
f:\tomcat41\webapps\localedemo\web-inf\classes\resources\application_fr.properties

index.info=FRANCE
------------------------------------------

Also, append this text in the application.properties file index.info=STRUTS TUTORIAL. 

Now we have to add entry in the struts-config.xml file for all the properties files. The entry and its corresponding portion are shown in code 3.

Code 3

<!-- Message Resources Definitions -->

<message-resources parameter="resources.application_fr"/>
<message-resources parameter="resources.application_es"/>
<message-resources parameter="resources.application_en"/>
<message-resources parameter="resources.application_de"/>
<message-resources parameter="resources.application"/>


Now restart the Tomcat server. Open Internet Explorer and type the URL as http://localhost:8080/localedemo/localedemo.jsp. We will get the message 'ENGLAND'. This is because our default browser language is 'United States English'.

Next, we have to change the language settings of the browser to change the locale. For that, open a new Internet Explorer, go to 'Tools' menu and select 'Internet Options'. In the 'Internet Option' dialog box, select 'General' tab and click the 'Languages...' button. We will get 'Language Preference' dialog box. Here click 'Add...' button and add the languages. For this example, add English, Spanish, German and French. Here we have languages specific to particular regions. We have French Belgium, French Canada, French France, etc. We can select any one of these in all cases. Then select 'German' and by using the 'Move up' button, place it on the top. Now type the URL as http://localhost:8080/localedemo/ localedemo.jsp. We will get the message 'GERMAN' Similarly, you can try placing 'Spanish' and 'French' at the top, we will get the message 'SPAIN' and 'FRANCE', respectively.

The author is available at: farihahnoushene@yahoo.co.in




Added on June 11, 2007 Comment

Comments

Post a comment

Your name:

Comment: