How to Change Locale Struts in Java
Struts are components of Java that provide various locale sensitive JSP tags, which are often used to simplify Java applications. They are also used for updating internationalization or I18N, which is the process of software design to support international languages and regions. To eliminate the re-engineering or restructuring of I18N, every language or country must be supported in this platform. Changing the locale struts in Java is necessary to enable this functionality.
Instructions
-
-
1
Copy the following code and save it as your demo file:
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>
Move this file to F:\tomcat41\webapps and start your application with JDK 1.4 as your Java_Home. This will create your folder named "struts-blank." Rename the folder as "localedemo" and copy the JSP file on display above to your designated webapps folder.
-
2
Edit the property files for the various locales listed in your struts framework. These can be found in a file called "application.properties," which is located in the folder "F:\tomcat41\webapps\localedemo\web-inf\classes\resources." This is the only place to add your own property file and it must name in conjunction with the language code of the countries you are working on. Here are some language code examples.
German - de
Spanish - es
English - en
The German language, when written in I18N format, must be placed in a property file called "application_de.properties" and all of the property files associated with it must be placed in the resources folder only. Any language can be used because struts does not depend on any language, so Germany can be assigned to language code "en" for English instead of "de" for German. The property file, however, is dependent on the language settings of your browser, hence that's where most of the editing will take place.
-
-
3
Establish "two application.properties" files for Germany and Spain, as noted below:
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
You must also append this text in the application.properties file to this:
index.info=STRUTS TUTORIAL.
-
4
Add the following entries in the "struts-config.xml" file for all the property files of the countries you are working on:
<!-- Message Resources Definitions -->
<message-resources parameter="resources.application_es"/>
<message-resources parameter="resources.application_en"/>
<message-resources parameter="resources.application_de"/>
<message-resources parameter="resources.application"/>
Relaunch the Tomcat server and open your browser and type in "http://localhost:8080/localedemo/localedemo.jsp" as the URL. The message "ENGLAND" will appear because IE's default language is "United States English."
-
5
Open a new "Internet Explorer," go to its "Tools" menu and choose "Internet Options." Choose the "General" tab in the Internet Option dialog box, and click the "Languages..." button. When the "Language Preference" dialog box appears, click "Add..." button and include the languages for the countries listed on your I18N project.
-
6
Choose "German" from the list, or any other language you prefer, and use the "Move up" button to place it on the top of the list. Type the URL as "http://localhost:8080/localedemo/localedemo.jsp" to see the change you implemented on your browser. Your browser will now display the message "GERMAN" because you just made it your default language. From this point on, you can assign your language of choice for your Java struts project in your browser settings.
-
1
Tips & Warnings
Java has many free online tutorials to choose from to help you learn this efficient and advanced Web technology. You can also join forums to seek expert advice from experienced users of Java.
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images