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:
- Create a ResourceBundle containing the strings to be localized. For example,
menu.file=File
menu.edit=Edit
- When creating the user interface, set any Strings which are to be localized to the keys defined
in the ResourceBundle:
menu.setText("menu.file");
- Add these Strings to the list of ones which should be localized:
WidgetLocalization.addLocalizedString("menu.file");
- Use WidgetEncoder to save the user interface to an XML file exactly as you
normally would.
- 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.
addLocalizedString
public static void addLocalizedString(String s)
Add a String object to the list of Strings which should be localized when the user interface
is reconstructed from XML.
getAllLocalizedStrings
public static String[] getAllLocalizedStrings()
Get the full list of String objects which should be localized when the user interface
is reconstructed from XML.
getLocalizedString
public static Object getLocalizedString(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.
isLocalizedString
public static boolean isLocalizedString(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.
removeLocalizedString
public static void removeLocalizedString(String s)
Remove a String object from the list of Strings which should be localized when the user interface
is reconstructed from XML.