1 package com.ca.commons.cbutil; 2 3 import java.text.MessageFormat ; 4 import java.util.*; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 9 22 23 public class CBIntText 24 { 25 static Locale locale = null; 26 27 static MessageFormat messageFormatter = null; 28 29 static Hashtable translations; 30 31 private static boolean errorGiven = false; 33 private static Logger log = Logger.getLogger(CBIntText.class.getName()); 34 38 39 private static boolean english = true; 40 41 49 50 public static void init(String bundleLocation, ClassLoader customLoader) 51 { 52 locale = Locale.getDefault(); 53 54 Locale def = Locale.getDefault(); 55 56 log.info("Default Locale is: " + def.getDisplayName()); 57 58 log.info("Using Locale: " + locale.getDisplayName() + " for " + locale.getDisplayCountry()); 59 log.info("language, localized for default locale is: " + def.getDisplayLanguage(locale)); 60 log.info("country name, localized for default locale: " + def.getDisplayCountry(locale)); 61 log.info("Default language, localized for your locale is: " + locale.getDisplayLanguage(def)); 62 log.info("Default country name, localized for your locale is: " + locale.getDisplayCountry(def)); 63 64 translations = new Hashtable(500); 65 66 addBundle(bundleLocation, customLoader); 67 68 if (!def.getLanguage().equals("en")) 69 english = false; 70 71 messageFormatter = new MessageFormat (""); 72 messageFormatter.setLocale(locale); 73 } 74 75 85 86 public static void addBundle(String bundleLocation, ClassLoader customLoader) 87 { 88 89 log.fine("adding resource bundle: " + bundleLocation + " using loader: " + customLoader.toString()); 90 91 int startSize = translations.size(); 92 93 if (locale == null) 94 { 95 log.warning(" ERROR: - CBIntText.addBundle() has been called before CBIntText was initialised! - ignoring call."); 96 return; 97 } 98 99 try 100 { 101 CBResourceBundle bundle = new CBResourceBundle(bundleLocation, locale); 103 104 String name = bundle.getString("name"); 105 log.info(" added language localizaton set: " + ((name == null) ? "(not named)" : name)); 106 107 110 Enumeration keys = bundle.getKeys(); 111 while (keys.hasMoreElements()) 112 { 113 Object key = keys.nextElement(); 114 if (translations.containsKey(key) == false) 115 { 116 log.fine("adding key: " + key + " trans: " + bundle.getString((String ) key)); 117 118 translations.put(key, bundle.getString((String ) key)); 119 } 120 } 121 122 } 123 catch (MissingResourceException e) 124 { 125 log.log(Level.WARNING, "unable to load resource bundle for " + locale.getDisplayLanguage(locale) + " in country " + locale.getDisplayCountry(locale), e); 126 } 127 finally 128 { 129 if (startSize < translations.size()) { 131 log.info(" locale language is " + locale.getDisplayLanguage(locale) + " in country " + locale.getDisplayCountry(locale)); 132 } 133 else 134 { 135 log.info("Unable to load language resource bundle (couldn't even find default 'JX.properties' file)!"); 136 } 137 } 138 } 139 140 146 147 public static String get(String key) 148 { 149 150 if (key == null) { 152 return "null key"; 153 } 154 155 if (translations == null || translations.size() == 0) { 157 if (errorGiven == false) { if (!english) log.info("Unable to translate (" + key + ") - can't find language resource bundle."); 160 errorGiven = true; 161 } 162 return key; } 164 165 try 166 { 167 String val = (String ) translations.get(key); if (val == null) { 170 if (!english) log.fine("Can't find translation for (" + key + ") - returning unchanged."); 171 return key; 172 } 173 return val; 174 } 175 catch (MissingResourceException e) 176 { 177 return key; } 179 } 180 181 192 193 public static String get(String key, Object [] args) 194 { 195 196 if (key == null) { 198 return "null key"; 199 } 200 String val = key; 201 202 if (translations == null || translations.size() == 0) { 204 if (errorGiven == false) { if (!english) log.info("Unable to translate (" + key + ") - can't find language resource bundle."); 207 errorGiven = true; 208 } 209 } 210 else 211 { 212 try 213 { 214 val = (String ) translations.get(key); if (val == null) { 217 if (!english) log.fine("Can't find translation for (" + key + ") - returning unchanged."); 218 val = key; } 220 } 221 catch (MissingResourceException e) 222 { 223 val = key; } 225 } 226 227 229 return MessageFormat.format(key, args); 231 } 232 } | Popular Tags |