From 0d67faee44a0aa8e45a146ce9353272045947e51 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 19 Jul 2004 12:39:08 +0000 Subject: [PATCH] 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... --- .../threerings/getdown/launcher/Getdown.java | 21 ++++--- .../getdown/launcher/StatusPanel.java | 62 ++++++++++++++++++- .../threerings/getdown/messages.properties | 8 ++- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/java/com/threerings/getdown/launcher/Getdown.java b/src/java/com/threerings/getdown/launcher/Getdown.java index 39ecdbf..0632512 100644 --- a/src/java/com/threerings/getdown/launcher/Getdown.java +++ b/src/java/com/threerings/getdown/launcher/Getdown.java @@ -1,5 +1,5 @@ // -// $Id: Getdown.java,v 1.12 2004/07/19 11:59:06 mdb Exp $ +// $Id: Getdown.java,v 1.13 2004/07/19 12:39:08 mdb Exp $ package com.threerings.getdown.launcher; @@ -12,6 +12,7 @@ import javax.imageio.ImageIO; import javax.swing.JFrame; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -95,7 +96,12 @@ public class Getdown extends Thread if (msg == null) { msg = "m.unknown_error"; } else if (!msg.startsWith("m.")) { - msg = MessageUtil.tcompose("m.init_error", msg); + // try to do something sensible based on the type of error + if (e instanceof FileNotFoundException) { + msg = MessageUtil.tcompose("m.missing_resource", msg); + } else { + msg = MessageUtil.tcompose("m.init_error", msg); + } } updateStatus(msg); } @@ -172,10 +178,8 @@ public class Getdown extends Thread } public void downloadFailed (Resource rsrc, Exception e) { - String msg = MessageFormat.format( - _msgs.getString("m.failure"), - new Object[] { e.getMessage() }); - updateStatus(msg); + updateStatus( + MessageUtil.tcompose("m.failure", e.getMessage())); Log.warning("Download failed [rsrc=" + rsrc + "]."); Log.logStackTrace(e); synchronized (lock) { @@ -261,7 +265,7 @@ public class Getdown extends Thread EventQueue.invokeLater(new Runnable() { public void run () { if (message != null) { - _status.setStatus(_msgs.getString(message)); + _status.setStatus(message); } if (percent >= 0) { _status.setProgress(percent, remaining); @@ -319,7 +323,8 @@ public class Getdown extends Thread }; protected Application _app; - protected Application.UpdateInterface _ifc; + protected Application.UpdateInterface _ifc = + new Application.UpdateInterface(); protected ResourceBundle _msgs; protected JFrame _frame; diff --git a/src/java/com/threerings/getdown/launcher/StatusPanel.java b/src/java/com/threerings/getdown/launcher/StatusPanel.java index d51658a..f05c731 100644 --- a/src/java/com/threerings/getdown/launcher/StatusPanel.java +++ b/src/java/com/threerings/getdown/launcher/StatusPanel.java @@ -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; diff --git a/src/java/com/threerings/getdown/messages.properties b/src/java/com/threerings/getdown/messages.properties index 788b6db..5722078 100644 --- a/src/java/com/threerings/getdown/messages.properties +++ b/src/java/com/threerings/getdown/messages.properties @@ -1,5 +1,5 @@ # -# $Id: messages.properties,v 1.3 2004/07/19 11:59:06 mdb Exp $ +# $Id: messages.properties,v 1.4 2004/07/19 12:39:08 mdb Exp $ # # Getdown translation messages @@ -26,10 +26,14 @@ m.unable_to_repair = We were unable to download the necessary files after \ m.unknown_error = The application has failed to launch due to some strange \ error from which we could not recover. Please visit the support section \ of the website for information on how to recover. -m.init_error = The application as failed to launch due to the following \ +m.init_error = The application has failed to launch due to the following \ error:\n{0}\n\nPlease visit the support section of the website for \ information on how to handle such problems. +m.missing_resource = The application has failed to launch due to a \ + missing resource:\n{0}\n\nPlease visit the support section of the \ + website for information on how to handle such problems. + # application/digest errors m.missing_appbase = The configuration file is missing the 'appbase'. m.invalid_version = The configuration file specifies an invalid version.