1 7 package winstone; 8 9 import java.util.Locale ; 10 import java.util.ResourceBundle ; 11 12 18 public class WinstoneResourceBundle { 19 private ResourceBundle resources; 20 21 24 public WinstoneResourceBundle(String baseName) { 25 this.resources = ResourceBundle.getBundle(baseName); 26 } 27 28 public WinstoneResourceBundle(String baseName, Locale loc) { 29 this.resources = ResourceBundle.getBundle(baseName, loc); 30 } 31 32 public WinstoneResourceBundle(String baseName, Locale loc, ClassLoader cl) { 33 this.resources = ResourceBundle.getBundle(baseName, loc, cl); 34 } 35 36 39 public String getString(String key) { 40 return this.resources.getString(key); 41 } 42 43 46 public String getString(String key, String parameter) { 47 return getString(key, new String [] { parameter }); 48 } 49 50 53 public String getString(String key, String [] parameters) { 54 String myCopy = this.resources.getString(key); 55 if (parameters != null) 56 for (int n = 0; n < parameters.length; n++) 57 myCopy = globalReplace(myCopy, "[#" + n + "]", parameters[n]); 58 return myCopy; 59 } 60 61 64 public static String globalReplace(String input, String fromMarker, 65 String toValue) { 66 if (input == null) { 67 return null; 68 } else if (fromMarker == null) { 69 return input; 70 } 71 72 StringBuffer out = new StringBuffer (); 73 int index = 0; 74 int foundAt = input.indexOf(fromMarker, index); 75 while (foundAt != -1) { 76 out.append(input.substring(index, foundAt)); 77 out.append(toValue); 78 index = foundAt + fromMarker.length(); 79 foundAt = input.indexOf(fromMarker, index); 80 } 81 out.append(input.substring(index)); 82 return out.toString(); 83 } 84 85 public static String globalReplace(String input, String parameters[][]) { 86 if (parameters != null) 87 for (int n = 0; n < parameters.length; n++) 88 input = globalReplace(input, parameters[n][0], parameters[n][1]); 89 return input; 90 } 91 } 92 | Popular Tags |