3 import freemarker.template.SimpleScalar;
4 import freemarker.template.TemplateMethodModel;
5 import freemarker.template.TemplateModel;
6 import org.apache.struts.util.MessageResources;
9 import java.util.Locale;
12 * A FreeMarker <code>TemplateMethodModel</code> that provides access to a
13 * Struts <code>MessageResources</code>, for use in Interantionalized templates.
15 * @author Kellan <kellan@protest.net>
18 public class MessageMethodModel implements TemplateMethodModel {
21 * The perferred locale for this instance of MessageMethod.
23 private Locale locale;
26 * The MessageResources to query, a single instance shared for
27 * the lifetime of servlet.
29 private MessageResources messages;
33 * Construct a MessageMethod that uses the JVM's default locale.
35 * @param message The MessageResources object to query
37 public MessageMethodModel(MessageResources messages) {
42 * Construct a MessageMethod
44 * @param locale a Locale object, persumably initialized
45 * from users Accept-Language header field
47 * @param message The MessageResources object to query
49 public MessageMethodModel(Locale locale, MessageResources messages) {
51 this.messages = messages;
56 * Takes the first argument as a resource key, then looks up
57 * a string in the MessagesResources, based on that key, and the Locale
59 * TODO: error messages should be i18n :)
61 * @param arguments List passed in by FM, first arguement is a string used as the key
62 * all subsequent arguments are used as described in MessageResources
63 * (they are filled into the placehoders of the string being returned)
65 public TemplateModel exec(List arguments) {
66 if (arguments != null) {
67 String key = (String) arguments.get(0);
69 String mesg = messages.getMessage(locale, key, arguments.toArray());
72 return new SimpleScalar(errUnknownTag+key);
74 return new SimpleScalar(mesg);
77 return missingKeyScalar;
81 // i'm not real clear on how this is used - kellan :)
82 public boolean isEmpty() {
89 private static String errUnknownTag = "MESSAGE NOT FOUND: ";
90 private static String missingKey = "MESSAGE CALL WITHOUT KEY";
91 private static SimpleScalar missingKeyScalar =
92 new SimpleScalar(missingKey);