Class ResourceTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.LocaleConfig
-
- org.apache.velocity.tools.generic.ResourceTool
-
- All Implemented Interfaces:
java.io.Serializable
@DefaultKey("text") public class ResourceTool extends LocaleConfig implements java.io.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:
- the result of HttpServletRequest.getLocale() (if used in request scope of a VelocityView app)
- the configured locale for this tool (as shown above)
- the configured locale for the toolbox this tool is in
- the configured locale for the toolbox factory managing this tool
- the system locale, if none of the 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.
- Since:
- VelocityTools 1.3
- Version:
- $Revision$ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
- Author:
- Nathan Bubna
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classResourceTool.KeyInternal class used to enable an elegant syntax for accessing resources.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBUNDLES_KEY-
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
-
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
-
Constructor Summary
Constructors Constructor Description ResourceTool()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourceTool.Keybundle(java.lang.String bundle)protected voidconfigure(ValueParser parser)Does the actual configuration.ResourceTool.Keyget(java.lang.Object k)Accepts objects and uses their string value as the key.java.lang.Objectget(java.lang.Object k, java.lang.String[] bundles, java.lang.Object l)Retrieve a resource for the specified key from the first of the specified bundles in which a matching resource is found.java.lang.Objectget(java.lang.Object key, java.lang.String baseName, java.lang.Object loc)Returns the value for the specified key in the ResourceBundle for the specified basename and locale.ResourceTool.Keyget(java.lang.String key)Accepts objects and uses their string value as the key.protected java.util.ResourceBundlegetBundle(java.lang.String baseName, java.lang.Object loc)Retrieves theResourceBundlefor the specified baseName and locale, if such exists.protected java.lang.StringgetDefaultBundle()java.util.List<java.lang.String>getKeys()Lists available keysjava.util.List<java.lang.String>getKeys(java.lang.String prefix, java.lang.String[] bundles, java.lang.Object loc)Returns aListof the key strings in the specified ResourceBundles.java.util.List<java.lang.String>getKeys(java.lang.String prefix, java.lang.String baseName, java.lang.Object loc)Returns aListof the key strings in the ResourceBundle with the specified baseName and locale.ResourceTool.Keyinsert(java.lang.Object arg)ResourceTool.Keyinsert(java.lang.Object[] args)ResourceTool.Keyinsert(java.lang.Object arg0, java.lang.Object arg1)ResourceTool.Keyinsert(java.util.List args)ResourceTool.Keylocale(java.lang.Object locale)java.lang.Stringrender(java.lang.Object resource, java.lang.Object[] args)Renders the specified resource value and arguments as a String.protected voidsetDefaultBundle(java.lang.String bundle)-
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale, toLocale
-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
-
-
-
Field Detail
-
BUNDLES_KEY
public static final java.lang.String BUNDLES_KEY
- See Also:
- Constant Field Values
-
-
Method Detail
-
setDefaultBundle
protected final void setDefaultBundle(java.lang.String bundle)
-
getDefaultBundle
protected final java.lang.String getDefaultBundle()
-
configure
protected void configure(ValueParser parser)
Description copied from class:LocaleConfigDoes the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- Overrides:
configurein classLocaleConfig- Parameters:
parser- configuration values
-
get
public ResourceTool.Key get(java.lang.Object k)
Accepts objects and uses their string value as the key.- Parameters:
k- key- Returns:
- Key object
-
get
public ResourceTool.Key get(java.lang.String key)
Accepts objects and uses their string value as the key.- Parameters:
key- key- Returns:
- Key object
-
getKeys
public java.util.List<java.lang.String> getKeys()
Lists available keys- Returns:
- keys list
-
bundle
public ResourceTool.Key bundle(java.lang.String bundle)
-
locale
public ResourceTool.Key locale(java.lang.Object locale)
-
insert
public ResourceTool.Key insert(java.lang.Object[] args)
-
insert
public ResourceTool.Key insert(java.util.List args)
-
insert
public ResourceTool.Key insert(java.lang.Object arg)
-
insert
public ResourceTool.Key insert(java.lang.Object arg0, java.lang.Object arg1)
-
getBundle
protected java.util.ResourceBundle getBundle(java.lang.String baseName, java.lang.Object loc)Retrieves theResourceBundlefor the specified baseName and locale, if such exists. If the baseName or locale is null or if the locale argument cannot be converted to aLocale, then this will return null.- Parameters:
baseName- base nameloc- locale- Returns:
- resource bundle
-
get
public java.lang.Object get(java.lang.Object key, java.lang.String baseName, java.lang.Object loc)Returns the value for the specified key in the ResourceBundle for the specified basename and locale. If no such resource can be found, no errors are thrown andnullis returned.- Parameters:
key- the key for the requested resourcebaseName- the base name of the resource bundle to searchloc- the locale to use- Returns:
- value
-
get
public java.lang.Object get(java.lang.Object k, java.lang.String[] bundles, java.lang.Object l)Retrieve a resource for the specified key from the first of the specified bundles in which a matching resource is found. If no resource is found, no exception will be thrown andnullwill be returned.- Parameters:
k- the key for the requested resourcebundles- the resource bundles to searchl- the locale to use- Returns:
- value
-
getKeys
public java.util.List<java.lang.String> getKeys(java.lang.String prefix, java.lang.String baseName, java.lang.Object loc)Returns aListof 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.- Parameters:
prefix- the prefix for the requested keysbaseName- the resource bundle base nameloc- the locale to use- Returns:
- keys list
-
getKeys
public java.util.List<java.lang.String> getKeys(java.lang.String prefix, java.lang.String[] bundles, java.lang.Object loc)Returns aListof 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.- Parameters:
prefix- the prefix for the requested keysbundles- the resource bundles to searchloc- the locale to use- Returns:
- keys list
- See Also:
getKeys(String,String,Object)
-
render
public java.lang.String render(java.lang.Object resource, java.lang.Object[] args)Renders the specified resource value and arguments as a String. The resource is treated as aMessageFormatpattern which is used for formatting along with any specified argument values.- Parameters:
resource- resource objectargs- arguments- Returns:
- formatted string
-
-