public class Localizer extends java.lang.Object implements Externalizable
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
currentLocale |
private OrderedMap<java.lang.String,PrefixTreeNode> |
currentLocaleData |
private java.lang.String |
defaultLocale |
private boolean |
fallbackDefaultForm |
private boolean |
fallbackDefaultLocale |
private OrderedMap<java.lang.String,java.util.List<LocaleDataSource>> |
localeResources |
private java.util.List<java.lang.String> |
locales |
private java.util.List<Localizable> |
observers |
private PrefixTree |
stringTree |
Constructor and Description |
---|
Localizer()
Default constructor.
|
Localizer(boolean fallbackDefaultLocale,
boolean fallbackDefaultForm)
Full constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addAvailableLocale(java.lang.String locale)
Create a new locale (with no mappings).
|
private void |
alertLocalizables()
Send a locale change update to all registered ILocalizables.
|
private static java.lang.String |
arg(java.lang.String in) |
static java.lang.String |
clearArguments(java.lang.String text) |
boolean |
destroyLocale(java.lang.String locale)
Undefine a locale and remove all its data.
|
boolean |
equals(java.lang.Object o) |
private static int |
extractNextIndex(java.lang.String text,
java.lang.String[] args) |
static java.util.List<java.lang.String> |
getArgs(java.lang.String text) |
java.lang.String[] |
getAvailableLocales()
Get a list of defined locales.
|
java.lang.String |
getDefaultLocale()
Get the default locale.
|
boolean |
getFallbackForm()
Get default form fallback mode
|
boolean |
getFallbackLocale()
Get default locale fallback mode
|
java.lang.String |
getLocale()
Get the current locale.
|
OrderedMap<java.lang.String,PrefixTreeNode> |
getLocaleData(java.lang.String locale)
Get the set of mappings for a locale.
|
OrderedMap<java.lang.String,PrefixTreeNode> |
getLocaleMap(java.lang.String locale)
Get the mappings for a locale, but throw an exception if locale is not defined.
|
java.lang.String |
getLocalizedText(java.lang.String textID)
Retrieve localized text for a text handle in the current locale.
|
java.lang.String |
getNextLocale()
Return the next locale in order, for cycling through locales.
|
java.lang.String |
getRawText(java.lang.String locale,
java.lang.String textID)
Get text for locale and exact text ID only, not using any fallbacks.
|
java.lang.String |
getText(java.lang.String textID)
Retrieve the localized text for a text handle in the current locale.
|
java.lang.String |
getText(java.lang.String textID,
java.util.HashMap<java.lang.String,java.lang.String> args)
Retrieve the localized text for a text handle in the current locale.
|
java.lang.String |
getText(java.lang.String textID,
java.lang.String locale)
Retrieve the localized text for a text handle in the given locale.
|
java.lang.String |
getText(java.lang.String textID,
java.lang.String[] args)
Retrieve the localized text for a text handle in the current locale.
|
boolean |
hasLocale(java.lang.String locale)
Get whether a locale is defined.
|
boolean |
hasMapping(java.lang.String locale,
java.lang.String textID)
Determine whether a locale has a mapping for a given text handle.
|
private void |
loadCurrentLocaleResources()
Constructs a body of local resources to be the set of Current Locale Data.
|
private void |
loadTable(OrderedMap<java.lang.String,PrefixTreeNode> destination,
OrderedMap<java.lang.String,java.lang.String> source)
Moves all relevant entries in the source dictionary into the destination dictionary
|
static java.lang.String |
processArguments(java.lang.String text,
java.util.HashMap<java.lang.String,java.lang.String> args) |
static java.lang.String |
processArguments(java.lang.String text,
java.lang.String[] args) |
static java.lang.String |
processArguments(java.lang.String text,
java.lang.String[] args,
int currentArg) |
void |
readExternal(java.io.DataInputStream dis,
PrototypeFactory pf)
Read the object from stream.
|
void |
registerLocaleResource(java.lang.String locale,
LocaleDataSource resource)
Registers a resource file as a source of locale data for the specified
locale.
|
void |
registerLocalizable(Localizable l)
Register a Localizable to receive updates when the locale is changed.
|
private static java.lang.String[] |
replaceFirstValue(java.lang.String text,
java.lang.String value) |
void |
setDefaultLocale(java.lang.String defaultLocale)
Set the default locale.
|
void |
setLocale(java.lang.String currentLocale)
Set the current locale.
|
void |
setToDefault()
Set the current locale to the default locale.
|
void |
unregisterAll()
Unregister all ILocalizables.
|
void |
unregisterLocalizable(Localizable l)
Unregister an Localizable from receiving locale change updates.
|
void |
writeExternal(java.io.DataOutputStream dos)
Write the object to stream.
|
private java.util.List<java.lang.String> locales
private OrderedMap<java.lang.String,java.util.List<LocaleDataSource>> localeResources
private OrderedMap<java.lang.String,PrefixTreeNode> currentLocaleData
private PrefixTree stringTree
private java.lang.String defaultLocale
private java.lang.String currentLocale
private boolean fallbackDefaultLocale
private boolean fallbackDefaultForm
private java.util.List<Localizable> observers
public Localizer()
public Localizer(boolean fallbackDefaultLocale, boolean fallbackDefaultForm)
fallbackDefaultLocale
- If true, search the default locale when no translation for a particular text handle
is found in the current locale.fallbackDefaultForm
- If true, search the default text form when no translation is available for the
specified text form ('long', 'short', etc.). Note: form is specified by appending ';[form]' onto the text ID.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean getFallbackLocale()
public boolean getFallbackForm()
public boolean addAvailableLocale(java.lang.String locale)
locale
- Locale to add. Must not be null.java.lang.NullPointerException
- if locale is nullpublic java.lang.String[] getAvailableLocales()
public boolean hasLocale(java.lang.String locale)
locale
- Localepublic java.lang.String getNextLocale()
public java.lang.String getLocale()
public void setLocale(java.lang.String currentLocale)
currentLocale
- Locale. Must be defined and not null.UnregisteredLocaleException
- If locale is null or not defined.public java.lang.String getDefaultLocale()
public void setDefaultLocale(java.lang.String defaultLocale)
defaultLocale
- Default locale. Must be defined. May be null, in which case there will be no default locale.UnregisteredLocaleException
- If locale is not defined.public void setToDefault()
java.lang.IllegalStateException
- If default locale is not set.private void loadCurrentLocaleResources()
private void loadTable(OrderedMap<java.lang.String,PrefixTreeNode> destination, OrderedMap<java.lang.String,java.lang.String> source)
destination
- A dictionary of key/value locale pairs that will be modifiedsource
- A dictionary of key/value locale pairs that will be copied into
destinationpublic void registerLocaleResource(java.lang.String locale, LocaleDataSource resource)
locale
- The locale of the definitions provided.resource
- A LocaleDataSource containing string data for the locale providedjava.lang.NullPointerException
- if resource or locale are nullpublic OrderedMap<java.lang.String,PrefixTreeNode> getLocaleData(java.lang.String locale)
locale
- Localepublic OrderedMap<java.lang.String,PrefixTreeNode> getLocaleMap(java.lang.String locale)
locale
- LocaleUnregisteredLocaleException
- If locale is not defined or null.public boolean hasMapping(java.lang.String locale, java.lang.String textID)
locale
- Locale. Must be defined and not null.textID
- Text handle.UnregisteredLocaleException
- If locale is not defined.public boolean destroyLocale(java.lang.String locale)
locale
- Locale to remove. Must not be null. Need not be defined. Must not be the current locale.java.lang.IllegalArgumentException
- If locale is the current locale.java.lang.NullPointerException
- if locale is nullpublic java.lang.String getText(java.lang.String textID)
textID
- Text handle (text ID appended with optional text form). Must not be null.UnregisteredLocaleException
- If current locale is not set.java.lang.NullPointerException
- if textID is nullpublic java.lang.String getText(java.lang.String textID, java.lang.String[] args)
textID
- Text handle (text ID appended with optional text form). Must not be null.args
- arguments for string variables.UnregisteredLocaleException
- If current locale is not set.java.lang.NullPointerException
- if textID is nullNoLocalizedTextException
- If there is no text for the specified idpublic java.lang.String getText(java.lang.String textID, java.util.HashMap<java.lang.String,java.lang.String> args)
textID
- Text handle (text ID appended with optional text form). Must not be null.args
- arguments for string variables.UnregisteredLocaleException
- If current locale is not set.java.lang.NullPointerException
- if textID is nullNoLocalizedTextException
- If there is no text for the specified idpublic java.lang.String getLocalizedText(java.lang.String textID)
textID
- Text handle (text ID appended with optional text form). Must not be null.NoLocalizedTextException
- If there is no text for the specified idUnregisteredLocaleException
- If current locale is not setjava.lang.NullPointerException
- if textID is nullpublic java.lang.String getText(java.lang.String textID, java.lang.String locale)
The search order is thus: 1) Specified locale, specified text form 2) Specified locale, default text form 3) Default locale, specified text form 4) Default locale, default text form
(1) and (3) are only searched if a text form ('long', 'short', etc.) is specified. If a text form is specified, (2) and (4) are only searched if default-form-fallback mode is enabled. (3) and (4) are only searched if default-locale-fallback mode is enabled. It is not an error in this situation if no default locale is set; (3) and (4) will simply not be searched.
textID
- Text handle (text ID appended with optional text form). Must not be null.locale
- Locale. Must be defined and not null.UnregisteredLocaleException
- If the locale is not defined or null.java.lang.NullPointerException
- if textID is nullpublic java.lang.String getRawText(java.lang.String locale, java.lang.String textID)
locale
- Locale. Must be defined and not null.textID
- Text handle (text ID appended with optional text form). Must not be null.UnregisteredLocaleException
- If the locale is not defined or null.java.lang.NullPointerException
- if textID is nullpublic void registerLocalizable(Localizable l)
l
- Localizable to register.public void unregisterLocalizable(Localizable l)
l
- Localizable to unregister.public void unregisterAll()
private void alertLocalizables()
private static java.lang.String arg(java.lang.String in)
public static java.util.List<java.lang.String> getArgs(java.lang.String text)
public static java.lang.String processArguments(java.lang.String text, java.util.HashMap<java.lang.String,java.lang.String> args)
public static java.lang.String processArguments(java.lang.String text, java.lang.String[] args)
public static java.lang.String processArguments(java.lang.String text, java.lang.String[] args, int currentArg)
public static java.lang.String clearArguments(java.lang.String text)
private static int extractNextIndex(java.lang.String text, java.lang.String[] args)
private static java.lang.String[] replaceFirstValue(java.lang.String text, java.lang.String value)
public void readExternal(java.io.DataInputStream dis, PrototypeFactory pf) throws java.io.IOException, DeserializationException
readExternal
in interface Externalizable
java.io.IOException
DeserializationException
public void writeExternal(java.io.DataOutputStream dos) throws java.io.IOException
writeExternal
in interface Externalizable
java.io.IOException