1 10 package com.sun.enterprise.jbi.serviceengine.util.soap; 11 12 import java.text.MessageFormat ; 13 14 import java.util.Locale ; 15 import java.util.ResourceBundle ; 16 import java.util.logging.Logger ; 17 18 19 26 public class StringTranslator 27 { 28 31 private static final String LOGGER_NAME = "com.sun.jbi.common.soap"; 32 33 36 public static final String RESOURCE_BUNDLE_NAME = "LocalStrings"; 37 38 41 private static final String LOG_NEW_INSTANCE = 42 "New StringTranslator for package {0}, classLoader is {1}"; 43 44 47 private static final String LOG_CURRENT_LOCALE = "Current locale is {0}"; 48 49 52 private static final String LOG_UNABLE_TO_LOAD_BUNDLE = 53 "Unable to load resource bundle {0} for locale {1}: {2}"; 54 55 58 private static final String LOG_USING_BUNDLE = 59 "Using resource bundle for locale {0} instead."; 60 61 64 private static final String LOG_TRANSLATION_USING_FALLBACK = 65 "No translation for key={0} found in resource bundle for locale {1}, " 66 + "using locale {2} instead."; 67 68 72 private static final String LOG_NO_TRANSLATION_FOR_KEY = 73 "No translation for key={0} found in any resource bundle. " 74 + "Insert data is [{1}]."; 75 76 80 private static final String LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE = 81 "No translation for key={0} found in resource bundle for locale {1}. " 82 + "Insert data is [{2}]."; 83 84 87 private static final String MSG_NO_TRANSLATION = 88 "No translation available for message with key={0} and inserts=[{1}]."; 89 90 93 private Locale mDefaultLocale; 94 95 98 private Logger mLog; 99 100 106 private ResourceBundle mFallbackBundle; 107 108 111 private ResourceBundle mResourceBundle; 112 113 124 public StringTranslator( 125 String packageName, 126 ClassLoader classLoader) 127 { 128 mLog = Logger.getLogger(packageName); 129 130 String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME; 131 mDefaultLocale = Locale.getDefault(); 132 133 mFallbackBundle = null; 135 136 ResourceBundle englishBundle = null; 137 138 try 139 { 140 if (null == classLoader) 141 { 142 englishBundle = ResourceBundle.getBundle(bundleName, Locale.US); 143 } 144 else 145 { 146 englishBundle = 147 ResourceBundle.getBundle(bundleName, Locale.US, classLoader); 148 } 149 } 150 catch (java.util.MissingResourceException mrEx) 151 { 152 mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE, 153 new Object [] {bundleName, Locale.US, mrEx})); 154 } 155 156 if (mDefaultLocale.equals(Locale.US)) 161 { 162 mResourceBundle = englishBundle; 163 } 164 else 165 { 166 try 167 { 168 if (null == classLoader) 169 { 170 mResourceBundle = ResourceBundle.getBundle(bundleName); 171 mFallbackBundle = englishBundle; 172 } 173 else 174 { 175 mResourceBundle = 176 ResourceBundle.getBundle(bundleName, mDefaultLocale, 177 classLoader); 178 mFallbackBundle = englishBundle; 179 } 180 } 181 catch (java.util.MissingResourceException mrEx) 182 { 183 mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE, 184 new Object [] {bundleName, mDefaultLocale, mrEx})); 185 mLog.warning(MessageFormat.format(LOG_USING_BUNDLE, 186 new Object [] {Locale.US})); 187 mResourceBundle = englishBundle; 188 } 189 } 190 } 191 192 199 public String getString(String key) 200 { 201 Object [] inserts = new Object [0]; 202 203 return getString(key, inserts); 204 } 205 206 215 public String getString( 216 String key, 217 Object insert1) 218 { 219 Object [] inserts = {insert1}; 220 221 return getString(key, inserts); 222 } 223 224 234 public String getString( 235 String key, 236 Object insert1, 237 Object insert2) 238 { 239 Object [] inserts = {insert1, insert2}; 240 241 return getString(key, inserts); 242 } 243 244 255 public String getString( 256 String key, 257 Object insert1, 258 Object insert2, 259 Object insert3) 260 { 261 Object [] inserts = {insert1, insert2, insert3}; 262 263 return getString(key, inserts); 264 } 265 266 278 public String getString( 279 String key, 280 Object insert1, 281 Object insert2, 282 Object insert3, 283 Object insert4) 284 { 285 Object [] inserts = {insert1, insert2, insert3, insert4}; 286 287 return getString(key, inserts); 288 } 289 290 308 public String getString( 309 String key, 310 Object [] inserts) 311 { 312 String translated = null; 313 314 if (null != mResourceBundle) 315 { 316 try 317 { 318 translated = mResourceBundle.getString(key); 319 translated = MessageFormat.format(translated, inserts); 320 } 321 catch (java.util.MissingResourceException mrEx) 322 { 323 if (null != mFallbackBundle) 324 { 325 try 326 { 327 translated = mFallbackBundle.getString(key); 328 translated = MessageFormat.format(translated, inserts); 329 mLog.fine(MessageFormat.format( 330 LOG_TRANSLATION_USING_FALLBACK, 331 new Object [] {key, mDefaultLocale, Locale.US})); 332 } 333 catch (java.util.MissingResourceException mreEx) 334 { 335 String fi = formatInserts(inserts); 336 translated = 337 MessageFormat.format(MSG_NO_TRANSLATION, 338 new Object [] {key, fi}); 339 mLog.warning(MessageFormat.format( 340 LOG_NO_TRANSLATION_FOR_KEY, 341 new Object [] {key, fi})); 342 } 343 } 344 else 345 { 346 String fi = formatInserts(inserts); 347 translated = 348 MessageFormat.format(MSG_NO_TRANSLATION, 349 new Object [] {key, fi}); 350 mLog.warning(MessageFormat.format( 351 LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE, 352 new Object [] {key, mDefaultLocale, fi})); 353 } 354 } 355 } 356 else 357 { 358 translated = 359 MessageFormat.format(MSG_NO_TRANSLATION, 360 new Object [] {key, formatInserts(inserts)}); 361 } 362 363 return translated; 364 } 365 366 374 private String formatInserts(Object [] inserts) 375 { 376 StringBuffer formatted = new StringBuffer (""); 377 378 for (int i = 0; i < inserts.length; i++) 379 { 380 if (i > 0) 381 { 382 formatted.append(","); 383 } 384 385 formatted.append(inserts[i].toString()); 386 } 387 388 return formatted.toString(); 389 } 390 } 391 | Popular Tags |