Class Localization

java.lang.Object
org.moeaframework.util.Localization

public class Localization extends Object
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 the getLocalization(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

The getString(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 Title
 
You 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 Details

    • getBundle

      public ResourceBundle getBundle()
      Returns the underlying resource bundle.
      Returns:
      the underlying resource bundle
    • getLocale

      public Locale getLocale()
      Returns the locale of this localization.
      Returns:
      the locale of this localization
    • getString

      public String getString(String key)
      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

      public boolean containsKey(String key)
      Returns true 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

      public String getString(String key, Object... arguments)
      Returns the localized string for the given key and formatting arguments. This method uses MessageFormat for formatting the arguments. If the key is not found, then this methods returns the key itself.
      Parameters:
      key - the key for the desired string
      arguments - the formatting arguments
      Returns:
      the localized string for the given key and formatting arguments
    • getLocalization

      public static Localization getLocalization(String packageName)
      Returns the localization object for the given package.
      Parameters:
      packageName - the name of the package
      Returns:
      the localization object for the given package
    • getLocalization

      public static Localization getLocalization(String packageName, Locale locale)
      Returns the localization object for the given package and locale.
      Parameters:
      packageName - the name of the package
      locale - the target locale
      Returns:
      the localization object for the given package
    • getLocalization

      public static Localization getLocalization(Class<?> type)
      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

      public static Localization getLocalization(Class<?> type, Locale locale)
      Returns the localization object for the given class and locale.
      Parameters:
      type - the class requesting the localization object
      locale - the target locale
      Returns:
      the localization object for the given class
    • containsKey

      public static boolean containsKey(Class<?> type, String key)
      Returns true 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 string
      key - the key (minus the class prefix)
      Returns:
      true if a localized string exists for the given key; false otherwise
    • getString

      public static String getString(Class<?> type, String key)
      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, calling Localization.getString(MainGUI.class, "title") returns the localized string for the key "MainGUI.title".
      Parameters:
      type - the class requesting the localized string
      key - the key (minus the class prefix)
      Returns:
      the localized string for the given key
    • getString

      public static String getString(Class<?> type, String key, Object... arguments)
      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, calling Localization.getString(MainGUI.class, "title") returns the localized string for the key "MainGUI.title".
      Parameters:
      type - the class requesting the localized string
      key - the key (minus the class prefix)
      arguments - the formatting arguments
      Returns:
      the localized string for the given key