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
// 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);
// 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
// if the suffixed key was not found, just try the original key
if (msg == null) {
if (getResourceString(key + ".n", false) != null) {
// try converting the first arg to an int
try {
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
msg = getResourceString(key, false);
// if the msg is still missing, we have a problem
if (msg == null) {
Log.getLog(this).warning("Missing translation message " +
"[bundle=" + _path + ", key=" + key + "].");
@@ -221,15 +213,17 @@ public class MessageBundle
/**
* A helper function for {@link #get(String,Object[])} that allows us
* 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
{
if (args.length > 0 && args[0] is int) {
if (args.length > 0) {
var count :int = int(args[0]);
switch (args[0]) {
case 0: return ".0";
case 1: return ".1";
default: return ".n";
case NaN: break; // Fall out
case 0: return ".0";
case 1: return ".1";
default: return ".n";
}
}
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
// convert our first argument to an Integer and try again
if (msg == null) {
if (getResourceString(key + ".n", false) != null) {
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 + "].");
}
msg = getResourceString(key, false);
} else {
if (msg == null) {
log.warning("Missing translation message", "bundle", _path, "key", key,
new Exception());
// return something bogus
return (key + StringUtil.toString(args));
}
}
return (msg != null) ? MessageFormat.format(MessageUtil.escape(msg), args) :
(key + StringUtil.toString(args));
return MessageFormat.format(MessageUtil.escape(msg), args);
}
/**
@@ -338,15 +326,21 @@ public class MessageBundle
/**
* A helper function for {@link #get(String,Object[])} that allows us
* 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)
{
if (args[0] instanceof Integer) {
switch (((Integer)args[0]).intValue()) {
case 0: return ".0";
case 1: return ".1";
default: return ".n";
if (args.length > 0) {
try {
int count = (args[0] instanceof Integer) ? (Integer)args[0] :
Integer.parseInt(args[0].toString());
switch (count) {
case 0: return ".0";
case 1: return ".1";
default: return ".n";
}
} catch (NumberFormatException e) {
// Fall out
}
}
return "";