Package church.i18n.resources.bundles
Class PolyglotMultiResourceBundle
- java.lang.Object
-
- church.i18n.resources.bundles.PolyglotMultiResourceBundle
-
- All Implemented Interfaces:
MultiResourceBundle
public class PolyglotMultiResourceBundle extends java.lang.Object implements MultiResourceBundle
Holder of different resource bundles for multiple languages. The class allows adding resource bundles 'on the fly' and loads locales lazily, when they are required.
-
-
Constructor Summary
Constructors Constructor Description PolyglotMultiResourceBundle(@Nullable java.lang.String... resourceFilenames)Load multiple resource bundles with messages.PolyglotMultiResourceBundle(@Nullable java.util.Locale defaultLocale, @Nullable java.lang.String... resourceFilenames)Load multiple resource bundles with messages.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull MultiResourceBundleaddMessageSources(@Nullable java.lang.String... fileLocations)Add message sources into multiple resource bundle.@NotNull java.util.Set<java.util.Locale>getLocales()Retrieve a list of loaded locales.static @NotNull PolyglotMultiResourceBundlegetSingleton()Retrieve singleton instance of this class.@NotNull java.util.Optional<java.text.MessageFormat>getString(@Nullable java.lang.String key, @Nullable java.util.Enumeration<java.util.Locale> locales)Retrieve the first localized message.@NotNull java.util.Optional<java.text.MessageFormat>getString(@Nullable java.lang.String key, @Nullable java.util.List<java.util.Locale> prioritizedLocales)Retrieve the first localized message.@NotNull java.util.Optional<java.text.MessageFormat>getString(@Nullable java.lang.String key, @Nullable java.util.Locale locale)Retrieve a localized message from the message bundle.@NotNull PolyglotMultiResourceBundleloadLocale(@NotNull java.util.Locale locale)Add a locale among other locales.@NotNull PolyglotMultiResourceBundlesetDefaultLocale(@NotNull java.util.Locale defaultLocale)
-
-
-
Constructor Detail
-
PolyglotMultiResourceBundle
public PolyglotMultiResourceBundle(@Nullable @Nullable java.util.Locale defaultLocale, @Nullable @Nullable java.lang.String... resourceFilenames)Load multiple resource bundles with messages.- Parameters:
defaultLocale- Set default locale that will be used by this class. Ifnull,Locale.getDefault()is used instead.resourceFilenames- List of resource bundle names to load. If you do not specify any file, no message will be retrieved. You can freely add resource files later. The order of added resource bundles is respected during message lookup.
-
PolyglotMultiResourceBundle
public PolyglotMultiResourceBundle(@Nullable @Nullable java.lang.String... resourceFilenames)Load multiple resource bundles with messages. When you call this constructor without any specific locale, the default localeLocale.getDefault()will be used.- Parameters:
resourceFilenames- List of resource bundles names to load. If you do not specify any file, no message will be retrieved. You can freely add resource files later. The order of added resource bundles is respected during message lookup.
-
-
Method Detail
-
getString
@NotNull public @NotNull java.util.Optional<java.text.MessageFormat> getString(@Nullable @Nullable java.lang.String key, @Nullable @Nullable java.util.List<java.util.Locale> prioritizedLocales)Retrieve the first localized message. The order of locales is respected, and the first found localized message is returned. If the specified locale was not loaded yet, it tries to load messages with required locale first and then it looks for a message. It always tries to load also the message from the default locale if none from specified one was found.- Parameters:
key- Message code reference from resource bundle.prioritizedLocales- Ordered list of locales. If the enumeration isnullor empty, default and root locale are used instead- Returns:
Optional.empty()is returned if the code isnull, or the message was not found in any locale in any loaded resource bundle. Otherwise, it returns the message and locale of the first locale and the first resource bundle file where the message was found.
-
getString
@NotNull public @NotNull java.util.Optional<java.text.MessageFormat> getString(@Nullable @Nullable java.lang.String key, @Nullable @Nullable java.util.Enumeration<java.util.Locale> locales)Retrieve the first localized message. The order of locales is respected and the first found localized message is returned. If the specified locale was not loaded yet, it tries to load messages with required locale first and then it looks for a message. It always tries to load also the message from the default locale if none from specified one was found.- Parameters:
key- Message code reference from resource bundle.locales- Ordered list of locales. If the enumeration isnullor empty, default and root locale are used instead- Returns:
Optional.empty()is returned if the code isnullor the message was not found in any locale in any loaded resource bundle. Otherwise it returns the message and locale in the first locale and the first resource bundle file found.
-
getString
@NotNull public @NotNull java.util.Optional<java.text.MessageFormat> getString(@Nullable @Nullable java.lang.String key, @Nullable @Nullable java.util.Locale locale)Retrieve a localized message from the message bundle. If the specified locale was not loaded yet, it tries to load messages first and then it looks for a message.- Parameters:
key- Message code reference from resource bundle.locale- Locale to use for message lookup.- Returns:
Optional.empty()is returned if the code isnullor the message was not found in default locale in any loaded resource bundle. Otherwise, it returns the message in the default locale, and the first resource bundle file found.
-
loadLocale
@NotNull public @NotNull PolyglotMultiResourceBundle loadLocale(@NotNull @NotNull java.util.Locale locale)
Add a locale among other locales. This loads all resource files that were previously added with specified locale and all resource files added later will be loaded with this new locale as well.- Parameters:
locale- Locale you want to add.- Returns:
- This instance.
-
getSingleton
@NotNull public static @NotNull PolyglotMultiResourceBundle getSingleton()
Retrieve singleton instance of this class.- Returns:
- Singleton instance.
-
addMessageSources
@NotNull public @NotNull MultiResourceBundle addMessageSources(@Nullable @Nullable java.lang.String... fileLocations)
Description copied from interface:MultiResourceBundleAdd message sources into multiple resource bundle.- Specified by:
addMessageSourcesin interfaceMultiResourceBundle- Parameters:
fileLocations- Resource bundles locations.- Returns:
- Returns current resource bundle with all previously added resource bundles.
-
getLocales
@NotNull public @NotNull java.util.Set<java.util.Locale> getLocales()
Retrieve a list of loaded locales.- Returns:
- Set of locales that this instance has already loaded.
-
setDefaultLocale
@NotNull public @NotNull PolyglotMultiResourceBundle setDefaultLocale(@NotNull @NotNull java.util.Locale defaultLocale)
-
-