1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.clirr.core;
21
22 import java.util.Locale;
23 import java.util.Iterator;
24 import java.util.Collection;
25 import java.util.ResourceBundle;
26
27 /***
28 * Class which is capable of translating a Message object into a localised
29 * string.
30 */
31 public final class MessageTranslator
32 {
33 /***
34 * The default base name of the resource bundle from which message
35 * descriptions are read.
36 */
37 public static final String DFLT_RESOURCE_NAME = EventMessages.class.getName();
38
39 private Locale locale = Locale.getDefault();
40 private String resourceName = DFLT_RESOURCE_NAME;
41 private ResourceBundle messageText;
42
43 /***
44 * This is a singleton class; to get an instance of this class, use
45 * the getInstance method.
46 */
47 public MessageTranslator()
48 {
49 }
50
51 /***
52 * Define the local language etc. Future calls to the getDesc method
53 * will attempt to use a properties file which is appropriate to that
54 * locale to look the message descriptions up in.
55 * <p>
56 * @param locale may be a valid Locale object, or null to indicate
57 * that the default locale is to be used.
58 */
59 public void setLocale(Locale locale)
60 {
61 if (locale == null)
62 {
63 locale = Locale.getDefault();
64 }
65 this.locale = locale;
66 this.messageText = null;
67 }
68
69 /***
70 * Define the base name of the properties file that message
71 * translations are to be read from.
72 */
73 public void setResourceName(String resourceName)
74 {
75 this.resourceName = resourceName;
76 this.messageText = null;
77 }
78
79 /***
80 * Verify that the resource bundle for the currently set locale has
81 * a translation string available for every message object in the provided
82 * collection. This method is expected to be called from the unit tests,
83 * so that if a developer adds a new message the unit tests will fail until
84 * translations are also available for that new message.
85 * <p>
86 * @throws java.util.MissingResourceException if there is a registered
87 * message for which no description is present in the current locale's
88 * resources.
89 */
90 public void checkComplete(Collection messages)
91 {
92 for (Iterator i = messages.iterator(); i.hasNext();)
93 {
94 Message m = (Message) i.next();
95 getDesc(m);
96 }
97 }
98
99 /***
100 * Given a Message object (containing a unique message id), look up
101 * that id in the appropriate resource bundle (properties file) for
102 * the set locale and return the text string associated with that
103 * message id.
104 * <p>
105 * Message ids in the properties file should be prefixed with an 'm',
106 * eg "m1000", "m5003".
107 * <p>
108 * @throws java.util.MissingResourceException if there is no entry in the
109 * message translation resource bundle for the specified message.
110 */
111 public String getDesc(Message msg)
112 {
113
114 if (locale == null)
115 {
116 locale = Locale.getDefault();
117 }
118
119 if (messageText == null)
120 {
121 messageText = ResourceBundle.getBundle(resourceName, locale);
122 }
123
124 return messageText.getString("m" + msg.getId());
125 }
126 }