Package freemarker.ext.beans
Class ResourceBundleModel
- java.lang.Object
-
- freemarker.ext.beans.BeanModel
-
- freemarker.ext.beans.ResourceBundleModel
-
- All Implemented Interfaces:
WrapperTemplateModel,AdapterTemplateModel,TemplateHashModel,TemplateHashModelEx,TemplateMethodModel,TemplateMethodModelEx,TemplateModel,TemplateModelWithAPISupport
public class ResourceBundleModel extends BeanModel implements TemplateMethodModelEx
A hash model that wraps a resource bundle. Makes it convenient to store localized content in the data model. It also acts as a method model that will take a resource key and arbitrary number of arguments and will apply
MessageFormatwith arguments on the string represented by the key.Typical usages:
- bundle.resourceKey will retrieve the object from resource bundle with key resourceKey
- bundle("patternKey", arg1, arg2, arg3) will retrieve the string from resource bundle with key patternKey, and will use it as a pattern for MessageFormat with arguments arg1, arg2 and arg3
-
-
Field Summary
-
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
-
-
Constructor Summary
Constructors Constructor Description ResourceBundleModel(java.util.ResourceBundle bundle, BeansWrapper wrapper)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectexec(java.util.List arguments)Takes first argument as a resource key, looks up a string in resource bundle with this key, then applies a MessageFormat.format on the string with the rest of the arguments.java.lang.Stringformat(java.lang.String key, java.lang.Object[] params)Provides direct access to caching format engine from code (instead of from script).java.util.ResourceBundlegetBundle()protected TemplateModelinvokeGenericGet(java.util.Map keyMap, java.lang.Class clazz, java.lang.String key)Overridden to invoke the getObject method of the resource bundle.booleanisEmpty()Returns true if this bundle contains no objects.protected java.util.SetkeySet()Helper method to support TemplateHashModelEx.intsize()-
Methods inherited from class freemarker.ext.beans.BeanModel
get, getAdaptedObject, getAPI, getWrappedObject, hasPlainGetMethod, keys, toString, unwrap, values, wrap
-
-
-
-
Constructor Detail
-
ResourceBundleModel
public ResourceBundleModel(java.util.ResourceBundle bundle, BeansWrapper wrapper)
-
-
Method Detail
-
invokeGenericGet
protected TemplateModel invokeGenericGet(java.util.Map keyMap, java.lang.Class clazz, java.lang.String key) throws TemplateModelException
Overridden to invoke the getObject method of the resource bundle.- Overrides:
invokeGenericGetin classBeanModel- Throws:
TemplateModelException
-
isEmpty
public boolean isEmpty()
Returns true if this bundle contains no objects.- Specified by:
isEmptyin interfaceTemplateHashModel- Overrides:
isEmptyin classBeanModel
-
size
public int size()
- Specified by:
sizein interfaceTemplateHashModelEx- Overrides:
sizein classBeanModel- Returns:
- the number of key/value mappings in the hash.
-
keySet
protected java.util.Set keySet()
Description copied from class:BeanModelHelper method to support TemplateHashModelEx. Returns the Set of Strings which are available via the TemplateHashModel interface. Subclasses that override invokeGenericGet to provide additional hash keys should also override this method.
-
exec
public java.lang.Object exec(java.util.List arguments) throws TemplateModelExceptionTakes first argument as a resource key, looks up a string in resource bundle with this key, then applies a MessageFormat.format on the string with the rest of the arguments. The created MessageFormats are cached for later reuse.- Specified by:
execin interfaceTemplateMethodModel- Specified by:
execin interfaceTemplateMethodModelEx- Parameters:
arguments- aListofTemplateModel-s, containing the arguments passed to the method. If the implementation absolutely wants to operate on POJOs, it can use the static utility methods in theDeepUnwrapclass to easily obtain them. However, unwrapping is not always possible (or not perfectly), and isn't always efficient, so it's recommended to use the originalTemplateModelvalue as much as possible.- Returns:
- the return value of the method, or
null. If the returned value does not implementTemplateModel, it will be automatically wrapped using theenvironment's object wrapper. - Throws:
TemplateModelException
-
format
public java.lang.String format(java.lang.String key, java.lang.Object[] params) throws java.util.MissingResourceExceptionProvides direct access to caching format engine from code (instead of from script).- Throws:
java.util.MissingResourceException
-
getBundle
public java.util.ResourceBundle getBundle()
-
-