i18n-feature based on kellans proposal implemented. the resource-bundles are located...
[mir.git] / source / mir / misc / MessageMethodModel.java
1 package mir.misc;
2
3 import java.util.*;
4 import freemarker.template.*;
5 import org.apache.struts.util.*;
6
7 /**
8  * A FreeMarker <code>TemplateMethodModel</code> that provides access to a
9  * Struts <code>MessageResources</code>, for use in Interantionalized templates.
10  *
11  * TODO:  we probably want to be doing some memoizing or something with this class*
12  * @author Kellan <kellan@protest.net>
13  *
14  */
15  public class MessageMethodModel implements TemplateMethodModel {
16    
17   /**
18    * The perferred locale for this instance of MessageMethod.
19    */
20         private Locale locale;
21     
22   /**
23    * The MessageResources to query, a single instances shared for
24    * the lifetime of servlet.
25    */
26         private MessageResources messages;
27    
28    
29         /**
30    * Construct a MessageMethod that uses the JVM's default locale.
31          *
32          * @param message The MessageResources object to query
33          */
34   public MessageMethodModel(MessageResources messages) {
35         this(null, messages);
36   }
37    
38         /**
39          * Construct a MessageMethod
40          *
41          * @param locale a Locale object, persumably initialized
42          *               from users Accept-Language header field
43          *
44          * @param message The MessageResources object to query
45          */
46         public MessageMethodModel(Locale locale, MessageResources messages) {
47                 this.locale = locale;
48                 this.messages = messages;
49         }
50          
51    
52   /**
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 :)
58    *
59    * @param arguments List passed in by FM, first arguement is a string used as the key
60    */
61   public TemplateModel exec(List arguments) {
62                 if ( arguments != null ) {
63                         String key = (String)arguments.get(0);
64                         String mesg = messages.getMessage(locale, key);
65                         if ( mesg == null ) {
66                                 return unknownTagScalar;
67                         }
68                         return new SimpleScalar(mesg);
69                 } else {
70                         return missingKeyScalar;
71                 }
72         }
73            
74         // i'm not real clear on how this is used - kellan :)
75         public boolean isEmpty() {
76         if (messages == null)
77                         return true;
78     else
79                         return false;
80   }
81  
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);
88 }