Java Reference
In-Depth Information
Creating a Button with I18N Resources
Problem
You want your program to take “sensitivity lessons” so that it can communicate well interna-
tionally.
Solution
Your program must obtain all control and message strings via the internationalization soft-
ware. Here's how:
1. Get a ResourceBundle .
ResourceBundle rb = ResourceBundle.getBundle("Menus");
I'll talk about ResourceBundle in Creating a Resource Bundle , but briefly, a Re-
sourceBundle represents a collection of name-value pairs (resources). The names are
names you assign to each GUI control or other user interface text, and the values are
the text to assign to each control in a given language.
2. Use this ResourceBundle to fetch the localized version of each control name.
Old way:
somePanel.add(new JButton("Exit"));
New way:
try { label = rb.getString("exit.label"); }
catch (MissingResourceException e) { label="Exit"; } // fallback
somePanel.add(new JButton(label));
This is quite a bit of code for one button, but distributed over all the widgets (buttons, menus,
etc.) in a program, it can be as little as one line with the use of convenience routines , which
I'll show in Writing Internationalization Convenience Routines .
Search WWH ::




Custom Search