001: //////////////////////////////////////////////////////////////////////////////
002: // Clirr: compares two versions of a java library for binary compatibility
003: // Copyright (C) 2003 - 2005 Lars Kühne
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //////////////////////////////////////////////////////////////////////////////
019:
020: package net.sf.clirr.core;
021:
022: import java.util.Locale;
023: import java.util.Iterator;
024: import java.util.Collection;
025: import java.util.ResourceBundle;
026:
027: /**
028: * Class which is capable of translating a Message object into a localised
029: * string.
030: */
031: public final class MessageTranslator {
032: /**
033: * The default base name of the resource bundle from which message
034: * descriptions are read.
035: */
036: public static final String DFLT_RESOURCE_NAME = EventMessages.class
037: .getName();
038:
039: private Locale locale = Locale.getDefault();
040: private String resourceName = DFLT_RESOURCE_NAME;
041: private ResourceBundle messageText;
042:
043: /**
044: * This is a singleton class; to get an instance of this class, use
045: * the getInstance method.
046: */
047: public MessageTranslator() {
048: }
049:
050: /**
051: * Define the local language etc. Future calls to the getDesc method
052: * will attempt to use a properties file which is appropriate to that
053: * locale to look the message descriptions up in.
054: * <p>
055: * @param locale may be a valid Locale object, or null to indicate
056: * that the default locale is to be used.
057: */
058: public void setLocale(Locale locale) {
059: if (locale == null) {
060: locale = Locale.getDefault();
061: }
062: this .locale = locale;
063: this .messageText = null;
064: }
065:
066: /**
067: * Define the base name of the properties file that message
068: * translations are to be read from.
069: */
070: public void setResourceName(String resourceName) {
071: this .resourceName = resourceName;
072: this .messageText = null;
073: }
074:
075: /**
076: * Verify that the resource bundle for the currently set locale has
077: * a translation string available for every message object in the provided
078: * collection. This method is expected to be called from the unit tests,
079: * so that if a developer adds a new message the unit tests will fail until
080: * translations are also available for that new message.
081: * <p>
082: * @throws java.util.MissingResourceException if there is a registered
083: * message for which no description is present in the current locale's
084: * resources.
085: */
086: public void checkComplete(Collection messages) {
087: for (Iterator i = messages.iterator(); i.hasNext();) {
088: Message m = (Message) i.next();
089: getDesc(m);
090: }
091: }
092:
093: /**
094: * Given a Message object (containing a unique message id), look up
095: * that id in the appropriate resource bundle (properties file) for
096: * the set locale and return the text string associated with that
097: * message id.
098: * <p>
099: * Message ids in the properties file should be prefixed with an 'm',
100: * eg "m1000", "m5003".
101: * <p>
102: * @throws java.util.MissingResourceException if there is no entry in the
103: * message translation resource bundle for the specified message.
104: */
105: public String getDesc(Message msg) {
106: // load resource bundle
107: if (locale == null) {
108: locale = Locale.getDefault();
109: }
110:
111: if (messageText == null) {
112: messageText = ResourceBundle
113: .getBundle(resourceName, locale);
114: }
115:
116: return messageText.getString("m" + msg.getId());
117: }
118: }
|