advertisement

Search Tips
Articles  |   Tutorials  |   Reviews  |   Dev Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source Code  |  

Browse DevX
DevX Updates - Sign Up Here


Partners & Affiliates












advertisement

Articles : Advanced Topics in Java :
Internationalization :

Contents
Introduction
Naming Resource Bundles
Creating Resource Bundles by Direct Subclassing
Using ListResourceBundle
Using PropertyResourceBundle
Compiling Resource Bundles

Creating Resource Bundles by Direct Subclassing

All ResourceBundle subclasses (the class you write, ListResourceBundle, PropertyResourceBundle all included) provide access methods to retrieve the locale-sensitive objects stored within them. Unique keys that are defined in your program identify the objects stored. e.g. the message in a particular error dialog box could be identified as "errmsg1" The resource bundles for different locales will now hold an object corresponding to this "errmsg1" key.

The most basic access method is the following:

public final Object getObject(String key)

This method returns the object associated with the key. Because the return value is simply an Object, you may have to cast it to a more specific object type.

Lets consider the rather hypothetical and extremely simple example where we have two buttons with the text "Ok" and "Cancel" displayed on them that need to be localized for the German language and UK country as locales. We identify the text with 2 keys okKey and cancelKey respectively and subclass ResourceBundle

The base version will look something like:

abstract class MyProgramResource extends ResourceBundle {
   public Object handleGetObject(String key) {
      if (key.equals("okKey")) return "Ok";
      if (key.equals("cancelKey")) return "Cancel";
      return null;
   }
}

The German version will look like :

abstract class MyProgramResource _de extends MyResources {
   public Object handleGetObject(String key) {
      if (key.equals("okKey")) return "Gut";
      if (key.equals("cancelKey")) return "Vernichten";
      return null;
   }
}

The UK version will look like:

abstract class MyProgramResource _uk extends MyResources {
   public Object handleGetObject(String key) {
      // don't need okKey, since parent level handles it.
      if (key.equals("cancelKey")) return "Dispose";
      return null;
   }
}

Instead of :

String labelOK = "Ok";
String labelCANCEL = "Cancel";
labelOk.setBackground(Color.Red);

We would need to write code that is similar to:

ResourceBundle res = ResourceBundle.getBundle("MyProgramResource ");
String labelOK = (String) res.getObject("okKey");
String labelCANCEL = (String) res.getObject("cancelKey");
labelOk.setBackground((Color)rb.getObject("stopColor"));

Casting the returned object is easy, but ResourceBundle provides a few convenience methods for returning common object types. Some examples follow:

public final String getString(String key)
public final String[] getStringArray(String key)

These methods are convenient, but they don't do anything complex. If they save you some of the time or trouble necessary to cast the returned value, try them.

The java.util.ResourceBundle is an abstract class with 2 abstract methods.

protected abstract Object handleGetObject(String key) throws
MissingResourceException
public abstract Enumeration getKeys()

If both methods are not implemented in your class it should use the abstract modifier for compilation to succeed.

NEXT


Sameer Tyagi is a Software Engineer with several years of programming experience in iNet application development and has conducted multiple training workshops in Java. Besides holding an Engineering degree in Electronics he is a Sun Certified Java 1.1 Programmer.
Email: sameertyagi@usa.net

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

internet.com logo


JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.
Blackmail Applet
This applet prints out text that looks like letters cut out of a newspaper. You can specify the text, the width and height of the applet and the delay after each letter. Future versions will have more parameters.
JB User Poll
What is your primary tool for learning Java?
The JavaBoutique Top 15:
1. Little_Wizard
2. articles.rdf
3. applets.rdf
4. Birdbrain
5. 3DPhotoCube
6. quickserv
7. 3DMaze
8. PacMan
9. ZTBanner
10. PingPong
11.
12. Pool
13. Viewer
14. AnimLetters_anim
15. AScroll2
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Google IM Not Talking to Other Jabbers
Newly-launched Google Talk IM service doesn't connect to other open source Jabber servers and users -- at least not yet.

Printer Sets Good Example for Small Business Security
While surveys suggest that small businesses aren't prepared for ever-present security dangers, this Boston-area printer has maintained a pristine operation since a scare six years ago.

VoIP Gizmo Comes to Universities
As student gear up for back to school, a SIP-based initiative aims to VoIP-enable universities around the world for free calling.


Access FREE BIRT Developer Tools from Actuate:
Download:
Visual Report Development Tool
Download:
Spreadsheet Report Writer
Webinar:
Point-and-click, Visual Report Development


Copyright 2002 INT Media Group, Incorporated. All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/