Whitespace

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6025 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Dave Hoover
2010-02-03 01:06:50 +00:00
parent 1420b06d3b
commit 49de1913d9
2 changed files with 72 additions and 99 deletions
@@ -55,7 +55,6 @@ public class DefaultMap<K, V> extends ForwardingMap<K, V>
return newMap(delegate, creator); return newMap(delegate, creator);
} }
/** /**
* Returns a Function that makes instances of the supplied class (using its no-args * Returns a Function that makes instances of the supplied class (using its no-args
* constructor) as default values. * constructor) as default values.
+72 -98
View File
@@ -33,19 +33,17 @@ import com.samskivert.util.StringUtil;
import static com.threerings.NaryaLog.log; import static com.threerings.NaryaLog.log;
/** /**
* A message bundle provides an easy mechanism by which to obtain * A message bundle provides an easy mechanism by which to obtain translated message strings from
* translated message strings from a resource bundle. It uses the {@link * a resource bundle. It uses the {@link MessageFormat} class to substitute arguments into the
* MessageFormat} class to substitute arguments into the translation * translation strings. Message bundles would generally be obtained via the {@link MessageManager},
* strings. Message bundles would generally be obtained via the {@link * but could be constructed individually if so desired.
* MessageManager}, but could be constructed individually if so desired.
*/ */
public class MessageBundle public class MessageBundle
{ {
/** /**
* Call this to "taint" any string that has been entered by an entity * Call this to "taint" any string that has been entered by an entity outside the application
* outside the application so that the translation code knows not to * so that the translation code knows not to attempt to translate this string when doing
* attempt to translate this string when doing recursive translations * recursive translations (see {@link #xlate}).
* (see {@link #xlate}).
*/ */
public static String taint (Object text) public static String taint (Object text)
{ {
@@ -53,8 +51,8 @@ public class MessageBundle
} }
/** /**
* Composes a message key with a single argument. The message can * Composes a message key with a single argument. The message can subsequently be translated
* subsequently be translated in a single call using {@link #xlate}. * in a single call using {@link #xlate}.
*/ */
public static String compose (String key, Object arg) public static String compose (String key, Object arg)
{ {
@@ -62,8 +60,8 @@ public class MessageBundle
} }
/** /**
* Composes a message key with an array of arguments. The message can * Composes a message key with an array of arguments. The message can subsequently be
* subsequently be translated in a single call using {@link #xlate}. * translated in a single call using {@link #xlate}.
*/ */
public static String compose (String key, Object... args) public static String compose (String key, Object... args)
{ {
@@ -71,8 +69,8 @@ public class MessageBundle
} }
/** /**
* Composes a message key with an array of arguments. The message can * Composes a message key with an array of arguments. The message can subsequently be
* subsequently be translated in a single call using {@link #xlate}. * translated in a single call using {@link #xlate}.
*/ */
public static String compose (String key, String... args) public static String compose (String key, String... args)
{ {
@@ -80,9 +78,8 @@ public class MessageBundle
} }
/** /**
* A convenience method for calling {@link #compose(String,Object[])} * A convenience method for calling {@link #compose(String,Object[])} with an array of
* with an array of arguments that will be automatically tainted (see * arguments that will be automatically tainted (see {@link #taint}).
* {@link #taint}).
*/ */
public static String tcompose (String key, Object... args) public static String tcompose (String key, Object... args)
{ {
@@ -106,9 +103,8 @@ public class MessageBundle
} }
/** /**
* A convenience method for calling {@link #compose(String,String[])} * A convenience method for calling {@link #compose(String,String[])} with an array of
* with an array of arguments that will be automatically tainted (see * arguments that will be automatically tainted (see {@link #taint}).
* {@link #taint}).
*/ */
public static String tcompose (String key, String... args) public static String tcompose (String key, String... args)
{ {
@@ -116,9 +112,8 @@ public class MessageBundle
} }
/** /**
* Returns a fully qualified message key which, when translated by * Returns a fully qualified message key which, when translated by some other bundle, will
* some other bundle, will know to resolve and utilize the supplied * know to resolve and utilize the supplied bundle to translate this particular key.
* bundle to translate this particular key.
*/ */
public static String qualify (String bundle, String key) public static String qualify (String bundle, String key)
{ {
@@ -136,8 +131,7 @@ public class MessageBundle
} }
/** /**
* Returns the unqualified portion of the key from a fully qualified * Returns the unqualified portion of the key from a fully qualified message key.
* message key.
* *
* @see #qualify * @see #qualify
*/ */
@@ -147,9 +141,8 @@ public class MessageBundle
} }
/** /**
* Initializes the message bundle which will obtain localized messages * Initializes the message bundle which will obtain localized messages from the supplied
* from the supplied resource bundle. The path is provided purely for * resource bundle. The path is provided purely for reporting purposes.
* reporting purposes.
*/ */
public void init (MessageManager msgmgr, String path, public void init (MessageManager msgmgr, String path,
ResourceBundle bundle, MessageBundle parent) ResourceBundle bundle, MessageBundle parent)
@@ -161,11 +154,10 @@ public class MessageBundle
} }
/** /**
* Obtains the translation for the specified message key. No arguments * Obtains the translation for the specified message key. No arguments are substituted into
* are substituted into the translated string. If a translation * the translated string. If a translation message does not exist for the specified key, an
* message does not exist for the specified key, an error is logged * error is logged and the key itself is returned so that the caller need not worry about
* and the key itself is returned so that the caller need not worry * handling a null response.
* about handling a null response.
*/ */
public String get (String key) public String get (String key)
{ {
@@ -180,11 +172,10 @@ public class MessageBundle
} }
/** /**
* Adds all messages whose key starts with the specified prefix to the * Adds all messages whose key starts with the specified prefix to the supplied collection.
* supplied collection.
* *
* @param includeParent if true, messages from our parent bundle (and its * @param includeParent if true, messages from our parent bundle (and its parent bundle, all
* parent bundle, all the way up the chain will be included). * the way up the chain will be included).
*/ */
public void getAll (String prefix, Collection<String> messages, boolean includeParent) public void getAll (String prefix, Collection<String> messages, boolean includeParent)
{ {
@@ -201,11 +192,11 @@ public class MessageBundle
} }
/** /**
* Adds all keys for messages whose key starts with the specified prefix to the * Adds all keys for messages whose key starts with the specified prefix to the supplied
* supplied collection. * collection.
* *
* @param includeParent if true, messages from our parent bundle (and its * @param includeParent if true, messages from our parent bundle (and its parent bundle, all
* parent bundle, all the way up the chain will be included). * the way up the chain will be included).
*/ */
public void getAllKeys (String prefix, Collection<String> keys, boolean includeParent) public void getAllKeys (String prefix, Collection<String> keys, boolean includeParent)
{ {
@@ -222,8 +213,7 @@ public class MessageBundle
} }
/** /**
* Returns true if we have a translation mapping for the supplied key, * Returns true if we have a translation mapping for the supplied key, false if not.
* false if not.
*/ */
public boolean exists (String key) public boolean exists (String key)
{ {
@@ -238,14 +228,12 @@ public class MessageBundle
return getResourceString(key, true); return getResourceString(key, true);
} }
/** /**
* Get a String from the resource bundle, or null if there was an * Get a String from the resource bundle, or null if there was an error.
* error.
* *
* @param key the resource key. * @param key the resource key.
* @param reportMissing whether or not the method should log an error * @param reportMissing whether or not the method should log an error if the resource didn't
* if the resource didn't exist. * exist.
*/ */
public String getResourceString (String key, boolean reportMissing) public String getResourceString (String key, boolean reportMissing)
{ {
@@ -275,35 +263,25 @@ public class MessageBundle
} }
/** /**
* Obtains the translation for the specified message key. The * Obtains the translation for the specified message key. The specified arguments are
* specified arguments are substituted into the translated string. * substituted into the translated string.
* *
* <p> If the first argument in the array is an {@link Integer} * <p> If the first argument in the array is an {@link Integer} object, a translation will be
* object, a translation will be selected accounting for plurality in * selected accounting for plurality in the following manner. Assume a message key of
* the following manner. Assume a message key of * <code>m.widgets</code>, the following translations should be defined: <pre> m.widgets.0 =
* <code>m.widgets</code>, the following translations should be * no widgets. m.widgets.1 = {0} widget. m.widgets.n = {0} widgets. </pre>
* defined:
* <pre>
* m.widgets.0 = no widgets.
* m.widgets.1 = {0} widget.
* m.widgets.n = {0} widgets.
* </pre>
* *
* The specified argument is substituted into the translated string as * The specified argument is substituted into the translated string as appropriate. Consider
* appropriate. Consider using: * using:
* *
* <pre> * <pre> m.widgets.n = {0,number,integer} widgets. </pre>
* m.widgets.n = {0,number,integer} widgets.
* </pre>
* *
* to obtain proper insertion of commas and dots as appropriate for * to obtain proper insertion of commas and dots as appropriate for the locale.
* the locale.
* *
* <p> See {@link MessageFormat} for more information on how the * <p> See {@link MessageFormat} for more information on how the substitution is performed. If
* substitution is performed. If a translation message does not exist * a translation message does not exist for the specified key, an error is logged and the key
* for the specified key, an error is logged and the key itself (plus * itself (plus the arguments) is returned so that the caller need not worry about handling a
* the arguments) is returned so that the caller need not worry about * null response.
* handling a null response.
*/ */
public String get (String key, Object... args) public String get (String key, Object... args)
{ {
@@ -319,8 +297,10 @@ public class MessageBundle
String msg = getResourceString(key + suffix, false); String msg = getResourceString(key + suffix, false);
if (msg == null) { if (msg == null) {
// Playing with fire: This only works because it's the same "" reference we return from getSuffix() // Playing with fire: This only works because it's the same "" reference we return
// Don't try this at home. Keep out of reach of children. If swallowed, consult StringUtil.isBlank() // from getSuffix()
// Don't try this at home. Keep out of reach of children. If swallowed, consult
// StringUtil.isBlank()
if (suffix != "") { if (suffix != "") {
// Try the original key // Try the original key
msg = getResourceString(key, false); msg = getResourceString(key, false);
@@ -328,7 +308,7 @@ public class MessageBundle
if (msg == null) { if (msg == null) {
log.warning("Missing translation message", "bundle", _path, "key", key, log.warning("Missing translation message", "bundle", _path, "key", key,
new Exception()); new Exception());
// return something bogus // return something bogus
return (key + StringUtil.toString(args)); return (key + StringUtil.toString(args));
@@ -339,8 +319,8 @@ public class MessageBundle
} }
/** /**
* Obtains the translation for the specified message key. The * Obtains the translation for the specified message key. The specified arguments are
* specified arguments are substituted into the translated string. * substituted into the translated string.
*/ */
public String get (String key, String... args) public String get (String key, String... args)
{ {
@@ -348,9 +328,8 @@ public class MessageBundle
} }
/** /**
* A helper function for {@link #get(String,Object[])} that allows us * A helper function for {@link #get(String,Object[])} that allows us to automatically perform
* to automatically perform plurality processing if our first argument * plurality processing if our first argument can be coaxed to an {@link Integer}.
* can be coaxed to an {@link Integer}.
*/ */
protected String getSuffix (Object[] args) protected String getSuffix (Object[] args)
{ {
@@ -371,23 +350,20 @@ public class MessageBundle
} }
/** /**
* Obtains the translation for the specified compound message key. A * Obtains the translation for the specified compound message key. A compound key contains the
* compound key contains the message key followed by a tab separated * message key followed by a tab separated list of message arguments which will be subsituted
* list of message arguments which will be subsituted into the * into the translation string.
* translation string.
* *
* <p> See {@link MessageFormat} for more information on how the * <p> See {@link MessageFormat} for more information on how the substitution is performed. If
* substitution is performed. If a translation message does not exist * a translation message does not exist for the specified key, an error is logged and the key
* for the specified key, an error is logged and the key itself (plus * itself (plus the arguments) is returned so that the caller need not worry about handling a
* the arguments) is returned so that the caller need not worry about * null response.
* handling a null response.
*/ */
public String xlate (String compoundKey) public String xlate (String compoundKey)
{ {
// if this is a qualified key, we need to pass the buck to the // if this is a qualified key, we need to pass the buck to the appropriate message bundle;
// appropriate message bundle; we have to do it here because we // we have to do it here because we want the compound arguments of this key to be
// want the compound arguments of this key to be translated in the // translated in the context of the containing message bundle qualification
// context of the containing message bundle qualification
if (compoundKey.startsWith(MessageUtil.QUAL_PREFIX)) { if (compoundKey.startsWith(MessageUtil.QUAL_PREFIX)) {
MessageBundle qbundle = _msgmgr.getBundle(getBundle(compoundKey)); MessageBundle qbundle = _msgmgr.getBundle(getBundle(compoundKey));
return qbundle.xlate(getUnqualifiedKey(compoundKey)); return qbundle.xlate(getUnqualifiedKey(compoundKey));
@@ -423,12 +399,10 @@ public class MessageBundle
return "[bundle=" + _bundle + ", path=" + _path + "]"; return "[bundle=" + _bundle + ", path=" + _path + "]";
} }
/** The message manager via whom we'll resolve fully qualified /** The message manager via whom we'll resolve fully qualified translation strings. */
* translation strings. */
protected MessageManager _msgmgr; protected MessageManager _msgmgr;
/** The path that identifies the resource bundle we are using to /** The path that identifies the resource bundle we are using to obtain our messages. */
* obtain our messages. */
protected String _path; protected String _path;
/** The resource bundle from which we obtain our messages. */ /** The resource bundle from which we obtain our messages. */