1 26 27 package org.objectweb.jonas_lib; 28 29 import java.text.MessageFormat ; 30 import java.util.HashMap ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.MissingResourceException ; 34 import java.util.ResourceBundle ; 35 36 37 43 public class I18n { 44 45 48 private static final String RESOURCE_BUNDLE_NAME = "I18n"; 49 50 53 private static Map bundles = null; 54 55 58 private ResourceBundle resourceBundle = null; 59 60 65 private I18n(String packageName, ClassLoader cl) { 66 67 String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME; 68 69 try { 71 Locale locale = Locale.getDefault(); 72 resourceBundle = ResourceBundle.getBundle(bundleName, locale, cl); 73 } catch (MissingResourceException mre) { 74 String err = "Error when trying to get a ResourceBundle for package '" + packageName + "' : " + mre.getMessage(); 75 throw new IllegalStateException (err); 76 } 77 } 78 79 84 public static I18n getInstance(Class c) { 85 return getInstance(c.getPackage().getName()); 86 } 87 88 94 public static I18n getInstance(Class c, ClassLoader cl) { 95 return getInstance(c.getPackage().getName(), cl); 96 } 97 98 103 public static I18n getInstance(String packageName) { 104 return getInstance(packageName, Thread.currentThread().getContextClassLoader()); 105 } 106 107 113 public static I18n getInstance(String packageName, ClassLoader cl) { 114 if (bundles == null) { 115 bundles = new HashMap (); 116 } 117 118 I18n i18n = (I18n) bundles.get(packageName); 119 if (i18n != null) { 120 return i18n; 121 } 122 123 124 i18n = new I18n(packageName, cl); 125 bundles.put(packageName, i18n); 126 return i18n; 127 } 128 129 135 public String getMessage(String key, Object [] args) { 136 String value = getMessage(key); 137 return MessageFormat.format(value, args); 138 } 139 140 141 146 public String getMessage(String key) { 147 String ret = null; 148 149 if (resourceBundle == null) { 151 return key; 152 } 153 154 try { 155 ret = resourceBundle.getString(key); 156 } catch (MissingResourceException mre) { 157 ret = key; 159 } 160 161 return ret; 162 } 163 164 170 public String getMessage(String key, Object obj) { 171 return getMessage(key, new Object [] {obj}); 172 } 173 174 175 182 public String getMessage(String key, Object obj1, Object obj2) { 183 return getMessage(key, new Object [] {obj1, obj2 }); 184 } 185 186 194 public String getMessage(String key, Object obj1, Object obj2, Object obj3) { 195 return getMessage(key, new Object [] {obj1, obj2, obj3 }); 196 } 197 198 199 208 public String getMessage(String key, Object obj1, Object obj2, Object obj3, Object obj4) { 209 return getMessage(key, new Object [] {obj1, obj2, obj3, obj4 }); 210 } 211 212 213 223 public String getMessage(String key, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) { 224 return getMessage(key, new Object [] {obj1, obj2, obj3, obj4, obj5 }); 225 } 226 227 228 239 public String getMessage(String key, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, Object obj6) { 240 return getMessage(key, new Object [] {obj1, obj2, obj3, obj4, obj5, obj6 }); 241 } 242 243 } 244 | Popular Tags |