Class ResourceBundleProvider<T>

  • Type Parameters:
    T - The localising object based on a ResourceBundle.
    All Implemented Interfaces:
    Function<Locale,​T>
    Direct Known Subclasses:
    MessagesProvider, ResBundleSubclassProvider

    public abstract class ResourceBundleProvider<T>
    extends Object
    implements Function<Locale,​T>
    This class provides resource bundles for various locales and caches them (or the corresponding related objects). This is coupled to a loading class providing the base name and the classloader (and usually also the key constants).

    This class generalises from the concrete localising object. It loads the appropriate resource bundle, wraps it with the localising object and caches it.

    The default function uses no locale fallback for the resource bundle.

    • Field Detail

      • baseName

        protected final String baseName
        The base name of the resource bundle.
      • classLoader

        protected final ClassLoader classLoader
        The class loader used to retrieve the resource bundles.
    • Constructor Detail

      • ResourceBundleProvider

        protected ResourceBundleProvider​(Class<?> loadingClass,
                                         List<String> bundleFormat)
        Constructs a new resource bundle provider for loading resource bundles for a specific locale for the designated class.
        Parameters:
        loadingClass - The class with which to load the resource bundle.
        bundleFormat - A list of bundle formats which to load, usually class and/or properties.
        See Also:
        ResourceBundle.Control.getFormats(String)
    • Method Detail

      • forLocale

        public T forLocale​(Locale locale)
        Returns the resource bundle or the related object for the specified locale.

        This method is thread-safe.

        Parameters:
        locale - The desired locale. If this is null, the default locale of the JVM will be used.
        Returns:
        The best-matching resource bundle or the related object for the specified locale.
      • convert

        protected abstract T convert​(Locale reqLocale,
                                     ResourceBundle bundle)
        Converts the designated resource bundle to the corresponding localising object.
        Parameters:
        reqLocale - The requested locale. This may differ from the locale of the resource bundle.
        bundle - The resource bundle for which to get the corresponding localising object.
        Returns:
        The localising object for the designated resource bundle.