Java Reference
In-Depth Information
Example 7−4: SimpleMenu.java (continued)
JMenu menu = SimpleMenu.create(bundle, "colors",
new String[] {"red", "green", "blue"},
listener);
// Finally add the menu to the GUI, and pop it up
menubar.add(menu);
// Add the menu to the menubar
f.setSize(300, 150);
// Set the window size.
f.setVisible(true);
// Pop the window up.
}
}
As I've already said, this example does not stand alone. It relies on resource bun-
dles to localize the menu. The following listing shows three property files that
serve as resource bundles for this example. Note that this single listing contains
the bodies of three separate files:
# The file Menus.properties is the default "Menus" resource bundle.
# As an American programmer, I made my own locale the default.
colors.label=Colors
colors.red.label=Red
colors.red.accelerator=alt R
colors.green.label=Green
colors.green.accelerator=alt G
colors.blue.label=Blue
colors.blue.accelerator=alt B
# This is the file Menus_en_GB.properties. It is the resource bundle for
# British English. Note that it overrides only a single resource definition
# and simply inherits the rest from the default (American) bundle.
colors.label=Colours
# This is the file Menus_fr.properties. It is the resource bundle for all
# French-speaking locales. It overrides most, but not all, of the resources
# in the default bundle.
colors.label=Couleurs
colors.red.label=Rouge
colors.green.label=Vert
colors.green.accelerator=control shift V
colors.blue.label=Bleu
Formatted Messages
We've seen that in order to internationalize programs, you must place all user-visi-
ble messages into resource bundles. This is straightforward when the text to be
localized consists of simple labels such as those on buttons and menu items. It is
trickier, however, with messages that are composed partially of static text and par-
tially of dynamic values. For example, a compiler might have to display a message
such as “Error at line 5 of file "hello.java"”, in which the line number and filename
are dynamic and locale-independent, while the rest of the message is static and
needs to be localized.
The MessageFormat class of the java.text package helps tremendously with these
types of messages. To use it, you store only the static parts of a message in the
ResourceBundle and include special characters that indicate where the dynamic
Search WWH ::




Custom Search