1 23 24 package com.sun.jdo.spi.persistence.utility; 25 26 import java.util.*; 27 import java.text.MessageFormat ; 28 29 public class I18NHelper { 30 private static final String bundleSuffix = ".Bundle"; private static Hashtable bundles = new Hashtable(); 32 private static Locale locale = Locale.getDefault(); 33 34 37 public I18NHelper() { 38 } 39 40 43 public static ResourceBundle loadBundle(String bundleName) { 44 return loadBundle(bundleName, I18NHelper.class.getClassLoader()); 45 } 46 47 50 public static ResourceBundle loadBundle(String bundleName, ClassLoader loader) { 51 ResourceBundle messages = (ResourceBundle)bundles.get(bundleName); 52 53 if (messages == null) { 55 messages = ResourceBundle.getBundle(bundleName, locale, loader); 56 bundles.put(bundleName, messages); 57 } 58 return messages; 59 } 60 61 65 public static ResourceBundle loadBundle(Class classObject) { 66 return loadBundle( 67 JavaTypeHelper.getPackageName(classObject.getName()) + bundleSuffix, 68 classObject.getClassLoader()); 69 } 70 71 72 75 final public static String getMessage(ResourceBundle messages, String messageKey) 76 { 77 return messages.getString(messageKey); 78 } 79 80 83 final public static String getMessage(ResourceBundle messages, String messageKey, Object msgArgs[]) 84 { 85 for (int i=0; i<msgArgs.length; i++) { 86 if (msgArgs[i] == null) msgArgs[i] = ""; } 88 MessageFormat formatter = new MessageFormat (messages.getString(messageKey)); 89 return formatter.format(msgArgs); 90 } 91 94 final public static String getMessage(ResourceBundle messages, String messageKey, String arg) 95 { 96 Object []args = {arg}; 97 return getMessage(messages, messageKey, args); 98 } 99 102 final public static String getMessage(ResourceBundle messages, String messageKey, String arg1, 103 String arg2) 104 { 105 Object []args = {arg1, arg2}; 106 return getMessage(messages, messageKey, args); 107 } 108 111 final public static String getMessage(ResourceBundle messages, String messageKey, String arg1, 112 String arg2, String arg3) 113 { 114 Object []args = {arg1, arg2, arg3}; 115 return getMessage(messages, messageKey, args); 116 } 117 121 final public static String getMessage(ResourceBundle messages, String messageKey, Object arg) 122 { 123 Object []args = {arg}; 124 return getMessage(messages, messageKey, args); 125 } 126 129 final public static String getMessage(ResourceBundle messages, String messageKey, int arg) 130 { 131 Object []args = {new Integer (arg)}; 132 return getMessage(messages, messageKey, args); 133 } 134 137 final public static String getMessage(ResourceBundle messages, String messageKey, boolean arg) 138 { 139 Object []args = {String.valueOf(arg)}; 140 return getMessage(messages, messageKey, args); 141 } 142 143 } 144 | Popular Tags |