java.lang.Object
org.tentackle.common.LocaleProvider
A provider for
Supports thread-local locales and mapping of locales according to the application requirements.
Locales.Supports thread-local locales and mapping of locales according to the application requirements.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAsserts that a thread-local Locale is set.Converts a tag to a supported locale.Gets the locale used by the current thread.Gets the effective initial locale on startup of the JVM.Gets the effective locale for the current locale.getEffectiveLocale(Locale locale) Gets the effective locale.
This is application-specific and allows narrowing the locales to the effectively supported locales.Returns an unmodifiable list of all supported effective locales.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.Gets the initial locale on startup of the JVM.static LocaleProviderThe singleton.Gets the locale.booleanisLocaleSupported(Locale locale) Returns whether given locale is covered by the effective locales.voidsetCurrentLocale(Locale locale) Sets the locale used by the current thread.
The locale is stored asThreadLocal.Converts a locale to a tag.
Since there is no guarantee thatLocale.toLanguageTag()andLocale.forLanguageTag(String)will round-trip, this method is provided to guarantee that at least for the supported locales.
-
Constructor Details
-
LocaleProvider
public LocaleProvider()Creates a locale provider.
-
-
Method Details
-
getInstance
The singleton.- Returns:
- the singleton
-
getInitialLocale
Gets the initial locale on startup of the JVM.- Returns:
- the initial locale
-
getEffectiveInitialLocale
Gets the effective initial locale on startup of the JVM.- Returns:
- the effective initial locale
-
getCurrentLocale
Gets the locale used by the current thread.- Returns:
- the locale, null if no thread-local Locale set
-
getLocale
Gets the locale.If there is no thread-local Locale, the default Locale is returned.
- Returns:
- the current locale, never null
-
setCurrentLocale
Sets the locale used by the current thread.
The locale is stored asThreadLocal.- 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
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
Gets the effective locale for the current locale.- Returns:
- the mapped 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
Returns an unmodifiable list of all supported effective locales.- Returns:
- the possible effective locales
-
isLocaleSupported
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
Converts a locale to a tag.
Since there is no guarantee thatLocale.toLanguageTag()andLocale.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
Converts a tag to a supported locale.- Parameters:
tag- the tag- Returns:
- the locale, null if tag is empty or null
- See Also:
-