Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Internationalize JSP-based Websites - JavaWorld March 2000

Internationalize JSP-based Websites - JavaWorld March 2000

Tutorial Details:

Internationalize JSP-based Websites
Internationalize JSP-based Websites
By: By Govind Seshadri
Build Websites that speak in tongues
ypically, most Websites you come across are customized for a specific locale, with the locale defined as a geographical region or political entity that shares a common language, culture, and customs. While this may not be a significant issue to most people, it may pose some interesting challenges to those conducting business on the Web. By its very nature, ecommerce is all about facilitating commerce across national, linguistic, and cultural boundaries. While the Web may have opened up businesses to a truly international clientele, Web-based firms have to contend with the all-too-probable scenario of non-English-speaking users struggling to understand their sites' content. As many businesses have come to realize, every Web surfer who turns away because of the site's English-centric nature is a potential customer lost.
So, is there an easy, manageable solution to this problem that keeps you from having to run multiple versions of the site? Well, if the dynamic content for the Website is generated through Java technology, there sure is! That's because the Java platform intrinsically supports the development of locale-independent applications through various classes supporting internationalization found within the java.util , java.io , and java.text packages. Internationalization itself is the name given to the design process wherein an application's country, language, and culture-specific information is isolated and typically encapsulated within some external files or objects. Consequently, making your application compatible with an entirely new language or country does not have to involve a major rewrite of your presentation logic; rather, it now merely involves creating an additional version, specific to the new locale, of the external files. The process of creating these locale-specific entities (be they files or objects), along with the associated translation of text, is called localization .
Websites based on JavaServer Pages (JSPs) lend themselves more easily to internationalization than those developed using servlets. This is because the technology facilitates the separation of presentation from application logic. Now, let us examine this premise in detail by developing a global version of the Music Without Borders online store that was demonstrated in my earlier article on JSP architecture (see the Resources section below for a link to this story). The online store is based on the Model-View-Controller (MVC) architecture to maximize the separation of presentation from content.
Although internationalizing a JSP-based Web application is not particularly difficult, it is a task best initiated at the design stage. Toward this end, all locale-specific textual data that constitute an application's user interface, such as page headers and trailers, user messages, form button labels, dates, currencies, and so forth, are first identified and isolated from the application within external files. These entities, which hold different values under different locales, are also known as resources; a grouping of resources within an external class or property file is known as a resource bundle or, simply, a bundle.
The Java 2 SDK provides the abstract base class java.util.ResourceBundle for handling resources, along with its subclasses ListResourceBundle and PropertyResourceBundle . The bundle for a supported locale can be defined as either a classfile (by using ListResourceBundle ) or as a property file (by using PropertyResourceBundle ). Both these mechanisms then allow your application to access the values for resources by name, just as you would with a Hashtable object. Although you can store objects of any type as values for resources using classfiles, most Web applications use property files, since they almost always customize only string types. Consequently, I will focus my discussion on the use of property files.
Developing global Web applications
In this example, the Music Without Borders online store has been localized to support users not only in the US, but also in Germany, Sweden, and France. Listings 1 and 2 show the property files supporting the locales for the US and France. (You can obtain the property files for Germany and Sweden when you download the source code for the example in Resources .) As you can see, the property files essentially isolate all of the GUI elements from the JSP pages. The property files for bundles must follow some standard naming conventions. The suffix must always be properties . Also, since a locale is typically defined in Java via a country code and/or a language code and an optional variant, the name of the bundle has to correlate with the locale it serves. For instance, all of the following bundle prefixes are valid:
BundleName + "_" + localeLanguage + "_" + localeCountry + "_" + localeVariant
BundleName + "_" + localeLanguage + "_" + localeCountry
BundleName + "_" + localeLanguage
BundleName
Observe that the property file serving as the bundle for France (shown in Listing 2) was named Message_fr_FR.properties . Bundles pertaining to a specific country need to have both the language code and country code appear within the prefix. (I could also have used the optional application-specific variant and further specialized the file as Message_fr_FR_MAC.properties -- the bundle for all French-speaking Mac users from France. But I shall resist the temptation!) Similarly, the bundle containing the German labels is named Message_de_DE.properties , and so on. Resources contains links for obtaining valid country and language codes. For the overly curious, the following code shows the source you can use to create a classfile, serving the same purpose as the property file shown in Listing 2:
public class Message_fr_FR extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"main.title", "Musique sans frontières"},
{"main.subhead", "Sons du village global"},
{"main.addLabel","Ajouter"},
. . .
{"cd.quantityLabel","Quantité"}
}
}
You might wonder why the property file containing the English labels (shown in Listing 1) does not contain a country or language code in its prefix. Although I could have named the bundle Message_en_US.properties , I chose to simply call it Message.properties . Any bundle that contains neither country nor language code is treated as the default bundle and serves a specific purpose: it is picked up as a last resort if there is no other matching bundle for the requested locale. Observe that the literals used to identify resources remain the same within the bundles for all locales; only the values change for the corresponding locale. Now that I have isolated the GUI elements and localized them within the property files, I'll show how you can use them within a JSP page.
Listing 1: Message.properties
main.title=Music Without Borders
main.subhead=Sounds from the Global Village
main.addLabel=Add
main.qtyLabel=Quantity
main.bgcolor=#33CCFF
cart.bgcolor=#FFFFFF
cart.delLabel=Delete
cart.checkoutLabel=Checkout
checkout.bgcolor=#33CCFF
checkout.title=Music Without Borders Checkout
checkout.subhead=Thanks for your order!
checkout.totalLabel=Total
checkout.returnLabel=Shop some more!
dollar.exchRate=1.00
cd.albumLabel=Album
cd.artistLabel=Artist
cd.countryLabel=Country
cd.priceLabel=Price
cd.quantityLabel=Quantity
Listing 2: Message_fr_FR.properties
main.title=Musique sans frontières
main.subhead=Sons du village global
main.addLabel=Ajouter
main.qtyLabel=Quantité
main.bgcolor=#33CCFF
cart.bgcolor=#FFFFFF
cart.delLabel=Supprimer
cart.checkoutLabel=Passez à la caisse
checkout.bgcolor=#33CCFF
checkout.title= Caisse pour Musique sans frontières
checkout.subhead=Merci pour votre commande!
checkout.totalLabel=Total
checkout.returnLabel=Faites d'autres commandes!
dollar.exchRate=6.48
cd.albumLabel=Album
cd.artistLabel=Artiste
cd.countryLabel=Pays
cd.priceLabel=Prix
cd.quantityLabel=Quantité
The JSP page i18nDemo.jsp (shown in Listing 3) acts as the gateway to the online store. Its main task is to enable the user to select an appropriate language for viewing the site, as shown in Figure 1.
Figure 1. Gateway to the internationalized Web application
In Java, the focal point of all internationalization activity is the java.util.Locale class, which serves to encapsulate the country code, language code, and the optional variant code. The Locale class also provides a number of convenient constants representing the most commonly used locales. Consider the following snippet, which establishes the locale for the Web application:
Locale locale=null;
if (lang.equals("German")) {
locale=Locale.GERMANY;
} else if (lang.equals("French")) {
locale=Locale.FRANCE;
} else if (lang.equals("Swedish")) {
locale=new Locale("sv","SE");
} else {
locale=Locale.US;
}
While the Locale instance is typically obtained from a predefined constant within the Locale class, it can also be instantiated using the class's constructor. You can do this by specifying the language code and country code (there is also another version of the Locale constructor that additionally accepts an application-specific variant code). The language code is two lowercase letters, and the country code is always two uppercase letters. (As mentioned earlier, you can find a link to lists of valid country and language code in Resources .)
Since I do not have a predefined constant for Sweden, observe that its Locale instance is instantiated by explicitly passing the language and country code as:
locale=new Locale("sv","SE");
Once the locale has been established, you can retrieve its corresponding bundle:
ResourceBundle bundle =
ResourceBundle.getBundle("Message", locale);
You can store the bundles anywhere in the classpath, as understood by your application server or J


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Internationalize JSP-based Websites - JavaWorld March 2000

View Tutorial:
Internationalize JSP-based Websites - JavaWorld March 2000

Related Tutorials:

Cache SOAP services on the client side
Cache SOAP services on the client side
 
A recipe for cookie management
A recipe for cookie management
 
A first look at JavaServer Faces, Part I
A first look at JavaServer Faces, Part Learn how to implement Web-based user interfaces with JSF
 
A first look at JavaServer Faces, Part 2
A first look at JavaServer Faces, Part 2
 
Call JavaBean methods from JSP
Call JavaBean methods from JSP 2.0 pages
 
Improving JSF by Dumping JSP
Improving JSF by Dumping JSP After a long wait and high expectations, JavaServer Faces (JSF) 1.0 was finally released on March 11, 2004. JSF introduces an event-driven component model for web application development, similar in spirit and function to t
 
Eclipse 3.0 is out
Eclipse 3.0 is out Welcome to eclipse.org Eclipse is a kind of universal tool platform - an open extensible IDE for anything and nothing in particular.
 
Software Download Central compatible.
GNU getopt - Java port A while back I found myself in need of a Java command line option parser. Unsatisfied with free versions I was able to find on the net, I volunteered to port the GNU getopt family of functions from C to Java. The current release
 
Access Windows Performance Monitor counters from Java, Part 1
Access Windows Performance Monitor counters from Java, Part 1 Use a simple Java API to gather valuable performance statistics Summary Windows NT, 2000, 2003, and XP contain a utility called the Performance Monitor that provides a rich array of perform
 
JTimepiece
JTimepiece is the advanced library for working with dates and times in Java. Many easy-to-use methods in this API make it easy for any developer, from beginner to expert, to use JTimepiece.
 
Java Servlets: Design Issues
This article covers the principal concepts associated with servlets. This article examines some of the design issues, and offers some guidelines on the applicability of Java servlets for web based application development.
 
Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three.
 
Streaming QuickTime with Java
This isn't an excerpt from my soon-to-be-released QuickTime for Java book, though now I wish it was.
 
istory of Bioinformatics
istory of Bioinformatics History of Bioinformatics The Modern bioinformatics is can be classified into two broad categories, Bi ological Science and computational Science . Here is the data of hi storical events for both biology and computer
 

Free Web Site Hosting Services Below is the listing of the hosting providers providing free web hosting services. These services helps you building your sites even if you have no experience in HTML writing. Zero
 
What is Web Hosting
What is Web Hosting What is Web Hosting? What is Web Hosting? If you have a company and want web presence than you need a website. With the website any one from the world must be able to view your pages, images etc. Website is actually a
 
Manual Submission to Search Engines. Hand Submit Website URL Submission to Major Search Engines
Manual Submission to Search Engines. Hand Submit Website URL Submission to Major Search Engines ATTENTION: Website owners! Hand Submissions to major search engines for as low as $10.00 per month Let us introduce you to the services provided by
 
New Technical Articles: 64-bit Programming on Solaris 10 OS for x86 Platforms
Four technical articles describe the new Sun Studio 10 software's 64-bit programming features on the Solaris 10 OS for x86 and AMD64 platforms. Important issues regarding the AMD64 ABI (Application Binary Interface), debugging, migration to 64-bits, and p
 
Chat Transcript: Java Web Services Developer Pack (Java WSDP) 1.5
Learn about the exciting new web services features in the recently-released Java WSDP 1.5.
 
Solaris 10 OS Certification Beta Exams
If you are an expert in system and network administration, you can get involved in the creation of three new Solaris 10 certification exams. These Beta exams count toward official Solaris Certification and allow you to provide comments and technical feedb
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.