@DefaultKey(value="text") public class ResourceTool extends LocaleConfig implements Serializable
Tool for accessing ResourceBundles and formatting messages therein.
Template example(s):
$text.foo -> bar $text.hello.world -> Hello World! $text.keys -> [foo, hello.world, world] #set( $otherText = $text.bundle('otherBundle') ) $otherText.foo -> woogie $otherText.bar -> The args are {0} and {1}. $otherText.bar.insert(4) -> The args are 4 and {1}. $otherText.bar.insert(4,true) -> The args are 4 and true. Toolbox configuration example: <tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.generic.ResourceTool" bundles="resources,com.foo.moreResources" locale="en_US"/> </toolbox> </tools>
This comes in very handy when internationalizing templates. Note that the default resource bundle baseName is "resources", and the default locale is either:
Also, be aware that very few performance considerations have been made in this initial version. It should do fine, but if you have performance issues, please report them to dev@velocity.apache.org, so we can make improvements.
Modifier and Type | Class and Description |
---|---|
class |
ResourceTool.Key
Internal class used to enable an elegant syntax for accessing
resources.
|
Modifier and Type | Field and Description |
---|---|
static String |
BUNDLES_KEY |
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
ResourceTool() |
Modifier and Type | Method and Description |
---|---|
ResourceTool.Key |
bundle(String bundle) |
protected void |
configure(ValueParser parser)
Does the actual configuration.
|
ResourceTool.Key |
get(Object k)
Accepts objects and uses their string value as the key.
|
Object |
get(Object k,
String[] bundles,
Object l)
Retrieve a resource for the specified key from the first of the
specified bundles in which a matching resource is found.
|
Object |
get(Object key,
String baseName,
Object loc)
Returns the value for the specified key in the ResourceBundle for
the specified basename and locale.
|
ResourceTool.Key |
get(String key)
Accepts objects and uses their string value as the key.
|
protected ResourceBundle |
getBundle(String baseName,
Object loc)
Retrieves the
ResourceBundle for the specified baseName
and locale, if such exists. |
protected String |
getDefaultBundle() |
List<String> |
getKeys()
Lists available keys
|
List<String> |
getKeys(String prefix,
String[] bundles,
Object loc)
Returns a
List of the key strings in the specified
ResourceBundles. |
List<String> |
getKeys(String prefix,
String baseName,
Object loc)
Returns a
List of the key strings in the ResourceBundle
with the specified baseName and locale. |
ResourceTool.Key |
insert(List args) |
ResourceTool.Key |
insert(Object arg) |
ResourceTool.Key |
insert(Object[] args) |
ResourceTool.Key |
insert(Object arg0,
Object arg1) |
ResourceTool.Key |
locale(Object locale) |
String |
render(Object resource,
Object[] args)
Renders the specified resource value and arguments as a String.
|
protected void |
setDefaultBundle(String bundle) |
getLocale, setLocale, toLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String BUNDLES_KEY
protected final void setDefaultBundle(String bundle)
protected final String getDefaultBundle()
protected void configure(ValueParser parser)
LocaleConfig
configure
in class LocaleConfig
parser
- configuration valuespublic ResourceTool.Key get(Object k)
k
- keypublic ResourceTool.Key get(String key)
key
- keypublic ResourceTool.Key bundle(String bundle)
public ResourceTool.Key locale(Object locale)
public ResourceTool.Key insert(Object[] args)
public ResourceTool.Key insert(List args)
public ResourceTool.Key insert(Object arg)
public ResourceTool.Key insert(Object arg0, Object arg1)
protected ResourceBundle getBundle(String baseName, Object loc)
ResourceBundle
for the specified baseName
and locale, if such exists. If the baseName or locale is null
or if the locale argument cannot be converted to a Locale
,
then this will return null.baseName
- base nameloc
- localepublic Object get(Object key, String baseName, Object loc)
null
is returned.key
- the key for the requested resourcebaseName
- the base name of the resource bundle to searchloc
- the locale to usepublic Object get(Object k, String[] bundles, Object l)
null
will be returned.k
- the key for the requested resourcebundles
- the resource bundles to searchl
- the locale to usepublic List<String> getKeys(String prefix, String baseName, Object loc)
List
of the key strings in the ResourceBundle
with the specified baseName and locale. If the specified prefix
is not null, then this will skip any keys that do not begin with
that prefix and trim the prefix and any subsequent '.' off of the
remaining ones. If the prefix is null, then no filtering or trimming
will be done.prefix
- the prefix for the requested keysbaseName
- the resource bundle base nameloc
- the locale to usepublic List<String> getKeys(String prefix, String[] bundles, Object loc)
List
of the key strings in the specified
ResourceBundles. If the specified prefix
is not null, then this will skip any keys that do not begin with
that prefix and trim the prefix and any subsequent '.' off of the
remaining ones. If the prefix is null, then no filtering or trimming
will be done.prefix
- the prefix for the requested keysbundles
- the resource bundles to searchloc
- the locale to usegetKeys(String,String,Object)
public String render(Object resource, Object[] args)
MessageFormat
pattern which
is used for formatting along with any specified argument values.resource
- resource objectargs
- argumentsCopyright © 2002–2021 The Apache Software Foundation. All rights reserved.