Class PreferredLocales


  • public class PreferredLocales
    extends Object
    This class encapsulates an ordered list of preferred locales, and the logic to use those to retrieve i18n ResourceBundles.

    ResourceBundles are found by iterating over the preferred locales and returning the first resource bundle for which a non-ROOT locale is available, that is not listed later in the list, or the ROOT locale if no better match is found.

    For example, given available locales of en and fr:

    • Preferred locales of fr-FR, en, resource bundle for locale fr is returned.
    • Preferred locales of fr-FR, en, fr, resource bundle for locale en is returned (fr is listed lower than en).
    • Preferred locales of de, resource bundle for the ROOT locale is returned.
    It is expected that the default resource bundle locale (i.e. the locale for the bundle properties file that doesn't have a language tag, e.g. MyBundle.properties) is en-US. If this is not the case for an application, it can be changed by setting the org.forgerock.defaultBundleLocale system property.
    • Constructor Detail

      • PreferredLocales

        public PreferredLocales​(List<Locale> locales)
        Create a new preference of locales by copying the provided locales list.
        Parameters:
        locales - The list of locales that are preferred, with the first item the most preferred.
      • PreferredLocales

        public PreferredLocales()
        Create a new, empty preference of locales.
    • Method Detail

      • getPreferredLocale

        public Locale getPreferredLocale()
        The preferred locale, i.e. the head of the preferred locales list.
        Returns:
        The most-preferred locale.
      • getLocales

        public List<Locale> getLocales()
        The ordered list of preferred locales.
        Returns:
        A immutable copy of the preferred locales list.
      • getBundleInPreferredLocale

        public ResourceBundle getBundleInPreferredLocale​(String bundleName,
                                                         ClassLoader classLoader)
        Get a ResourceBundle using the preferred locale list and using the provided ClassLoader.
        Parameters:
        bundleName - The name of the bundle to load.
        classLoader - The ClassLoader to use to load the bundle.
        Returns:
        The bundle in the best matching locale.
      • matches

        public static boolean matches​(Locale requested,
                                      Locale candidate,
                                      List<Locale> remainingLocales)
        Checks if the candidate locale the best match for the requested locale? Exclude Locale.ROOT, as it should be the fallback only when all locales are tried.
        Parameters:
        requested - The requested Locale
        candidate - The candidate Locale
        remainingLocales - The remaining locales
        Returns:
        The candidate is beast match for requested locale