From: kellan Date: Mon, 22 Oct 2001 01:45:38 +0000 (+0000) Subject: Freemarker I18N tag. For use with a resource bundle model of X-Git-Tag: prexmlproducerconfig~327 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=90fc16e50c499da4d3e80054c9d4971e2ba0c555;p=mir.git Freemarker I18N tag. For use with a resource bundle model of internationalization. more info at http://riseup.net/~kellan/i18n/ --- diff --git a/source/mir/misc/MessageMethodModel.java b/source/mir/misc/MessageMethodModel.java new file mode 100755 index 00000000..a9cb3265 --- /dev/null +++ b/source/mir/misc/MessageMethodModel.java @@ -0,0 +1,86 @@ +import java.util.*; +import freemarker.template.*; +import org.apache.struts.util.*; + +/** + * A FreeMarker TemplateMethodModel that provides access to a + * Struts MessageResources, for use in Interantionalized templates. + * + * TODO: we probably want to be doing some memoizing or something with this class* + * @author Kellan + * + */ + public class MessageMethodModel implements TemplateMethodModel { + + /** + * The perferred locale for this instance of MessageMethod. + */ + private Locale locale; + + /** + * The MessageResources to query, a single instances shared for + * the lifetime of servlet. + */ + private MessageResources messages; + + + /** + * Construct a MessageMethod that uses the JVM's default locale. + * + * @param message The MessageResources object to query + */ + public MessageMethodModel(MessageResources messages) { + this(null, messages); + } + + /** + * Construct a MessageMethod + * + * @param locale a Locale object, persumably initialized + * from users Accept-Language header field + * + * @param message The MessageResources object to query + */ + public MessageMethodModel(Locale locale, MessageResources messages) { + this.locale = locale; + this.messages = messages; + } + + + /** + * Takes the first argument as a resource key, then looks up + * a string in the MessagesResources, based on that key, and the Locale + * TODO: this is an alpha implementation. We should then check if we have + * any keys left over, and attempt to apply MessageFormat.format. + * TODO: error messages should be i18n :) + * + * @param arguments List passed in by FM, first arguement is a string used as the key + */ + public TemplateModel exec(List arguments) { + if ( arguments != null ) { + String key = (String)arguments.get(0); + String mesg = messages.getMessage(locale, key); + if ( mesg == null ) { + return unknownTagScalar; + } + return new SimpleScalar(mesg); + } else { + return missingKeyScalar; + } + } + + // i'm not real clear on how this is used - kellan :) + public boolean isEmpty() { + if (messages == null) + return true; + else + return false; + } + + private static String errUnknownTag = "TAG NOT FOUND"; + private static String missingKey = "MESSAGE CALL WITHOUT KEY"; + private static SimpleScalar unknownTagScalar = + new SimpleScalar(errUnknownTag); + private static SimpleScalar missingKeyScalar = + new SimpleScalar(missingKey); +}