1 5 package xdoclet.util; 6 7 import java.text.MessageFormat ; 8 9 import java.util.MissingResourceException ; 10 import java.util.ResourceBundle ; 11 12 import org.apache.commons.logging.Log; 13 14 21 public final class Translator 22 { 23 26 private Translator() 27 { 28 } 29 30 37 public static java.lang.String getString(Class clazz, String resourceKey) 38 { 39 return getString(clazz, resourceKey, null); 40 } 41 42 51 public static java.lang.String getString(Class clazz, String resourceKey, String [] arguments) 52 { 53 return getString(clazz.getName() + "Messages", resourceKey, arguments); 54 } 55 56 63 public static java.lang.String getString(String bundleKey, String resourceKey) 64 { 65 return getString(bundleKey + "Messages", resourceKey, null); 66 } 67 68 77 public static java.lang.String getString(String bundleKey, String resourceKey, String [] arguments) 78 { 79 Log log = LogUtil.getLog(Translator.class, "getString"); 80 81 if (log.isDebugEnabled()) { 82 log.debug("bundleKey=" + bundleKey); 83 log.debug("resourceKey=" + resourceKey); 84 85 if (arguments == null) { 86 log.debug("no arguments"); 87 } 88 else { 89 log.debug("#arguments=" + arguments.length); 90 for (int i = 0; i < arguments.length; i++) { 91 log.debug("arg #" + i + '=' + arguments[i]); 92 } 93 } 94 } 95 96 try { 97 ResourceBundle bundle = getBundle(bundleKey); 98 String resource = bundle.getString(resourceKey.toLowerCase()); 99 100 log.debug("resource=" + resource); 101 102 String msg = MessageFormat.format(resource, arguments); 103 104 log.debug("return message: " + msg); 105 106 return msg; 107 } 108 catch (MissingResourceException e) { 109 log.debug("XDoclet MISSING RESOURCE: Can't locate resource '" + resourceKey + "' for bundle '" + bundleKey + "'."); 115 return resourceKey + " arguments: " + arguments; 116 } 117 } 118 119 private static String getPackageNameOfClass(Class clazz) 120 { 121 String class_name = clazz.getName(); 122 123 return class_name.substring(0, class_name.lastIndexOf('.')); 124 } 125 126 133 private static ResourceBundle getBundle(java.lang.String bundleKey) throws MissingResourceException 134 { 135 Log log = LogUtil.getLog(Translator.class, "getBundle"); 136 137 if (log.isDebugEnabled()) { 138 log.debug("bundleKey=" + bundleKey); 139 } 140 141 try { 142 return ResourceBundle.getBundle(bundleKey); 143 } 144 catch (MissingResourceException e) { 145 log.debug("bundle not found"); 147 throw e; 148 } 149 } 150 } 151 | Popular Tags |