From b07fef8396a1f9a231d48e917ab4974105d0c91b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 1 Nov 2002 00:01:37 +0000 Subject: [PATCH] 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 --- src/java/com/threerings/util/MessageBundle.java | 17 +++++++++++++++-- .../com/threerings/util/MessageManager.java | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/java/com/threerings/util/MessageBundle.java b/src/java/com/threerings/util/MessageBundle.java index 6c301c067..7dbdb0cc7 100644 --- a/src/java/com/threerings/util/MessageBundle.java +++ b/src/java/com/threerings/util/MessageBundle.java @@ -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; @@ -23,10 +23,11 @@ public class MessageBundle * from the supplied resource bundle. The path is provided purely for * reporting purposes. */ - public void init (String path, ResourceBundle bundle) + public void init (String path, ResourceBundle bundle, MessageBundle parent) { _path = path; _bundle = bundle; + _parent = parent; } /** @@ -72,6 +73,15 @@ public class MessageBundle } } 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) { Log.warning("Missing translation message " + "[bundle=" + _path + ", key=" + key + "]."); @@ -325,6 +335,9 @@ public class MessageBundle /** The resource bundle from which we obtain our messages. */ 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 * doing recursive translations and won't be translated. */ protected static final String TAINT_CHAR = "~"; diff --git a/src/java/com/threerings/util/MessageManager.java b/src/java/com/threerings/util/MessageManager.java index 0e87cff0f..002a7ab9b 100644 --- a/src/java/com/threerings/util/MessageManager.java +++ b/src/java/com/threerings/util/MessageManager.java @@ -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; @@ -24,6 +24,12 @@ import com.samskivert.util.StringUtil; */ 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 global.properties 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 * the default locale. The prefix will be prepended to the path of all @@ -51,6 +57,9 @@ public class MessageManager if (!_prefix.endsWith(".")) { _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 // couldn't resolve the bundle, the message bundle will cope with // it's null resource bundle) - bundle.init(path, rbundle); + bundle.init(path, rbundle, _global); _cache.put(path, bundle); return bundle; } @@ -135,6 +144,10 @@ public class MessageManager /** A cache of instantiated message bundles. */ 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 * class to be used to handle messages for a particular bundle. */ protected static final String MBUNDLE_CLASS_KEY = "msgbundle_class";