public class TranslationManager
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
private static class |
TranslationManager.BundleControl
Custom bundle control to override the bundle name logic.
|
| Modifier and Type | Field and Description |
|---|---|
private TranslationManager.BundleControl |
bundleControl
Custom bundle control
|
private java.util.List<java.lang.String> |
currentLanguages
Currently set current languages
|
private double |
expansionRatio
String expansion ratio
|
private static int[] |
expansionTable
Expansion table:
Even indexes contain the minimum number of chars for the expansion value to take effect.
|
private static ContextLocal<TranslationManager> |
instance
Context local data
|
private static CentralLogger |
LOG
Logger
|
private java.util.concurrent.atomic.AtomicLong |
nextReferentId
Next referent id
|
static java.lang.String |
REFERENT_ID_FIELD_NAME
Synthetic field names generated in the converted java classes for business logic, frames and legacy
classes.
|
private java.util.Map<java.lang.Long,java.util.ResourceBundle> |
referentBundleMap
Map of referent ids to resource budles
|
private java.util.Map<java.lang.Class<?>,java.lang.Long> |
referentIdMap
Map of referent classes and their corresponding referent ids
|
private java.util.Map<java.lang.Long,java.lang.Class<?>> |
referentMap
Map of referent ids and their referent classes
|
static java.lang.String |
TR_MANAGER_FIELD_NAME |
| Constructor and Description |
|---|
TranslationManager()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
applyLanguage(java.lang.Class<?> referentClass)
Resolves a resource bundle based on the current languages set with
setCurrentLanguages(String)
and associates it with the supplied referent class. |
void |
applyLanguage(long referentId)
Resolves a resource bundle based on the current languages set with
setCurrentLanguages(String)
and associates it with the supplied referent. |
static java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> |
getBundles(java.lang.String bundleBaseName,
java.util.List<java.lang.String> languages)
Returns the iterator over the collection of i18n resources for the given bundle base name and
the given list of language identifiers.
|
static java.lang.String |
getDefaultLanguage()
Get the JVM default language in the form of language code + _ + country code, for example, en_US.
|
static int |
getExpandedLength(java.lang.String str,
double expansionRatio)
Calculates the expanded length of the supplied char.
|
static java.lang.String |
getIdentifier(java.lang.String str)
Converts the supplied string to a valid Java identifier by skipping the unsupported chars from the
result.
|
static TranslationManager |
getInstance()
Returns the context local instance.
|
long |
initReferent(java.lang.Class<?> referentClass)
Initializes supplied class (aka referent class) for the purpose of i18n.
|
static java.util.List<java.lang.String> |
parseLanguages(java.lang.String languages,
boolean computeDefaultLanguage)
Converts the given string of comma separated list of language identifiers to the list of
language identifiers.
|
private java.util.ResourceBundle |
resolveBundle(java.lang.Class<?> referentClass)
Resolves resource bundle for the supplied reference.
|
void |
setCurrentLanguages(java.lang.String languages)
Sets the current languages.
|
java.lang.String |
translate(long referentId,
long ctxt,
java.lang.String msg)
Translates the supplied message to the current target language.
|
java.lang.String |
translate(long referentId,
java.lang.String msg)
Translates the supplied message to the current target language.
|
I18nString |
translateExpand(long referentId,
long ctxt,
java.lang.String msg)
Translates the supplied string for the supplied referent and calculates the expansion length (see
getExpandedLength(String, double) for more details). |
I18nString |
translateExpand(long referentId,
java.lang.String msg)
Translates the supplied string for the supplied referent and calculates the expansion length (see
getExpandedLength(String, double) for more details). |
public static final java.lang.String REFERENT_ID_FIELD_NAME
public static final java.lang.String TR_MANAGER_FIELD_NAME
private static CentralLogger LOG
private static final ContextLocal<TranslationManager> instance
private static int[] expansionTable
private final java.util.concurrent.atomic.AtomicLong nextReferentId
private final java.util.Map<java.lang.Long,java.util.ResourceBundle> referentBundleMap
private final java.util.Map<java.lang.Long,java.lang.Class<?>> referentMap
private final java.util.Map<java.lang.Class<?>,java.lang.Long> referentIdMap
private volatile java.util.List<java.lang.String> currentLanguages
private TranslationManager.BundleControl bundleControl
private double expansionRatio
public static TranslationManager getInstance()
public static java.lang.String getIdentifier(java.lang.String str)
str - The input string.public static int getExpandedLength(java.lang.String str,
double expansionRatio)
str - The string to be expanded.expansionRatio - The expansion ratio, must be 0 or greater.null or empty, or
expansionRatio is less than 0, the returned value is 0.public static java.util.List<java.lang.String> parseLanguages(java.lang.String languages,
boolean computeDefaultLanguage)
languages - The given string of comma separated list of language identifierscomputeDefaultLanguage - True value indicates that the default language is computed if languages value is
given as "?".public static java.lang.String getDefaultLanguage()
public static java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getBundles(java.lang.String bundleBaseName,
java.util.List<java.lang.String> languages)
bundleBaseName - The given bundle base namelanguages - The given list of language identifierpublic long initReferent(java.lang.Class<?> referentClass)
CURRENT-LANGUAGE@ session
variable.referentClass - The referent class.public java.lang.String translate(long referentId,
java.lang.String msg)
referentId - Referent class id returned by initReferent(Class).msg - A message to translate.msg if the message can't be translated.public java.lang.String translate(long referentId,
long ctxt,
java.lang.String msg)
referentId - Referent class id returned by initReferent(Class).ctxt - A context identifier. It is used to disambiguate messages of the same value but appearing in
different locations in the source.msg - A message to translate.msg if the message can't be translated.public I18nString translateExpand(long referentId, java.lang.String msg)
getExpandedLength(String, double) for more details).referentId - Id of the referent.msg - The string to be translated.public I18nString translateExpand(long referentId, long ctxt, java.lang.String msg)
getExpandedLength(String, double) for more details).referentId - Id of the referent.ctxt - Context id.msg - The string to be translated.public void setCurrentLanguages(java.lang.String languages)
null a comma separated string.
When null the effective current language will be cleared and messages will not be translated.
When multiple languages are given, they are used during language bundle resolution such that the
current languages are iterated until a corresponding resource bundle is found.languages - List of languages to assign as current languages. The value is a comma separated string.public void applyLanguage(long referentId)
setCurrentLanguages(String)
and associates it with the supplied referent. Any further translation requests for the referent will be
performed against the resolved resource bundle.referentId - The referent id.public boolean applyLanguage(java.lang.Class<?> referentClass)
setCurrentLanguages(String)
and associates it with the supplied referent class. Any further translation requests for the referent
class will be performed against the resolved resource bundle.referentClass - The referent class.true if the language was successfully applied to the referent, false
otherwise.private java.util.ResourceBundle resolveBundle(java.lang.Class<?> referentClass)
referentClass - The referent class.