The Jakarta Project < Velocity Tools - Struts >

Velocity Tools

VelocityStruts

VelocityStruts Tools

MessageTool Reference Documentation
       

The MessageTool is used to render internationalized message strings. Source of the strings are the message resource bundles of the Struts framework. The following methods operate on these message resources.

Class
 org.apache.velocity.tools.struts.MessageTool
Name
 $msg (this is the recommended name of the tool in the Velocity context)
Toolbox Configuration Example
 
<tool>
  <key>msg</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.MessageTool</class>
</tool>
Author(s)
 Gabriel Sidler
Method Overview
get() Looks up and returns the localized message for the specified key.
exists() Checks if a message string for a specified message key exists for the user's locale.
getLocale() Returns the user's locale. If a locale is not found, the default locale is returned.
get()
       

Looks up and returns the localized message for the specified key.

String get(String key)

String get(String key, Object args[])

String get(String key, List args)

Parameters
key
Message key.
args
Replacement parameters for this message. Typically an array of Strings or a List of Strings (but any Object with a reasonable toString() method can be used).
Returns
The localized message for the specified key. Returns null if no message exists for the key passed.
See Also
Struts user's guide on Internationalized Messages.

The user's locale is consulted to determine the language of the message. The second and third signature take a list of up to five replacement parameters. The second signature is provided for compatibility with existing applications. The third signature is Velocity-friendlier.

Assuming that the message resource files contain the following messages:

title=Welcome to Velocity for Struts
test=This message has five replacement parameters: {0}, {1}, {2}, {3}, {4}

then the following Velocity script:

$msg.get("title")
$msg.title
$msg.get("test", ["bear", "wolf", "tiger"])

produces this output:

Welcome to Velocity for Struts
Welcome to Velocity for Struts
This message has five replacement parameters: bear, wolf, tiger, {3}, {4}

exists()
       

Checks if a message string for a specified message key exists for the user's locale.

boolean exists(String key)

Parameters
key
Message key.
Returns
true if a message string for the specified message key exists for the user's locale. false otherwise.
$msg.exists("title")

getLocale()
       

Returns the user's locale. If a locale is not found, the default locale is returned.

Locale getLocale()

Returns
An instance of class java.util.Locale.
$msg.getLocale()
$msg.locale
$msg.getLocale().getDisplayLanguage()

produces:

en
en
English

The returned object of class Locale is automatically rendered by Velocity by calling its toString() method. The third example calls method getDisplayLanguage() of class Locale



Copyright © 1999-2003, Apache Software Foundation