Package church.i18n.resources.bundles
Class PolyglotMultiResourceBundle
java.lang.Object
church.i18n.resources.bundles.PolyglotMultiResourceBundle
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionPolyglotMultiResourceBundle(@Nullable ResourceBundleDescriptor... resourceBundleDescriptors) Load multiple resource bundles with messages.PolyglotMultiResourceBundle(@Nullable String... resourceBundleLocations) PolyglotMultiResourceBundle(@Nullable Locale defaultLocale, @Nullable ResourceBundleDescriptor... resourceBundleDescriptors) Load multiple resource bundles with messages.PolyglotMultiResourceBundle(@Nullable Locale defaultLocale, @Nullable String... resourceBundleLocations) -
Method Summary
Modifier and TypeMethodDescription@NotNull MultiResourceBundleaddMessageSources(@Nullable ResourceBundleDescriptor... resourceBundleDescriptors) Add message sources into multiple resource bundle.Retrieve a list of loaded locales.static @NotNull PolyglotMultiResourceBundleRetrieve singleton instance of this class.@NotNull Optional<MessageFormat> getString(@Nullable String key, @Nullable Enumeration<Locale> locales) Retrieve the first localized message.@NotNull Optional<MessageFormat> Retrieve the first localized message.@NotNull Optional<MessageFormat> Retrieve a localized message from the message bundle.@NotNull PolyglotMultiResourceBundleloadLocale(@NotNull Locale locale) Add a locale among other locales.@NotNull PolyglotMultiResourceBundlesetDefaultLocale(@NotNull Locale defaultLocale) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface church.i18n.resources.bundles.MultiResourceBundle
addMessageSources
-
Constructor Details
-
PolyglotMultiResourceBundle
-
PolyglotMultiResourceBundle
public PolyglotMultiResourceBundle(@Nullable @Nullable Locale defaultLocale, @Nullable @Nullable ResourceBundleDescriptor... resourceBundleDescriptors) Load multiple resource bundles with messages.- Parameters:
defaultLocale- Set default locale that will be used by this class. Ifnull,Locale.getDefault()is used instead.resourceBundleDescriptors- 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 ResourceBundleDescriptor... resourceBundleDescriptors) Load multiple resource bundles with messages. When you call this constructor without any specific locale, the default localeLocale.getDefault()will be used.- Parameters:
resourceBundleDescriptors- 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.
-
PolyglotMultiResourceBundle
-
PolyglotMultiResourceBundle
public PolyglotMultiResourceBundle()
-
-
Method Details
-
getString
@NotNull public @NotNull Optional<MessageFormat> getString(@Nullable @Nullable String key, @Nullable @Nullable List<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 Optional<MessageFormat> getString(@Nullable @Nullable String key, @Nullable @Nullable Enumeration<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 Optional<MessageFormat> getString(@Nullable @Nullable String key, @Nullable @Nullable 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
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
Retrieve singleton instance of this class.- Returns:
- Singleton instance.
-
addMessageSources
@NotNull public @NotNull MultiResourceBundle addMessageSources(@Nullable @Nullable ResourceBundleDescriptor... resourceBundleDescriptors) Description copied from interface:MultiResourceBundleAdd message sources into multiple resource bundle.- Specified by:
addMessageSourcesin interfaceMultiResourceBundle- Parameters:
resourceBundleDescriptors- Resource bundles locations.- Returns:
- Returns current resource bundle with all previously added resource bundles.
-
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 Locale defaultLocale)
-