public class I18n
extends java.lang.Object
Java's ResourceBundles are intended to do this, but they don't provide the level of support that most other libraries do.
For example, suppose you have a "Copy" menuitem.
JMenuItem copy = new JMenuItem("Copy");Of course, you should internationalize this. But you want to change the text, the mnemonic, and the keyboard accelerator. If you only had ResourceBundle to work with, you might end up with 3 lines of translation for every word in your program. Plus, you'd have to know the name of the I18n file you used, each time you wanted to use it. Most other libraries (like Powerplant on Mac and Win95 resources) let you put them all in one line, which makes the translator's job much easier. That's what this class does.
Now, all you have to say is:
copy = &Copy [accel C]
There are several things going on here:
KeyStroke.getKeyStroke(I18n.getKeyStroke("copy"))to get a Swing KeyStroke object.
This is convenient, but it gets even better: normally, you don't even have to mess with keystrokes and mnemonics. You can simply use the Builder factory to do the dirty work for you:
JMenuItem copy = Builder.makeMenuItem("copy");Of course, if you just want the text (for making a printout, for example), you should still use I18n.getText().
Not all keys must have a keystroke, or a mnemonic. Note that those methods can return nulls.
These methods get their values from the resource bundle called "TextBundle". That is, the file is called "TextBundle.properties", or some variant, like "TextBundle_de_DE.properties".
ResourceBundle
Modifier and Type | Class and Description |
---|---|
static class |
I18n.FHAESLocale
Locale enum for supported country/languages
|
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
log |
private static java.util.ResourceBundle |
msg |
Modifier | Constructor and Description |
---|---|
private |
I18n() |
Modifier and Type | Method and Description |
---|---|
static I18n.FHAESLocale |
getDefaultFHAESLocale(java.lang.Boolean fallback)
Get the default locale for the system.
|
static javax.swing.KeyStroke |
getKeyStroke(java.lang.String key)
Get the keystroke string for this key.
|
static java.lang.Integer |
getMnemonic(java.lang.String key)
Get the mnemonic character this key.
|
static java.lang.Integer |
getMnemonicPosition(java.lang.String key)
Get the position of the mnemonic character in the string Used for setDisplayedMnemonicIndex
|
static I18n.FHAESLocale |
getPreferredFHAESLocale(java.lang.Boolean fallback)
Get the FHAESLocale from the preferences, or if none is specified get the default.
|
static I18n.FHAESLocale |
getTellervoLocale(java.lang.String country,
java.lang.String language)
Get the FHAESLocale using the country and language codes.
|
static java.lang.String |
getText(java.lang.String key)
Get the text for this key.
|
static java.lang.String |
getText(java.lang.String argKey,
java.lang.String... argReplacing)
Easier way of using an arbitrary number of replacing strings.
|
private static final org.slf4j.Logger log
private static final java.util.ResourceBundle msg
public static java.lang.String getText(java.lang.String key)
For example, if the localization file has the line copy = &Copy [accel C]
, the string "Copy" is returned.
key
- the key to look up in the localization filepublic static javax.swing.KeyStroke getKeyStroke(java.lang.String key)
For example, if the localization file has the line copy = &Copy [accel C]
, the string "control C" is returned (or on
the Mac, "meta C").
If the string has no [keystroke] listed, null is returned.
key
- the key to look up in the localization filepublic static java.lang.Integer getMnemonic(java.lang.String key)
For example, if the localization file has the line copy = &Copy [accel C]
, the character "C" is returned.
If the string has no &mnemonic listed, null is returned.
key
- the key to look up in the localization filepublic static java.lang.Integer getMnemonicPosition(java.lang.String key)
key
- public static java.lang.String getText(java.lang.String argKey, java.lang.String... argReplacing)
argKey
- argReplacing
- public static I18n.FHAESLocale getTellervoLocale(java.lang.String country, java.lang.String language)
country
- language
- public static I18n.FHAESLocale getDefaultFHAESLocale(java.lang.Boolean fallback)
fallback
- public static I18n.FHAESLocale getPreferredFHAESLocale(java.lang.Boolean fallback)
fallback
- - if true then return default Locale if all else fails