Added support for a global message bundle to which other bundles fall back

if they can't find a message in their own list of strings.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1876 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-11-01 00:01:37 +00:00
parent dd4b1c69c3
commit b07fef8396
2 changed files with 30 additions and 4 deletions
@@ -1,5 +1,5 @@
// //
// $Id: MessageBundle.java,v 1.13 2002/08/16 19:31:39 mdb Exp $ // $Id: MessageBundle.java,v 1.14 2002/11/01 00:01:37 mdb Exp $
package com.threerings.util; package com.threerings.util;
@@ -23,10 +23,11 @@ public class MessageBundle
* from the supplied resource bundle. The path is provided purely for * from the supplied resource bundle. The path is provided purely for
* reporting purposes. * reporting purposes.
*/ */
public void init (String path, ResourceBundle bundle) public void init (String path, ResourceBundle bundle, MessageBundle parent)
{ {
_path = path; _path = path;
_bundle = bundle; _bundle = bundle;
_parent = parent;
} }
/** /**
@@ -72,6 +73,15 @@ public class MessageBundle
} }
} catch (MissingResourceException mre) { } catch (MissingResourceException mre) {
// if we have a parent, try getting the string from them
if (_parent != null) {
String value = _parent.getResourceString(key, false);
if (value != null) {
return value;
}
// if we didn't find it in our parent, we want to fall
// through and report missing appropriately
}
if (reportMissing) { if (reportMissing) {
Log.warning("Missing translation message " + Log.warning("Missing translation message " +
"[bundle=" + _path + ", key=" + key + "]."); "[bundle=" + _path + ", key=" + key + "].");
@@ -325,6 +335,9 @@ public class MessageBundle
/** The resource bundle from which we obtain our messages. */ /** The resource bundle from which we obtain our messages. */
protected ResourceBundle _bundle; protected ResourceBundle _bundle;
/** Our parent bundle if we're not the global bundle. */
protected MessageBundle _parent;
/** Text prefixed by this character will be considered tainted when /** Text prefixed by this character will be considered tainted when
* doing recursive translations and won't be translated. */ * doing recursive translations and won't be translated. */
protected static final String TAINT_CHAR = "~"; protected static final String TAINT_CHAR = "~";
@@ -1,5 +1,5 @@
// //
// $Id: MessageManager.java,v 1.4 2002/05/01 02:47:18 mdb Exp $ // $Id: MessageManager.java,v 1.5 2002/11/01 00:01:37 mdb Exp $
package com.threerings.util; package com.threerings.util;
@@ -24,6 +24,12 @@ import com.samskivert.util.StringUtil;
*/ */
public class MessageManager public class MessageManager
{ {
/** The name of the global resource bundle (which other bundles revert
* to if they can't locate a message within themselves). It must be
* named <code>global.properties</code> and live at the top of the
* bundle hierarchy. */
public static final String GLOBAL_BUNDLE = "global";
/** /**
* Constructs a message manager with the supplied resource prefix and * Constructs a message manager with the supplied resource prefix and
* the default locale. The prefix will be prepended to the path of all * the default locale. The prefix will be prepended to the path of all
@@ -51,6 +57,9 @@ public class MessageManager
if (!_prefix.endsWith(".")) { if (!_prefix.endsWith(".")) {
_prefix += "."; _prefix += ".";
} }
// load up the global bundle
_global = getBundle(GLOBAL_BUNDLE);
} }
/** /**
@@ -121,7 +130,7 @@ public class MessageManager
// initialize our message bundle, cache it and return it (if we // initialize our message bundle, cache it and return it (if we
// couldn't resolve the bundle, the message bundle will cope with // couldn't resolve the bundle, the message bundle will cope with
// it's null resource bundle) // it's null resource bundle)
bundle.init(path, rbundle); bundle.init(path, rbundle, _global);
_cache.put(path, bundle); _cache.put(path, bundle);
return bundle; return bundle;
} }
@@ -135,6 +144,10 @@ public class MessageManager
/** A cache of instantiated message bundles. */ /** A cache of instantiated message bundles. */
protected HashMap _cache = new HashMap(); protected HashMap _cache = new HashMap();
/** Our top-level message bundle, from which others obtain messages if
* they can't find them within themselves. */
protected MessageBundle _global;
/** A key that can contain the classname of a custom message bundle /** A key that can contain the classname of a custom message bundle
* class to be used to handle messages for a particular bundle. */ * class to be used to handle messages for a particular bundle. */
protected static final String MBUNDLE_CLASS_KEY = "msgbundle_class"; protected static final String MBUNDLE_CLASS_KEY = "msgbundle_class";