Class PolyglotMultiResourceBundle

java.lang.Object
church.i18n.resources.bundles.PolyglotMultiResourceBundle
All Implemented Interfaces:
MultiResourceBundle

public class PolyglotMultiResourceBundle extends 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 Details

    • PolyglotMultiResourceBundle

      public PolyglotMultiResourceBundle(@Nullable @Nullable Locale defaultLocale, @Nullable @Nullable String... resourceBundleLocations)
    • 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. If null, 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 locale Locale.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

      public PolyglotMultiResourceBundle(@Nullable @Nullable String... resourceBundleLocations)
    • 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 is null or empty, default and root locale are used instead
      Returns:
      Optional.empty() is returned if the code is null, 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 is null or empty, default and root locale are used instead
      Returns:
      Optional.empty() is returned if the code is null or 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 is null or 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 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 ResourceBundleDescriptor... resourceBundleDescriptors)
      Description copied from interface: MultiResourceBundle
      Add message sources into multiple resource bundle.
      Specified by:
      addMessageSources in interface MultiResourceBundle
      Parameters:
      resourceBundleDescriptors - Resource bundles locations.
      Returns:
      Returns current resource bundle with all previously added resource bundles.
    • getLocales

      @NotNull public @NotNull Set<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 Locale defaultLocale)