4 import freemarker.template.*;
5 import org.apache.struts.util.*;
8 * A FreeMarker <code>TemplateMethodModel</code> that provides access to a
9 * Struts <code>MessageResources</code>, for use in Interantionalized templates.
11 * TODO: we probably want to be doing some memoizing or something with this class*
12 * @author Kellan <kellan@protest.net>
15 public class MessageMethodModel implements TemplateMethodModel {
18 * The perferred locale for this instance of MessageMethod.
20 private Locale locale;
23 * The MessageResources to query, a single instances shared for
24 * the lifetime of servlet.
26 private MessageResources messages;
30 * Construct a MessageMethod that uses the JVM's default locale.
32 * @param message The MessageResources object to query
34 public MessageMethodModel(MessageResources messages) {
39 * Construct a MessageMethod
41 * @param locale a Locale object, persumably initialized
42 * from users Accept-Language header field
44 * @param message The MessageResources object to query
46 public MessageMethodModel(Locale locale, MessageResources messages) {
48 this.messages = messages;
53 * Takes the first argument as a resource key, then looks up
54 * a string in the MessagesResources, based on that key, and the Locale
55 * TODO: this is an alpha implementation. We should then check if we have
56 * any keys left over, and attempt to apply MessageFormat.format.
57 * TODO: error messages should be i18n :)
59 * @param arguments List passed in by FM, first arguement is a string used as the key
61 public TemplateModel exec(List arguments) {
62 if ( arguments != null ) {
63 String key = (String)arguments.get(0);
64 String mesg = messages.getMessage(locale, key);
66 return unknownTagScalar;
68 return new SimpleScalar(mesg);
70 return missingKeyScalar;
74 // i'm not real clear on how this is used - kellan :)
75 public boolean isEmpty() {
82 private static String errUnknownTag = "TAG NOT FOUND";
83 private static String missingKey = "MESSAGE CALL WITHOUT KEY";
84 private static SimpleScalar unknownTagScalar =
85 new SimpleScalar(errUnknownTag);
86 private static SimpleScalar missingKeyScalar =
87 new SimpleScalar(missingKey);