buoy.xml
Class WidgetLocalization

java.lang.Object
  extended by buoy.xml.WidgetLocalization

public class WidgetLocalization
extends java.lang.Object

This class cooperates with WidgetEncoder and WidgetDecoder to localized the text stored in XML files. Rather than containing the actual text which will appear in the user interface, the XML file contains keys which are looked up from a ResourceBundle at decoding time. This class maintains a list of String objects which are to be localized at encoding time, and performs the actual substitution at decoding time.

To use this class, follow these steps:

  1. Create a ResourceBundle containing the strings to be localized. For example,

    menu.file=File
    menu.edit=Edit

  2. When creating the user interface, set any Strings which are to be localized to the keys defined in the ResourceBundle:

    menu.setText("menu.file");

  3. Add these Strings to the list of ones which should be localized:

    WidgetLocalization.addLocalizedString("menu.file");

  4. Use WidgetEncoder to save the user interface to an XML file exactly as you normally would.
  5. When loading the XML file, pass the ResourceBundle as an argument to WidgetDecoder's constructor. All the Strings which were marked as needing to be localized will automatically be replaced with values from the ResourceBundle.
The Strings to be localized are identified by object identity rather than value equality. This means that when you call addLocalizedString(), you must pass in the exact String object which is used in the user interface. It also means that it is possible for the same String value to appear twice in the user interface, and be localized in one place but not in the other.

This class can also operate in another mode from that described above. Suppose a graphical GUI editor application is used to create a user interface. That application defines the Strings to be localized, then uses WidgetEncoder to save it as XML. When that file is processed by WidgetDecoder to generate the user interface for an application, the localized Strings are obtained from a ResourceBundle.

Suppose, however, that you want to reload the XML file into the GUI editor application for further editing. In that case, load the XML file with WidgetDecoder, but use one of the constructors which does not take a ResourceBundle. This will cause the user interface to be loaded exactly as it originally was before encoding. The localization keys will be loaded directly, not replaced with localized versions. Furthermore, as they are loaded, they are automatically added to the list of Strings to localize so that when the file is saved again, all of the Strings will be properly localized.

Author:
Peter Eastman

Constructor Summary
WidgetLocalization()
           
 
Method Summary
static void addLocalizedString(java.lang.String s)
          Add a String object to the list of Strings which should be localized when the user interface is reconstructed from XML.
static java.lang.String[] getAllLocalizedStrings()
          Get the full list of String objects which should be localized when the user interface is reconstructed from XML.
static java.lang.Object getLocalizedString(java.lang.String key)
          This method is invoked during decoding to get the localized String corresponding to a key.
static boolean isLocalizedString(java.lang.String s)
          Determine whether a String object is currently in the list of Strings which should be localized when the user interface is reconstructed from XML.
static void removeLocalizedString(java.lang.String s)
          Remove a String object from the list of Strings which should be localized when the user interface is reconstructed from XML.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WidgetLocalization

public WidgetLocalization()
Method Detail

addLocalizedString

public static void addLocalizedString(java.lang.String s)
Add a String object to the list of Strings which should be localized when the user interface is reconstructed from XML.


removeLocalizedString

public static void removeLocalizedString(java.lang.String s)
Remove a String object from the list of Strings which should be localized when the user interface is reconstructed from XML.


isLocalizedString

public static boolean isLocalizedString(java.lang.String s)
Determine whether a String object is currently in the list of Strings which should be localized when the user interface is reconstructed from XML.


getAllLocalizedStrings

public static java.lang.String[] getAllLocalizedStrings()
Get the full list of String objects which should be localized when the user interface is reconstructed from XML.


getLocalizedString

public static java.lang.Object getLocalizedString(java.lang.String key)
This method is invoked during decoding to get the localized String corresponding to a key. It is intended for use by XMLDecoder, and you should not invoke it directly.



Written by Peter Eastman.