If the suffixed message key for integers is missing, fall back to the unsuffixed key instead of banking on the ".n" suffix.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5323 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Bruno Garcia
2008-08-15 21:45:36 +00:00
parent 172349abb3
commit 29204b385d
2 changed files with 30 additions and 42 deletions
+12 -18
View File
@@ -190,22 +190,14 @@ public class MessageBundle
} }
// look up our message string, selecting the proper plurality // look up our message string, selecting the proper plurality
// string if our first argument is an Integer // string if our first argument can be coaxed into an integer
var msg :String = getResourceString(key + getSuffix(args), false); var msg :String = getResourceString(key + getSuffix(args), false);
// if the base key is not found, look to see if we should try to // if the suffixed key was not found, just try the original key
// convert our first argument to an Integer and try again
if (msg == null) { if (msg == null) {
if (getResourceString(key + ".n", false) != null) { msg = getResourceString(key, false);
// try converting the first arg to an int
try { // if the msg is still missing, we have a problem
args[0] = StringUtil.parseInteger(args[0]);
msg = getResourceString(key + getSuffix(args), false);
} catch (err :Error) {
// fall through...
}
}
// if the msg is still null, we have a problem
if (msg == null) { if (msg == null) {
Log.getLog(this).warning("Missing translation message " + Log.getLog(this).warning("Missing translation message " +
"[bundle=" + _path + ", key=" + key + "]."); "[bundle=" + _path + ", key=" + key + "].");
@@ -221,15 +213,17 @@ 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 plurality processing if our first argument * to automatically perform plurality processing if our first argument
* is an {@link Integer}. * can be coaxed to an {@link Integer}.
*/ */
protected function getSuffix (args :Array) :String protected function getSuffix (args :Array) :String
{ {
if (args.length > 0 && args[0] is int) { if (args.length > 0) {
var count :int = int(args[0]);
switch (args[0]) { switch (args[0]) {
case 0: return ".0"; case NaN: break; // Fall out
case 1: return ".1"; case 0: return ".0";
default: return ".n"; case 1: return ".1";
default: return ".n";
} }
} }
return ""; return "";
+18 -24
View File
@@ -300,30 +300,18 @@ public class MessageBundle
// if the base key is not found, look to see if we should try to // if the base key is not found, look to see if we should try to
// convert our first argument to an Integer and try again // convert our first argument to an Integer and try again
if (msg == null) { if (msg == null) {
if (getResourceString(key + ".n", false) != null) { msg = getResourceString(key, false);
try {
// args could be a String[], we need to turn it into
// an Object[]
Object[] newargs = new Object[args.length];
System.arraycopy(args, 1, newargs, 1, args.length - 1);
newargs[0] = new Integer(args[0].toString());
args = newargs;
msg = getResourceString(key + getSuffix(args));
} catch (Exception e) {
log.warning("Failure doing automatic plural handling " +
"[bundle=" + _path + ", key=" + key +
", args=" + StringUtil.toString(args) +
", error=" + e + "].");
}
} else { 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 (key + StringUtil.toString(args));
} }
} }
return (msg != null) ? MessageFormat.format(MessageUtil.escape(msg), args) : return MessageFormat.format(MessageUtil.escape(msg), args);
(key + StringUtil.toString(args));
} }
/** /**
@@ -338,15 +326,21 @@ 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 plurality processing if our first argument * to automatically perform plurality processing if our first argument
* is an {@link Integer}. * can be coaxed to an {@link Integer}.
*/ */
protected String getSuffix (Object[] args) protected String getSuffix (Object[] args)
{ {
if (args[0] instanceof Integer) { if (args.length > 0) {
switch (((Integer)args[0]).intValue()) { try {
case 0: return ".0"; int count = (args[0] instanceof Integer) ? (Integer)args[0] :
case 1: return ".1"; Integer.parseInt(args[0].toString());
default: return ".n"; switch (count) {
case 0: return ".0";
case 1: return ".1";
default: return ".n";
}
} catch (NumberFormatException e) {
// Fall out
} }
} }
return ""; return "";