public class ResourceTool
extends java.lang.Object
Tool for accessing ResourceBundles and formatting messages therein.
Template example(s):
$text.foo -> bar
$text.hello.world -> Hello 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:
<tool>
<key>text</key>
<class>org.apache.velocity.tools.generic.ResourceTool</class>
<parameter name="bundles" value="resources,com.foo.moreResources"/>
<parameter name="locale" value="en_US"/>
</tool>
This comes in very handy when internationalizing templates. Note that the default resource bundle baseName is "resources", and the default locale is the system locale. These may both be overridden in your toolbox config as demonstrated above.
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 |
|---|---|
private java.lang.String[] |
bundles |
static java.lang.String |
BUNDLES_KEY |
private java.util.Locale |
locale |
static java.lang.String |
LOCALE_KEY |
| Constructor and Description |
|---|
ResourceTool() |
| Modifier and Type | Method and Description |
|---|---|
ResourceTool.Key |
bundle(java.lang.String bundle) |
void |
configure(java.util.Map params) |
ResourceTool.Key |
get(java.lang.String key) |
java.lang.Object |
get(java.lang.String key,
java.lang.String[] bundles,
java.util.Locale locale)
Retrieve a resource for the specified key from the first of the
specified bundles in which a matching resource is found.
|
java.lang.Object |
get(java.lang.String key,
java.lang.String baseName,
java.util.Locale locale)
Returns the value for the specified key in the ResourceBundle for
the specified basename and locale.
|
protected java.lang.String |
getDefaultBundle() |
protected java.util.Locale |
getDefaultLocale() |
ResourceTool.Key |
insert(java.util.List args) |
ResourceTool.Key |
insert(java.lang.Object arg) |
ResourceTool.Key |
insert(java.lang.Object[] args) |
ResourceTool.Key |
insert(java.lang.Object arg0,
java.lang.Object arg1) |
ResourceTool.Key |
locale(java.util.Locale locale) |
java.lang.String |
render(java.lang.Object resource,
java.lang.Object[] args)
Renders the specified resource value and arguments as a String.
|
protected void |
setDefaultBundle(java.lang.String bundle) |
protected void |
setDefaultLocale(java.util.Locale locale) |
public static final java.lang.String BUNDLES_KEY
public static final java.lang.String LOCALE_KEY
private java.lang.String[] bundles
private java.util.Locale locale
protected final void setDefaultBundle(java.lang.String bundle)
protected final java.lang.String getDefaultBundle()
protected final void setDefaultLocale(java.util.Locale locale)
protected final java.util.Locale getDefaultLocale()
public void configure(java.util.Map params)
public ResourceTool.Key get(java.lang.String key)
public ResourceTool.Key bundle(java.lang.String bundle)
public ResourceTool.Key locale(java.util.Locale locale)
public ResourceTool.Key insert(java.lang.Object[] args)
public ResourceTool.Key insert(java.util.List args)
public ResourceTool.Key insert(java.lang.Object arg)
public ResourceTool.Key insert(java.lang.Object arg0, java.lang.Object arg1)
public java.lang.Object get(java.lang.String key,
java.lang.String baseName,
java.util.Locale locale)
null is returned.public java.lang.Object get(java.lang.String key,
java.lang.String[] bundles,
java.util.Locale locale)
null
will be returned.public java.lang.String render(java.lang.Object resource,
java.lang.Object[] args)
MessageFormat pattern and used to format
the specified argument values.Copyright (c) 2003-2007 Apache Software Foundation