Package org.moeaframework.util
Class Localization
java.lang.Object
org.moeaframework.util.Localization
Facilitates internalization (i18n) and localization (l10n) of strings. Locale-specific strings are stored in a
single file in each package named
LocalStrings.properties, LocalStrings_{locale}.properties or any
other format supported by ResourceBundle. This class supports two modes of localization:
Package-specific
This is useful when classes in a package share common resources. All classes have access to all stored resources. Use thegetLocalization(Class) method to create instances of the Localization class for a specific
package. Here, you use the getString(String) and getString(String, Object...) methods on the
Localization instance you previously created.
Class-specific
ThegetString(Class, String) and getString(Class, String, Object...) static methods do not require
you to explicitly create a new Localization instance. In addition, the keys are automatically prefixed with
the class name. For example, if your property file contains the following lines:
WindowA.title = First Title WindowB.title = Second TitleYou can read each entry with
Localization.getString(WindowA.class, "title") and
Localization.getString(WindowB.class, "title"). This is convenient for providing localization in subclasses
by using the getClass() method.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsKey(Class<?> type, String key) Returnstrueif a localized string exists for the given key;falseotherwise.booleancontainsKey(String key) Returnstrueif a localized string exists for the given key;falseotherwise.Returns the underlying resource bundle.Returns the icon defined by the given key.Returns the locale of this localization.static LocalizationgetLocalization(Class<?> type) Returns the localization object for the given class.static LocalizationgetLocalization(Class<?> type, Locale locale) Returns the localization object for the given class and locale.static StringReturns the localized string for the given key.static StringReturns the localized string for the given key and formatting arguments.Returns the localized string for the given key.Returns the localized string for the given key and formatting arguments.
-
Method Details
-
getBundle
Returns the underlying resource bundle.- Returns:
- the underlying resource bundle
-
getLocale
Returns the locale of this localization.- Returns:
- the locale of this localization
-
getString
Returns the localized string for the given key. If the key is not found, then this methods returns the key itself.- Parameters:
key- the key for the desired string- Returns:
- the localized string for the given key
-
getIcon
Returns the icon defined by the given key. If the key is not found, thennullis returned.- Parameters:
key- the key for the desired icon- Returns:
- the icon
-
containsKey
Returnstrueif a localized string exists for the given key;falseotherwise.- Parameters:
key- the key for the desired string- Returns:
trueif a localized string exists for the given key;falseotherwise
-
getString
Returns the localized string for the given key and formatting arguments. This method usesMessageFormatfor formatting the arguments. If the key is not found, then this methods returns the key itself.- Parameters:
key- the key for the desired stringarguments- the formatting arguments- Returns:
- the localized string for the given key and formatting arguments
-
getLocalization
Returns the localization object for the given class.- Parameters:
type- the class requesting the localization object- Returns:
- the localization object for the given class
-
getLocalization
Returns the localization object for the given class and locale.- Parameters:
type- the class requesting the localization objectlocale- the target locale- Returns:
- the localization object for the given class
-
containsKey
Returnstrueif a localized string exists for the given key;falseotherwise. This method automatically finds the correct resource bundle and key prefix appropriate for the class.- Parameters:
type- the class requesting the localized stringkey- the key (minus the class prefix)- Returns:
trueif a localized string exists for the given key;falseotherwise
-
getString
Returns the localized string for the given key. This method automatically finds the correct resource bundle and key prefix appropriate for the class. For example, callingLocalization.getString(MainGUI.class, "title")returns the localized string for the key"MainGUI.title".- Parameters:
type- the class requesting the localized stringkey- the key (minus the class prefix)- Returns:
- the localized string for the given key
-
getString
Returns the localized string for the given key and formatting arguments. This method automatically finds the correct resource bundle and key prefix appropriate for the class. For example, callingLocalization.getString(MainGUI.class, "title")returns the localized string for the key"MainGUI.title".- Parameters:
type- the class requesting the localized stringkey- the key (minus the class prefix)arguments- the formatting arguments- Returns:
- the localized string for the given key
-