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(String)
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 boolean
containsKey
(Class<?> type, String key) Returnstrue
if a localized string exists for the given key;false
otherwise.boolean
containsKey
(String key) Returnstrue
if a localized string exists for the given key;false
otherwise.Returns the underlying resource bundle.Returns the locale of this localization.static Localization
getLocalization
(Class<?> type) Returns the localization object for the given class.static Localization
getLocalization
(Class<?> type, Locale locale) Returns the localization object for the given class and locale.static Localization
getLocalization
(String packageName) Returns the localization object for the given package.static Localization
getLocalization
(String packageName, Locale locale) Returns the localization object for the given package and locale.static String
Returns the localized string for the given key.static String
Returns 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
-
containsKey
Returnstrue
if a localized string exists for the given key;false
otherwise.- Parameters:
key
- the key for the desired string- Returns:
true
if a localized string exists for the given key;false
otherwise
-
getString
Returns the localized string for the given key and formatting arguments. This method usesMessageFormat
for 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 package.- Parameters:
packageName
- the name of the package- Returns:
- the localization object for the given package
-
getLocalization
Returns the localization object for the given package and locale.- Parameters:
packageName
- the name of the packagelocale
- the target locale- Returns:
- the localization object for the given package
-
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
Returnstrue
if a localized string exists for the given key;false
otherwise. 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:
true
if a localized string exists for the given key;false
otherwise
-
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
-