Freemarker I18N tag. For use with a resource bundle model of
authorkellan <kellan>
Mon, 22 Oct 2001 01:45:38 +0000 (01:45 +0000)
committerkellan <kellan>
Mon, 22 Oct 2001 01:45:38 +0000 (01:45 +0000)
internationalization.  more info at http://riseup.net/~kellan/i18n/

source/mir/misc/MessageMethodModel.java [new file with mode: 0755]

diff --git a/source/mir/misc/MessageMethodModel.java b/source/mir/misc/MessageMethodModel.java
new file mode 100755 (executable)
index 0000000..a9cb326
--- /dev/null
@@ -0,0 +1,86 @@
+import java.util.*;
+import freemarker.template.*;
+import org.apache.struts.util.*;
+
+/**
+ * A FreeMarker <code>TemplateMethodModel</code> that provides access to a 
+ * Struts <code>MessageResources</code>, for use in Interantionalized templates.
+ *
+ * TODO:  we probably want to be doing some memoizing or something with this class* 
+ * @author Kellan <kellan@protest.net>
+ *
+ */
+ 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);
+}