Class LocaleProvider

java.lang.Object
org.tentackle.common.LocaleProvider

@Service(LocaleProvider.class) public class LocaleProvider extends Object
A provider for Locales.
Supports thread-local locales and mapping of locales according to the application requirements.
Author:
harald
  • Constructor Details

    • LocaleProvider

      public LocaleProvider()
      Creates a locale provider.
  • Method Details

    • getInstance

      public static LocaleProvider getInstance()
      The singleton.
      Returns:
      the singleton
    • getInitialLocale

      public Locale getInitialLocale()
      Gets the initial locale on startup of the JVM.
      Returns:
      the initial locale
    • getEffectiveInitialLocale

      public Locale getEffectiveInitialLocale()
      Gets the effective initial locale on startup of the JVM.
      Returns:
      the effective initial locale
    • getCurrentLocale

      public Locale getCurrentLocale()
      Gets the locale used by the current thread.
      Returns:
      the locale, null if no thread-local Locale set
    • getLocale

      public Locale getLocale()
      Gets the locale.

      If there is no thread-local Locale, the default Locale is returned.

      Returns:
      the current locale, never null
    • setCurrentLocale

      public void setCurrentLocale(Locale locale)
      Sets the locale used by the current thread.
      The locale is stored as ThreadLocal.
      Parameters:
      locale - the locale
    • assertCurrentLocaleValid

      public void assertCurrentLocaleValid()
      Asserts that a thread-local Locale is set.
      Throws:
      TentackleRuntimeException - if thread-local locale is null
    • getEffectiveLocale

      public Locale getEffectiveLocale(Locale locale)
      Gets the effective locale.
      This is application-specific and allows narrowing the locales to the effectively supported locales. If, for example, the default language in all resource bundles is "en" and there is a second language "de", then all locales beginning with "de" will map to "de" and all others to "en".
      Parameters:
      locale - the requested locale
      Returns:
      the mapped locale
    • getEffectiveLocale

      public Locale getEffectiveLocale()
      Gets the effective locale for the current locale.
      Returns:
      the mapped locale
    • getFallbackLocale

      public Locale getFallbackLocale()
      Returns the fallback locale.
      If there is no translation provided for a given locale, the resource bundles eventually fall back to the "no-locale" translation.
      In most cases this corresponds to the "en"-locale (Locale.ENGLISH), but applications can change this by overriding this method.

      Important: the fallback-locale must be language-only! No country or variant!

      Returns:
      the fallback locale
    • getEffectiveLocales

      public List<Locale> getEffectiveLocales()
      Returns an unmodifiable list of all supported effective locales.
      Returns:
      the possible effective locales
    • isLocaleSupported

      public boolean isLocaleSupported(Locale locale)
      Returns whether given locale is covered by the effective locales.
      Parameters:
      locale - the locale
      Returns:
      true if locale would map to a valid effective locale
    • toTag

      public String toTag(Locale locale)
      Converts a locale to a tag.
      Since there is no guarantee that Locale.toLanguageTag() and Locale.forLanguageTag(String) will round-trip, this method is provided to guarantee that at least for the supported locales.
      Parameters:
      locale - the locale
      Returns:
      the tag
      See Also:
    • fromTag

      public Locale fromTag(String tag)
      Converts a tag to a supported locale.
      Parameters:
      tag - the tag
      Returns:
      the locale, null if tag is empty or null
      See Also: