Locale-Sensitive Java Method
javax.swing.AbstractButton
public void setMnemonic(int mnemonic)
public void setMnemonic(char mnemonic)
Internationalization (I18n) Method Overview
This AbstractButton method sets the keyboard mnemonic, which is a
keyboard shortcut that the user can press (usually in conjunction with the
Alt key) to activate the button.
The mnemonic is usually a letter associated with the label of the button itself,
such as 'S' for "Submit".
The mnemonic argument passed into this method is either the int
VK_XXX keycode constant, which is defined in
java.awt.event.KeyEvent or a char literal that represents the
mnemonic.
I18n Issues
The mnemonic key is generally the same as one of the first letters
in the button label. Once the application text is localized for several
languages and the correct translation pulled in at runtime, the
original letter is often not a mnemonic for the new translations.
For example, the Alt+S shortcut
would need to be altered for a Japanese user who instead of seeing a "Submit" button, sees
a "堤出" button. Good I18n practice dictates that the mnemonic value
be set dynamically according to the user's locale.
Globalyzer will detect this call and report it as an I18n issue
regardless of whether mnemonic is being retrieved from a localized
properties file. If you have determined
that the call is being handled correctly, you can use Globalyzer's
Ignore Comment
functionality to ensure that it isn't picked up in a subsequent scan.
Suggested Replacement
public void setMnemonic(char mnemonic)
Instead of:
JButton button = new JButton("Submit");
button.setMnemonic('S');
Use:
ResourceBundle bundle =
ResourceBundle.getBundle(
"myBundle", locale);
JButton button = new JButton(bundle.get("submit_key"));
String mnemonic = bundle.get("mnemonic_key");
button.setMnemonic(mnemonic.charAt(0));
Locale-Sensitive Java Methods
|