Handle compound translation keys. We have to duplicate some stuff from

Narya in here, but I don't want to introduce that dependency. Moving the
Narya stuff into samskivert is sort of an option, but here (and in most
places) we don't need the full-blown multiple named resource file support
that Narya has. Maybe just Narya's ResourceBundle could be moved...
This commit is contained in:
Michael Bayne
2004-07-19 12:39:08 +00:00
parent 884f4100ee
commit 0d67faee44
3 changed files with 79 additions and 12 deletions
@@ -1,5 +1,5 @@
//
// $Id: StatusPanel.java,v 1.2 2004/07/07 10:45:20 mdb Exp $
// $Id: StatusPanel.java,v 1.3 2004/07/19 12:39:08 mdb Exp $
package com.threerings.getdown.launcher;
@@ -15,10 +15,15 @@ import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import com.samskivert.swing.Label;
import com.samskivert.swing.util.SwingUtil;
import com.samskivert.text.MessageUtil;
import com.samskivert.util.StringUtil;
import com.threerings.getdown.Log;
/**
* Displays download and patching status.
@@ -42,7 +47,7 @@ public class StatusPanel extends JComponent
{
_progress = percent;
String msg = (remaining > 1) ? "m.complete_remain" : "m.complete";
msg = _msgs.getString(msg);
msg = get(msg);
String label = MessageFormat.format(msg, new Object[] {
new Integer(percent), new Long(remaining) });
_newplab = new Label(label);
@@ -54,6 +59,7 @@ public class StatusPanel extends JComponent
*/
public void setStatus (String status)
{
status = xlate(status);
_newlab = new Label(status, Color.white, null);
_newlab.setTargetWidth(_spos.width);
repaint();
@@ -113,6 +119,58 @@ public class StatusPanel extends JComponent
return _psize;
}
/** Used by {@link #setStatus}. */
protected String xlate (String compoundKey)
{
// to be more efficient about creating unnecessary objects, we
// do some checking before splitting
int tidx = compoundKey.indexOf('|');
if (tidx == -1) {
return get(compoundKey);
} else {
String key = compoundKey.substring(0, tidx);
String argstr = compoundKey.substring(tidx+1);
String[] args = StringUtil.split(argstr, "|");
// unescape and translate the arguments
for (int i = 0; i < args.length; i++) {
// if the argument is tainted, do no further translation
// (it might contain |s or other fun stuff)
if (args[i].startsWith(MessageUtil.TAINT_CHAR)) {
args[i] = MessageUtil.unescape(args[i].substring(1));
} else {
args[i] = xlate(MessageUtil.unescape(args[i]));
}
}
return get(key, args);
}
}
/** Used by {@link #setStatus}. */
protected String get (String key, Object[] args)
{
String msg = get(key);
return (msg != null) ?
MessageFormat.format(MessageUtil.escape(msg), args)
: (key + StringUtil.toString(args));
}
/** Used by {@link #setStatus}, and {@link #setProgress}. */
protected String get (String key)
{
// if this string is tainted, we don't translate it, instead we
// simply remove the taint character and return it to the caller
if (key.startsWith(MessageUtil.TAINT_CHAR)) {
return key.substring(1);
}
try {
return _msgs.getString(key);
} catch (MissingResourceException mre) {
Log.warning("Missing translation message '" + key + "'.");
return key;
}
}
protected BufferedImage _bgimg;
protected Dimension _psize;
protected Rectangle _ppos, _spos;