Class LocaleProvider

java.lang.Object
net.simplace.core.service.LocaleProvider

public final class LocaleProvider extends Object
Singleton resource manager for localized strings, read from resource bundles on the class path (e.g. LocaleBundle). Optionally, bundles prefixed with DEV_ are looked up in an additional directory first (see setInterceptPath(URL)).
Author:
Andreas Enders
  • Field Details

    • DEFAULT_EMPTY

      public static final String DEFAULT_EMPTY
      Value returned if no localized string was found and no default value was given.
      See Also:
    • CUSTOMER_PREFIX

      public static final String CUSTOMER_PREFIX
      Prefix for customer specific resource bundles, e.g. CustomerLocaleBundle.
      See Also:
  • Method Details

    • getInstance

      public static LocaleProvider getInstance()
      Returns the singleton instance of LocaleProvider.
      Returns:
      the singleton instance of LocaleProvider
    • getInstance

      public static LocaleProvider getInstance(Locale aLocale)
      Returns the singleton instance of LocaleProvider after setting its current locale.
      Parameters:
      aLocale - locale to use from now on
      Returns:
      the singleton instance of LocaleProvider
    • getString

      public String getString(String aStringSet, String aKey)
      Returns a localized string using the current locale. If the key is not found in the given bundle, it is looked up in LocaleBundle; if the key contains spaces, it is also tried with spaces replaced by underscores. In development mode the key itself is returned if nothing was found.
      Parameters:
      aStringSet - name of the resource bundle
      aKey - key of the string
      Returns:
      the localized string, or DEFAULT_EMPTY if not found
    • getString

      public String getString(String aKey)
      Returns a localized string from LocaleBundle using the current locale.
      Parameters:
      aKey - key of the string
      Returns:
      the localized string, or DEFAULT_EMPTY if not found
    • getString

      public String getString(String aStringSet, String aKey, String aDefaultValue)
      Returns a localized string using the current locale, with a default value for missing keys. If the key is not found in the given bundle, it is looked up in LocaleBundle; if the key contains spaces, it is also tried with spaces replaced by underscores.
      Parameters:
      aStringSet - name of the resource bundle
      aKey - key of the string
      aDefaultValue - value to return if the key is not found
      Returns:
      the localized string, or aDefaultValue if not found
    • getString

      public String getString(String aStringSet, String aKey, Locale aLocale)
      Returns a localized string from the given bundle using the specified locale. No fallback bundle is used.
      Parameters:
      aStringSet - name of the resource bundle
      aKey - key of the string
      aLocale - locale to use
      Returns:
      the localized string, or DEFAULT_EMPTY if not found
    • getString

      public String getString(String aStringSet, String aKey, Locale aLocale, String aDefaultValue)
      Returns a localized string from the given bundle using the specified locale, with a default value for missing keys. No fallback bundle is used.
      Parameters:
      aStringSet - name of the resource bundle
      aKey - key of the string
      aLocale - locale to use
      aDefaultValue - value to return if the key is not found; if null, DEFAULT_EMPTY is returned instead
      Returns:
      the localized string, or the default value if not found
    • getLocale

      public Locale getLocale()
      Returns the locale currently in use.
      Returns:
      a copy of the current locale
    • setLocale

      public void setLocale(Locale aLocale)
      Sets the locale used by this provider.
      Parameters:
      aLocale - locale to use
    • setInterceptPath

      public void setInterceptPath(URL tInterceptPath)
      Sets a directory in which bundles prefixed with DEV_ are looked up before the class path.
      Parameters:
      tInterceptPath - URL of the directory, or null to use the class path only
    • resetLocale

      public void resetLocale()
      Resets the locale of this provider to the default locale of the JVM.
    • getLocaleKey

      public String getLocaleKey()
      Returns:
      the language code of the current locale, e.g. de
    • setDefaultLanguage

      public void setDefaultLanguage(String aLocalization)
      Sets the default locale of the JVM to the given language and resets this provider to the new default locale. A graphical client has to reinitialise its look and feel afterwards.
      Parameters:
      aLocalization - locale string; only the first two characters (language code) are used, e.g. de_DE