1 package jimm.util; 2 import java.util.HashMap ; 3 import java.util.ResourceBundle ; 4 import java.util.Locale ; 5 import java.util.MissingResourceException ; 6 7 17 public class I18N { 18 19 public static final String RESOURCE_FILE_PREFIX = "datavision"; 20 public static final String MENU_FILE_PREFIX = "menu"; 21 public static final String PAPER_FILE_PREFIX = "paper"; 22 23 protected static Locale locale; 24 protected static HashMap bundles; 25 26 static { 28 setLanguage(Locale.getDefault()); 29 } 30 31 38 public static void setLanguage(Locale l) { 39 if (!l.equals(locale)) { 40 locale = l; 41 bundles = new HashMap (); 42 } 43 } 44 45 55 public static String get(String key) { 56 return get(RESOURCE_FILE_PREFIX, key); 57 } 58 59 71 public static String get(String prefix, String key) { 72 if (key == null || prefix == null || prefix.length() == 0) return null; 73 if (key.length() == 0) return ""; 74 75 String val = ""; 76 try { 77 ResourceBundle strings = getBundle(prefix); 78 val = strings.getString(key); 79 if (val == null) val = key; 80 else val = val.trim(); 81 } 82 catch (MissingResourceException ex) { 83 val = key; 84 } 85 return val; 86 } 87 88 98 public static String getNullIfMissing(String key) { 99 return getNullIfMissing(RESOURCE_FILE_PREFIX, key); 100 } 101 102 114 public static String getNullIfMissing(String prefix, String key) { 115 if (key == null || prefix == null || prefix.length() == 0) return null; 116 if (key.length() == 0) return ""; 117 118 String val = null; 119 try { 120 ResourceBundle strings = getBundle(prefix); 121 val = strings.getString(key); 122 if (val == null) val = ""; 123 else val = val.trim(); 124 } 125 catch (MissingResourceException ex) { 126 val = null; 127 } 128 return val; 129 } 130 131 protected static ResourceBundle getBundle(String prefix) { 132 ResourceBundle bundle = (ResourceBundle )bundles.get(prefix); 133 if (bundle == null) { 134 bundle = ResourceBundle.getBundle(prefix, locale); 135 bundles.put(prefix, bundle); 136 } 137 return bundle; 138 } 139 140 } 141 | Popular Tags |