KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > clirr > core > MessageTranslator


1 //////////////////////////////////////////////////////////////////////////////
2
// Clirr: compares two versions of a java library for binary compatibility
3
// Copyright (C) 2003 - 2005 Lars Kühne
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
//////////////////////////////////////////////////////////////////////////////
19

20 package net.sf.clirr.core;
21
22 import java.util.Locale JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
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 JavaDoc DFLT_RESOURCE_NAME = EventMessages.class.getName();
38
39     private Locale JavaDoc locale = Locale.getDefault();
40     private String JavaDoc resourceName = DFLT_RESOURCE_NAME;
41     private ResourceBundle JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc messages)
91     {
92         for (Iterator JavaDoc 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 JavaDoc getDesc(Message msg)
112     {
113         // load resource bundle
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 }
127
Popular Tags