Package org.forgerock.util.i18n
Class PreferredLocales
- java.lang.Object
-
- org.forgerock.util.i18n.PreferredLocales
-
public class PreferredLocales extends Object
This class encapsulates an ordered list of preferred locales, and the logic to use those to retrieve i18nResourceBundle
s.ResourceBundle
s 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
andfr
:-
Preferred locales of
fr-FR, en
, resource bundle for localefr
is returned. -
Preferred locales of
fr-FR, en, fr
, resource bundle for localeen
is returned (fr
is listed lower thanen
). -
Preferred locales of
de
, resource bundle for the ROOT locale is returned.
MyBundle.properties
) isen-US
. If this is not the case for an application, it can be changed by setting theorg.forgerock.defaultBundleLocale
system property. -
Preferred locales of
-
-
Constructor Summary
Constructors Constructor Description PreferredLocales()
Create a new, empty preference of locales.PreferredLocales(List<Locale> locales)
Create a new preference of locales by copying the provided locales list.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourceBundle
getBundleInPreferredLocale(String bundleName, ClassLoader classLoader)
Get aResourceBundle
using the preferred locale list and using the providedClassLoader
.List<Locale>
getLocales()
The ordered list of preferred locales.Locale
getPreferredLocale()
The preferred locale, i.e.static boolean
matches(Locale requested, Locale candidate, List<Locale> remainingLocales)
Checks if the candidate locale the best match for the requested locale? ExcludeLocale.ROOT
, as it should be the fallback only when all locales are tried.
-
-
-
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 aResourceBundle
using the preferred locale list and using the providedClassLoader
.- Parameters:
bundleName
- The name of the bundle to load.classLoader
- TheClassLoader
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? ExcludeLocale.ROOT
, as it should be the fallback only when all locales are tried.- Parameters:
requested
- The requested Localecandidate
- The candidate LocaleremainingLocales
- The remaining locales- Returns:
- The candidate is beast match for requested locale
-
-