The sample application uses resource bundles to provide I18N support. The resource bundles files, such as the ArticlesGUI_ja_JP.properties
file mentioned earlier, contain specific values for the response based on the client's language and the requesting user agent.For example, the initial Options page of the application displays different titles based on the user agent, one each for Web, WAP, and iMode clients. Therefore, the options page ResourceBundle file contains three different "_Options_Page_Heading
" propertiesone each for the Web, WAP and iMode user agents. Here's an excerpt from the ArticlesGUI_ja_JP.properties
|Author Note: The symbol "\u65e5\u672c\" in the following code is the Unicode value of the two Japanese characters used in the sample application.
// Options Page
IMode_Options_Page_Heading=\u65e5\u672c\Demo -- iMode
The following code segment fetches the appropriate page heading for the given user agent by adding a prefix to the value of the pageType
variable defined in the JSP file. Remember that the JSP page set the pageType
variable to a value of "Wap", ":Web" or "iMode", and passed the variable to the XML generator class.
This scheme lets you generate XML pages containing values appropriate for the targeted user agent. The resulting XML document, which is generic, and common to all requests, is subsequently translated into user agent specific format during the XSL transform. Separating the page generation logic in this manner gives you the flexibility to provide different values for different user agents.
Adding Support for Additional User Agents and Languages
As you can see, the architecture makes it relatively easy to add support for other user agents and languages anytime. Here are the steps you would need to take to extend the application to support an additional language or user agent..
Adding support for additional user agents
Adding support additional support for additional languages
- Modify the JSP file to check for the new user agent
- Create a new value for the pageType variable that identifies the new user agent, and set that value in the JSP file. For example, if you wanted to add support for VoiceXML browsers, then the value of pageType variable might be "VOICE".
- Create a new XSL stylesheet that corresponds to the new user agent.
- Add new key value entries in the Resource bundles files that correspond to the new user agent.
Create a new resource bundle file corresponding to the targeted language and country. For example, to provide support for the Korean language and country, you would create a file named ArticlesGUI_ko_KO.properties
and write the corresponding entries in Unicode format for all keys. The starting URL for accessing this application in the Korean language will would be:
Dealing with Images and Dates
You can even use Resource Bundles to provide language and user agent-specific images. For example:
// Locating user agent specific images using resource bundles
Note that you use the same pageType
variable and locale objects to fetch the appropriate image. For internationalized Date, Numbers and Currency use the java.text
package. The following code demonstrates how to retrieve a locale-specific date.
// For Japanese language and Japan Country date
Locale locale = new Locale("ja","JP");
Date today = new Date();
Creating date as per locale
dateFormatter = DateFormat.getDateInstance(DateFormat.FULL,locale);
You've seen some powerful techniques for handling and displaying internationalized, platform-agnostic content Using these techniques, you can design applications using J2EE, XML and XLST that support any number of client types and languages, and make your applications not only truly global, but also extensible and adaptable for as-yet-unknown client types. You could further improve the design of this application by moving the process to add support for additional user agents and languages from JSP to a Java class. You could even write an intelligent java class to handle the showPage() method processing. The possibilities are endless!
See the sidebar: Products Used to Test and Develop the Sample Application
for a complete list of the technology used to create the sample code for this article.
: Many thanks to Mr. Saurabh Bhatnagar for his valuable contributions as technical reviewer for this article.