1 10 11 package org.mule.config.i18n; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import java.text.MessageFormat ; 17 import java.util.HashMap ; 18 import java.util.Locale ; 19 import java.util.Map ; 20 import java.util.MissingResourceException ; 21 import java.util.ResourceBundle ; 22 23 27 public class Messages implements CoreMessageConstants 28 { 29 32 protected static final Log logger = LogFactory.getLog(Messages.class); 33 34 public static final String DEFAULT_BUNDLE = "core"; 35 36 private static Map bundles = new HashMap (); 37 38 private static Object [] emptyArgs = new Object []{}; 39 40 public static String get(int code) 41 { 42 return getString(DEFAULT_BUNDLE, code, emptyArgs); 43 } 44 45 public static String get(int code, Object [] args) 46 { 47 if (args == null) 48 { 49 args = Messages.emptyArgs; 50 } 51 return getString(DEFAULT_BUNDLE, code, args); 52 } 53 54 public static String get(int code, Object arg1) 55 { 56 if (arg1 == null) 57 { 58 arg1 = "null"; 59 } 60 return getString(DEFAULT_BUNDLE, code, new Object []{arg1}); 61 } 62 63 public static String get(int code, Object arg1, Object arg2) 64 { 65 if (arg1 == null) 66 { 67 arg1 = "null"; 68 } 69 if (arg2 == null) 70 { 71 arg2 = "null"; 72 } 73 return getString(DEFAULT_BUNDLE, code, new Object []{arg1, arg2}); 74 } 75 76 public static String get(int code, Object arg1, Object arg2, Object arg3) 77 { 78 if (arg1 == null) 79 { 80 arg1 = "null"; 81 } 82 if (arg2 == null) 83 { 84 arg2 = "null"; 85 } 86 if (arg3 == null) 87 { 88 arg3 = "null"; 89 } 90 return getString(DEFAULT_BUNDLE, code, new Object []{arg1, arg2, arg3}); 91 } 92 93 public static String get(String bundle, int code) 94 { 95 return getString(bundle, code, emptyArgs); 96 } 97 98 public static String get(String bundle, int code, Object [] args) 99 { 100 if (args == null) 101 { 102 args = Messages.emptyArgs; 103 } 104 return getString(bundle, code, args); 105 } 106 107 public static String get(String bundle, int code, Object arg1) 108 { 109 if (arg1 == null) 110 { 111 arg1 = "null"; 112 } 113 return getString(bundle, code, new Object []{arg1}); 114 } 115 116 public static String get(String bundle, int code, Object arg1, Object arg2) 117 { 118 if (arg1 == null) 119 { 120 arg1 = "null"; 121 } 122 if (arg2 == null) 123 { 124 arg2 = "null"; 125 } 126 return getString(bundle, code, new Object []{arg1, arg2}); 127 } 128 129 public static String get(String bundle, int code, Object arg1, Object arg2, Object arg3) 130 { 131 if (arg1 == null) 132 { 133 arg1 = "null"; 134 } 135 if (arg2 == null) 136 { 137 arg2 = "null"; 138 } 139 if (arg3 == null) 140 { 141 arg3 = "null"; 142 } 143 return getString(bundle, code, new Object []{arg1, arg2, arg3}); 144 } 145 146 public static String getString(String bundle, int code, Object [] args) 147 { 148 String m = getBundle(bundle).getString(String.valueOf(code)); 149 if (m == null) 150 { 151 logger.error("Failed to find message for id " + code + " in resource bundle " + bundle); 152 return ""; 153 } 154 return MessageFormat.format(m, args); 155 } 156 157 protected static ResourceBundle getBundle(String name) 158 { 159 ResourceBundle bundle = (ResourceBundle )bundles.get(name); 160 if (bundle == null) 161 { 162 String path = "META-INF.services.org.mule.i18n." + name + "-messages"; 163 logger.debug("Loading resource bundle: " + path); 164 Locale locale = Locale.getDefault(); 165 try 166 { 167 bundle = ResourceBundle.getBundle(path, locale); 168 } 169 catch (MissingResourceException e) 170 { 171 logger.warn("Failed to find resource bundle using default Locale: " + locale.toString() 172 + ", defaulting to Locale.US. Error was: " + e.getMessage()); 173 bundle = ResourceBundle.getBundle(path); 174 } 175 bundles.put(name, bundle); 176 } 177 return bundle; 178 } 179 } 180 | Popular Tags |