Previous | Table of Contents | Next |
The final major problem of internationalization is making your program flexible enough to display messages (or any type of user-visible text, such as the labels on GUI buttons) to the user in an appropriate language for the current locale. Typically, this means that the program cannot use hard-coded messages and must instead read in a set of messages at runtime, based on the locale setting. Java provides an easy way to do this. You define your messages as key/value pairs in a ResourceBundle subclass. Then, you create a subclass of ResourceBundle for each language or locale your application supports, naming each class following a convention that includes the locale name. At runtime, you can use the ResourceBundle.getBundle() method to load the appropriate ResourceBundle class for the current locale. The ResourceBundle contains the messages your application uses, each associated with a key, that serves as the message name. Using this technique, your application can look up a locale-dependent message translation based on a locale-independent message name. Note that this technique is useful for things other than textual messages. It can be used to internationalize icons, or any other locale-dependent object used by your application.
There is one final twist to this problem of internationalizing messages. Often, we want to output messages such as Error at line 5 of file hello.java. where parts of the message are static, and parts are dynamically generated at runtime (such as the line number and filename above). This is further complicated by the fact that when we translate such messages, the values we substitute in at runtime may appear in a different order. For example, in some different English, speaking locale, we might want to display the line above as: hello.java: error at line 5. The MessageFormat class of the java.text package allows you to substitute dynamic values into static messages in a very flexible way and helps tremendously with this situation, particularly when used in conjunction with resource bundles.
Object serialization is one of the major new features of Java 1.1. It refers to the ability to write the complete state of an object (including any objects it refers to) to an output stream, and then recreate that object at some later time by reading its serialized state from an input stream. You can serialize an object simply by passing it to the writeObject() method of an ObjectOutputStream. Similarly, you can create an object from a serialized object stream by calling the readObject() method of an ObjectInputStream. Both of these new object stream types are part of the java.io package.
Typically, object serialization is as simple as calling writeObject() and read\%Object(). There are a few additional twists, however, that are worth mentioning here. First, only objects that subclass the Serializable (or Externalizable) interface can be serialized. The Serializable interface does not define any methods, but merely acts as a marker that indicates whether serialization is allowed on a given object. Second, fields of a class declared transient are not serialized as part of an objects state. The transient modifier was legal in Java 1.0, but had no defined behavior. Third, some objects may need to implement custom serialization or de-serialization behavior. They can do this by implementing special readObject() and writeObject() methods.
Despite the fact that only a few classes and interfaces are part of the Object Serialization API, serialization is a very important technology and is used in several places in Java 1.1. It is used as the basis for transferring objects via cut-and-paste. It is used to transfer objects between a client and a server for remote method invocation. It is used by the JavaBeans API; beans are often provided as pre-initialized, serialized objects, rather than merely as class files. Java 1.1 also adds the capability for applets to be loaded into an applet viewer or browser as serialized objects. One common use we are likely to see for object serialization is as a way to save user preferences and other application statea serialized object is an instant file format that works for any application. Another use that should be popular with GUI builder tools is saving the complete Component hierarchy of an applications GUI as a serialized object, and then later loading in that object in order to automatically recreate the GUI.
Previous | Table of Contents | Next |