My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Friday, December 4, 2009

Struts and internationalization

Struts Internationalization (i18n) can be done with some handy modifications in our existing application. We have to know the two Internationalization (i18n) components that are packaged with the Struts Framework. 
  1. The first of these components, which is managed by the application Controller, is a Message class that references a resource bundle containing Locale-dependent strings.
  2. The second Internationalization (i18n) component is a JSP custom tag, , which is used in the View layer to present the actual strings managed by the Controller.
Here are the steps I would like to outline for internationalizing your application using Struts
First thing we will require for Internationalization (i18n) is a set of simple Java properties files.
  1. Each file contains a key/value pair for each message that you expect your application to present, in the language appropriate for the requesting client.
  2. This property file contains the key/value pairs for the default language of your application. The naming format for this file is ResourceBundleName.properties.
  3. An example of this default file, using English as the default language, would be ApplicationResources.properties.
  4. Define a properties file for each language that your application will use.
  5. This file must follow the same naming convention as the default properties file, except that it must include the two-letter ISO language code of the language that it represents.
Example of this naming convention
i.            For an German-speaking client would be ApplicationResources_de.properties 
ii.            For an French-speaking client would be ApplicationResources_fr.properties
iii.            For an Italian-speaking client would be ApplicationResources_it.properties
iv.            For an Spanish-speaking client would be ApplicationResources_es.properties 
Now add the respective entries in each properties files you require.
  1. After you define all of the properties files for your application, you need to make Struts aware of them.
  2. It is achieved by adding a sub-element to the struts-config.xml file.
  3. Copy all the resource bundles into the application classpath, /WEB-INF/classes/example,
  4. and then use the package path plus the base file name as the value of the subelement.

  5. The following snippet shows an example of using the subelement to configure a resource bundle, using the properties files described above

Use JSP custom tag, 
, which is used to present the actual strings that have been loaded by the Controller.
  1. To use the , we must first deploy the bean tag library, which contains the tag.
  2. Deploying a tag library is a very simple process that requires only the addition of a new entry in the web.xml file of the Web application using the bean library.
  3. Here is an example of this entry:

/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld
 
  1. Check that the struts-bean.tld file is copied to the /WEB-INF/ folder.
  2. Now your tag and how it is configured for use.
  3. Copy all of the properties files to the /WEB-INF/classes/example directory and add an application subelement,

                
                 
                 

                
                
                

Modify the index.jsp file index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 


Sample Struts Application



:
Modify the diplayname.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 


Sample Struts Display Name


<%= request.getAttribute("NAME") %> !!
Some of the finer things which needs to be noted are as follows
  1. The correct entry in struts-config.xml would be:
  2. All the resource bundles SHOULD BE in WEB-INF\classes
  3. Execute method SHOULD always have
LanguageForm lf =(LanguageForm) form;
Locale locale = new Locale(lf.getLanguage()); 
HttpSession session = request.getSession(true);
super.setLocale(request, locale);
internationalization the value of the buttons

No comments :