public class TranslationControl
extends java.util.ResourceBundle.Control
ResourceBundle.Control
that optimizes the loading of
resources based on the knowledge of which locales the resources have been
translated to.
An instance of TranslationControl
is constructed providing the
available locales in a String
object separated by commas.
When an instance of TranslationControl
is passed to one of the
getBundle
methods of ResourceBundle
, the overridden
getCandidateLocales(java.lang.String, java.util.Locale)
restricts the target
locale to be compatible to one of those specified at contruction time,
in order to minimize the time spent looking for non existent resources.
TranslationControl
accepts the same resource formats as its
superclass ResourceBundle.Control
does. If other formats are
required, TranslationControl
must be subclassed and the relevant
methods must be overriden.Modifier | Constructor and Description |
---|---|
protected |
TranslationControl(java.util.Locale targetLocale,
java.util.List<java.lang.String> formats,
java.lang.String translations)
Creates an instance of
TranslationControl . |
Modifier and Type | Method and Description |
---|---|
java.util.List<java.util.Locale> |
getCandidateLocales(java.lang.String baseName,
java.util.Locale locale)
Returns a list of candidate locales give the specified target locale.
|
static TranslationControl |
getClassFormatTranslationControl()
Returns the singleton instance of
TranslationControl that
handles the java class format for resources. |
static TranslationControl |
getDefaultFormatTranslationControl()
Returns the singleton instance of
TranslationControl that
handles both the java class and java properties formats. |
java.util.Locale |
getFallbackLocale(java.lang.String baseName,
java.util.Locale locale)
Returns the fallback locale.
|
java.util.List<java.lang.String> |
getFormats(java.lang.String baseName)
Returns the resource formats specified at construction time.
|
static TranslationControl |
getPropertiesFormatTranslationControl()
Returns the singleton instance of
TranslationControl that
handles the java properties format for resources. |
protected TranslationControl(java.util.Locale targetLocale, java.util.List<java.lang.String> formats, java.lang.String translations)
TranslationControl
.
The argument translations
provides the available locales
in a comma-separated list, like, for example, "en_US, ja". If the argument
is null, then getCandidateLocales(java.lang.String, java.util.Locale)
doesn't make
any attempt to restrict the locale; if it is an empty
String
, TranslationControl#getCandidateLocales
restricts the locale to the Locale.ROOT
one.
If the targetLocale
argument is null the default locale will used
as target locale.targetLocale
- the target locale, can be nulltranslations
- a String
with the comma-separated list
of locales, can be nullpublic java.util.List<java.util.Locale> getCandidateLocales(java.lang.String baseName, java.util.Locale locale)
getCandidateLocales
in class java.util.ResourceBundle.Control
baseName
- the base name of the resource, it must not be nulllocale
- the target localeList<String>
containing the candidate localespublic java.util.Locale getFallbackLocale(java.lang.String baseName, java.util.Locale locale)
getCandidateLocales
method ensures that the locale
matches one of those for which the resource are available. Otherwise,
it calls the superclass' method.getFallbackLocale
in class java.util.ResourceBundle.Control
baseName
- the base name of the resource, it must not be nulllocale
- the target localepublic java.util.List<java.lang.String> getFormats(java.lang.String baseName)
getFormats
in class java.util.ResourceBundle.Control
baseName
- the base name of the resource, it must not be nullList
of String
objects representing the
resource formats.public static TranslationControl getClassFormatTranslationControl()
TranslationControl
that
handles the java class format for resources.TranslationControl
instance, it is never nullpublic static TranslationControl getPropertiesFormatTranslationControl()
TranslationControl
that
handles the java properties format for resources.TranslationControl
instance, it is never nullpublic static TranslationControl getDefaultFormatTranslationControl()
TranslationControl
that
handles both the java class and java properties formats.TranslationControl
instance, it is never null