public class BaseL10n
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
BaseL10n.LANGUAGE
Central list of languages and the codes to identify them.
|
Constructor and Description |
---|
BaseL10n(java.lang.String l10nFilesBasePath,
java.lang.String l10nFilesMask,
java.lang.String l10nOverrideFilesMask) |
BaseL10n(java.lang.String l10nFilesBasePath,
java.lang.String l10nFilesMask,
java.lang.String l10nOverrideFilesMask,
BaseL10n.LANGUAGE lang) |
BaseL10n(java.lang.String l10nFilesBasePath,
java.lang.String l10nFilesMask,
java.lang.String l10nOverrideFilesMask,
BaseL10n.LANGUAGE lang,
java.lang.ClassLoader cl)
Create a new BaseL10n object.
|
Modifier and Type | Method and Description |
---|---|
void |
addL10nSubstitution(HTMLNode node,
java.lang.String key,
java.lang.String[] patterns,
HTMLNode[] values)
Loads an L10n string, replaces variables such as ${link} or ${bold} in it with
HTMLNode s
and adds the result to the given HTMLNode. |
void |
addL10nSubstitution(HTMLNode node,
java.lang.String key,
java.lang.String[] patterns,
java.lang.String[] values)
Deprecated.
|
java.lang.String[] |
getAllNamesWithPrefix(java.lang.String prefix) |
SimpleFieldSet |
getCurrentLanguageTranslation()
Get a copy of the currently used SimpleFieldSet.
|
SimpleFieldSet |
getDefaultLanguageTranslation()
Get the SimpleFieldSet of the default language (should be english).
|
java.lang.String |
getDefaultString(java.lang.String key)
Get the default value for a key.
|
java.lang.String |
getDefaultString(java.lang.String key,
java.lang.String[] patterns,
java.lang.String[] values)
Get the default value for a key.
|
HTMLNode |
getHTMLNode(java.lang.String key)
Get a localized string and put it in a HTMLNode for the translation page.
|
HTMLNode |
getHTMLNode(java.lang.String key,
java.lang.String[] patterns,
java.lang.String[] values)
Get a localized string and put it in a HTMLNode for the translation page.
|
java.lang.String |
getL10nFileName(BaseL10n.LANGUAGE lang)
Get the full base name of the L10n file used by the current language.
|
java.lang.String |
getL10nOverrideFileName(BaseL10n.LANGUAGE lang)
Get the full base name of the L10n override file used by the current language.
|
SimpleFieldSet |
getOverrideForCurrentLanguageTranslation()
Get a copy of the currently used SimpleFieldSet (overriden messages).
|
BaseL10n.LANGUAGE |
getSelectedLanguage()
Get the language currently used by this BaseL10n.
|
java.lang.String |
getString(java.lang.String key)
Get a localized string.
|
java.lang.String |
getString(java.lang.String key,
boolean returnNullIfNotFound)
Get a localized string.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String[] patterns,
java.lang.String[] values)
Get a localized string, and replace on-the-fly some values.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String pattern,
java.lang.String value)
Get a localized string, and replace on-the-fly a value.
|
boolean |
isOverridden(java.lang.String key)
Returns true if a key is overriden.
|
void |
setLanguage(BaseL10n.LANGUAGE selectedLanguage)
Use a new language, and load the SimpleFieldSets accordingly.
|
void |
setOverride(java.lang.String key,
java.lang.String value)
Override a custom key with a new value.
|
public BaseL10n(java.lang.String l10nFilesBasePath, java.lang.String l10nFilesMask, java.lang.String l10nOverrideFilesMask)
public BaseL10n(java.lang.String l10nFilesBasePath, java.lang.String l10nFilesMask, java.lang.String l10nOverrideFilesMask, BaseL10n.LANGUAGE lang)
public BaseL10n(java.lang.String l10nFilesBasePath, java.lang.String l10nFilesMask, java.lang.String l10nOverrideFilesMask, BaseL10n.LANGUAGE lang, java.lang.ClassLoader cl)
l10nFilesBasePath
- Base path of the l10n files, ex. "com/mycorp/myproject/l10n"l10nFilesMask
- Mask of the l10n files, ex. "messages_${lang}.l10n"l10nOverrideFilesMask
- Same as l10nFilesMask, but for overriden messages.lang
- Language to use.cl
- ClassLoader to use.public java.lang.String getL10nFileName(BaseL10n.LANGUAGE lang)
public java.lang.String getL10nOverrideFileName(BaseL10n.LANGUAGE lang)
public void setLanguage(BaseL10n.LANGUAGE selectedLanguage) throws java.util.MissingResourceException
selectedLanguage
- New language to use.java.util.MissingResourceException
- If the l10n file could not be found.public BaseL10n.LANGUAGE getSelectedLanguage()
public boolean isOverridden(java.lang.String key)
key
- Key to check override statuspublic void setOverride(java.lang.String key, java.lang.String value)
key
- Key to override.value
- New value of that key.public SimpleFieldSet getCurrentLanguageTranslation()
public SimpleFieldSet getOverrideForCurrentLanguageTranslation()
public SimpleFieldSet getDefaultLanguageTranslation()
public java.lang.String getString(java.lang.String key)
key
- Key to search for.public java.lang.String getString(java.lang.String key, boolean returnNullIfNotFound)
key
- Key to search for.returnNullIfNotFound
- If this is true, will return null if the key is not found.public HTMLNode getHTMLNode(java.lang.String key)
key
- Key to search for.public HTMLNode getHTMLNode(java.lang.String key, java.lang.String[] patterns, java.lang.String[] values)
key
- Key to search for.patterns
- Patterns to replace. May be null, if so values must also be null.values
- Values to replace patterns with.public java.lang.String getDefaultString(java.lang.String key)
key
- Key to search for.public java.lang.String getDefaultString(java.lang.String key, java.lang.String[] patterns, java.lang.String[] values)
key
- Key to search for.public java.lang.String getString(java.lang.String key, java.lang.String[] patterns, java.lang.String[] values)
key
- Key to search for.patterns
- Patterns to replace, ${ and } are not included.values
- Replacement values.public java.lang.String getString(java.lang.String key, java.lang.String pattern, java.lang.String value)
key
- Key to search for.pattern
- Pattern to replace, ${ and } not included.value
- Replacement value.@Deprecated public void addL10nSubstitution(HTMLNode node, java.lang.String key, java.lang.String[] patterns, java.lang.String[] values)
addL10nSubstitution(HTMLNode, String, String[], HTMLNode[])
instead.node
- The result will be put in this HTMLNode.key
- Key to search for.patterns
- Patterns to replace, ${ and } are not included.values
- Replacement values.public void addL10nSubstitution(HTMLNode node, java.lang.String key, java.lang.String[] patterns, HTMLNode[] values)
HTMLNode
s
and adds the result to the given HTMLNode.
This is *much* safer than the deprecated addL10nSubstitution(HTMLNode, String, String[], String[])
.
Callers won't accidentally pass in unencoded strings and cause vulnerabilities.
Callers should try to reuse parameters if possible.
We automatically close each tag: When a pattern ${name} is matched, we search for
${/name}. If we find it, we make the tag enclose everything between the two; if we
can't find it, we just add it with no children. It is not possible to create an
HTMLNode representing a tag closure, so callers will need to change their code to
not pass in /link or similar, and in some cases will need to change the l10n
strings themselves to always close the tag properly, rather than using a generic
/link for multiple links as we use in some places.
Examples:
TranslationLookup.string=This is a ${link}link${/link} about ${text}.
addL10nSubstitution(html, "TranslationLookup.string", new String[] { "link", "text" },
new HTMLNode[] { HTMLNode.link("/KSK@gpl.txt"), HTMLNode.text("blah") });
TranslationLookup.string=${bold}This${/bold} is a bold text.
addL10nSubstitution(html, "TranslationLookup.string", new String[] { "bold" },
new HTMLNode[] { HTMLNode.STRONG });
node
- The HTMLNode
to which the L10n should be added after substitution was done.key
- The key of the L10n string which shall be used.patterns
- Specifies things such as ${link} which shall be replaced in the L10n string with HTMLNode
s.values
- For each entry in the previous array parameter, this array specifies the HTMLNode
with which it shall be replaced.public java.lang.String[] getAllNamesWithPrefix(java.lang.String prefix)