diff --git a/com/gallery/GalleryRemote/util/GRI18n.java b/com/gallery/GalleryRemote/util/GRI18n.java index 39c65df..7bfe00e 100644 --- a/com/gallery/GalleryRemote/util/GRI18n.java +++ b/com/gallery/GalleryRemote/util/GRI18n.java @@ -12,6 +12,8 @@ import java.util.Locale; import java.util.ResourceBundle; import java.util.MissingResourceException; +import java.io.*; +import java.text.MessageFormat; public class GRI18n implements PreferenceNames { private static final String RESNAME = "com.gallery.GalleryRemote.resources.GRResources"; @@ -20,6 +22,7 @@ public class GRI18n implements PreferenceNames { private static GRI18n ourInstance; private Locale grLocale; private ResourceBundle grResBundle; + private MessageFormat grMsgFrmt; public synchronized static GRI18n getInstance() { if (ourInstance == null) { @@ -40,6 +43,7 @@ private GRI18n() { grLocale = new Locale(myLang, myCountry); } + grMsgFrmt = new MessageFormat(""); setResBoundle(); } @@ -50,22 +54,59 @@ public void setLocale(String language, String country) { } public String getString(String key) { + return getString(getCallerName(), key); + } + + public String getString(Object caller, String key) { + return getString(caller.getClass().getName(), key); + } + + public String getString(String key, Object [] params) { + return getString(getCallerName(), key, params); + } + + public String getString(Object caller, String key, Object [] params) { + return getString(caller.getClass().getName(), key, params); + } + + private String getString(String className, String key) { String msg; + String extKey = className + "." + key; try { - msg = grResBundle.getString(key); + msg = grResBundle.getString(extKey); } catch (NullPointerException e) { Log.log(Log.ERROR, MODULE, "Key null error"); Log.logException(Log.ERROR, MODULE, e); msg = "[NULLKEY]"; } catch (MissingResourceException e) { - Log.log(Log.INFO, MODULE, "Key [" + key + "] not defined"); + Log.log(Log.INFO, MODULE, "Key [" + extKey + "] not defined"); Log.logException(Log.INFO, MODULE, e); - msg = "["+key+"]"; + msg = "["+extKey+"]"; } return msg; + } + private String getString(String className, String key, Object [] params) { + String template, msg; + String extKey = className + "." + key; + try { + template = grResBundle.getString(extKey); + grMsgFrmt.applyPattern(template); + msg = grMsgFrmt.format(params); + } catch (NullPointerException e) { + Log.log(Log.ERROR, MODULE, "Key null error"); + Log.logException(Log.ERROR, MODULE, e); + msg = "[NULLKEY]"; + } catch (MissingResourceException e) { + Log.log(Log.INFO, MODULE, "Key [" + extKey + "] not defined"); + Log.logException(Log.INFO, MODULE, e); + msg = "["+extKey+"]"; + } + + return msg; + } private void setResBoundle() { try { @@ -74,8 +115,54 @@ private void setResBoundle() { Log.log(Log.ERROR, MODULE, "Resource bundle error"); Log.logException(Log.ERROR, MODULE, e); } + + grMsgFrmt.setLocale(grLocale); + } + private static String getCallerName() { + + + StringWriter sw = new StringWriter(); + new Throwable().printStackTrace(new PrintWriter(sw)); + BufferedReader sr = new BufferedReader(new StringReader(sw.getBuffer().toString())); + + String returnVal = ""; + + try { + int lineCount = 0; + String line; + + while( (line = sr.readLine()) != null ) { + if(lineCount == 3) { // stack depth 1 --> getCallerName() + // 2 getCallerName's caller + // 3 actual caller we're interested in + int start = line.indexOf("at ") + 3, + end = line.indexOf("("); + String [] returnVals; + String retTemp = line.substring(start, end); + returnVals = retTemp.split("\\."); + returnVal = returnVals[returnVals.length - 2]; + + } + lineCount++; + } + } + catch(IOException e) { + Log.log(Log.ERROR, MODULE, "Error reading the call stack..."); + Log.logException(Log.ERROR, MODULE, e); + } + + int initBegin = returnVal.indexOf("."); // trim off "." for initializers + if (initBegin != -1) + returnVal = returnVal.substring(0, initBegin); + + int dollarSignBegin = returnVal.indexOf("$"); + if (dollarSignBegin != -1) + returnVal = returnVal.substring(0, dollarSignBegin); + + return returnVal; + } }