1 11 12 package org.jivesoftware.util; 13 14 import java.text.*; 15 import java.util.*; 16 17 22 public class LocaleUtils { 23 24 private static String [][] timeZoneList = null; 25 26 private static Object timeZoneLock = new Object (); 27 28 private static final String resourceBaseName = "messenger_i18n"; 32 33 private LocaleUtils() { 34 } 35 36 43 public static Locale localeCodeToLocale(String localeCode) { 44 Locale locale = null; 45 if (localeCode != null) { 46 String language = null; 47 String country = null; 48 String variant = null; 49 if (localeCode != null) { 50 StringTokenizer tokenizer = new StringTokenizer(localeCode, "_"); 51 if (tokenizer.hasMoreTokens()) { 52 language = tokenizer.nextToken(); 53 if (tokenizer.hasMoreTokens()) { 54 country = tokenizer.nextToken(); 55 if (tokenizer.hasMoreTokens()) { 56 variant = tokenizer.nextToken(); 57 } 58 } 59 } 60 } 61 locale = new Locale(language, 62 ((country != null) ? country : ""), 63 ((variant != null) ? variant : "")); 64 } 65 return locale; 66 } 67 68 81 public static String [][] getTimeZoneList() { 82 if (timeZoneList == null) { 83 synchronized (timeZoneLock) { 84 if (timeZoneList == null) { 85 Date now = new Date(); 86 87 String [] timeZoneIDs = TimeZone.getAvailableIDs(); 88 Locale jiveLocale = JiveGlobals.getLocale(); 89 timeZoneList = new String [timeZoneIDs.length][2]; 91 for (int i = 0; i < timeZoneList.length; i++) { 92 String zoneID = timeZoneIDs[i]; 93 timeZoneList[i][0] = zoneID; 94 timeZoneList[i][1] = getTimeZoneName(zoneID, now, jiveLocale); 95 } 96 } 97 } 98 } 99 return timeZoneList; 100 } 101 102 112 private static String getTimeZoneName(String zoneID, Date now, Locale locale) { 113 TimeZone zone = TimeZone.getTimeZone(zoneID); 114 StringBuilder buf = new StringBuilder (); 115 int offset = zone.getRawOffset(); 117 if (zone.inDaylightTime(now) && zone.useDaylightTime()) { 118 offset += (int)JiveConstants.HOUR; 119 } 120 121 if (offset < 0) { 122 buf.append("GMT-"); 123 } 124 else { 125 buf.append("GMT+"); 126 } 127 offset = Math.abs(offset); 128 int hours = offset / (int)JiveConstants.HOUR; 129 int minutes = (offset % (int)JiveConstants.HOUR) / (int)JiveConstants.MINUTE; 130 buf.append(hours).append(":"); 131 if (minutes < 10) { 132 buf.append("0").append(minutes); 133 } 134 else { 135 buf.append(minutes); 136 } 137 buf.append(" - ").append(zoneID.replace('_', ' ').replace('/', ' ')).append(" "); 138 buf.append(zone.getDisplayName(true, TimeZone.SHORT, locale).replace('_', ' ').replace('/', ' ')); 139 return buf.toString(); 140 } 141 142 152 public static ResourceBundle getResourceBundle(String baseName, 153 Locale locale) { 154 return ResourceBundle.getBundle(baseName, locale); 155 } 156 157 165 public static String getLocalizedString(String key) { 166 return getLocalizedString(key, JiveGlobals.getLocale(), null); 167 } 168 169 179 public static String getLocalizedString(String key, Locale locale) { 180 return getLocalizedString(key, locale, null); 181 } 182 183 195 public static String getLocalizedString(String key, List arguments) { 196 return getLocalizedString(key, JiveGlobals.getLocale(), arguments); 197 } 198 199 212 public static String getLocalizedString(String key, Locale locale, List arguments) { 213 if (key == null) { 214 throw new NullPointerException ("Key cannot be null"); 215 } 216 if (locale == null) { 217 locale = JiveGlobals.getLocale(); 218 } 219 220 String value = ""; 221 222 try { 224 ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale); 226 value = bundle.getString(key); 227 if (arguments != null) { 229 MessageFormat messageFormat = new MessageFormat(""); 230 messageFormat.setLocale(bundle.getLocale()); 231 messageFormat.applyPattern(value); 232 try { 233 Format[] formats = messageFormat.getFormats(); 238 for (int i = 0; i < formats.length; i++) { 239 Format format = formats[i]; 240 if (format != null) { 241 if (format instanceof DateFormat) { 242 if (arguments.size() > i) { 243 Object val = arguments.get(i); 244 if (val instanceof String ) { 245 DateFormat dateFmt = (DateFormat)format; 246 try { 247 val = dateFmt.parse((String )val); 248 arguments.set(i, val); 249 } 250 catch (ParseException e) { 251 Log.error(e); 252 } 253 } 254 } 255 } 256 else if (format instanceof NumberFormat) { 257 if (arguments.size() > i) { 258 Object val = arguments.get(i); 259 if (val instanceof String ) { 260 NumberFormat nbrFmt = (NumberFormat)format; 261 try { 262 val = nbrFmt.parse((String )val); 263 arguments.set(i, val); 264 } 265 catch (ParseException e) { 266 Log.error(e); 267 } 268 } 269 } 270 } 271 } 272 } 273 value = messageFormat.format(arguments.toArray()); 274 } 275 catch (IllegalArgumentException e) { 276 Log.error("Unable to format resource string for key: " 277 + key + ", argument type not supported"); 278 value = ""; 279 } 280 } 281 } 282 catch (java.util.MissingResourceException mre) { 283 Log.warn("Missing resource for key: " + key 284 + " in locale " + locale.toString()); 285 value = ""; 286 } 287 288 return value; 289 } 290 291 294 public static String getLocalizedNumber(long number) { 295 return NumberFormat.getInstance().format(number); 296 } 297 298 301 public static String getLocalizedNumber(long number, Locale locale) { 302 return NumberFormat.getInstance(locale).format(number); 303 } 304 305 308 public static String getLocalizedNumber(double number) { 309 return NumberFormat.getInstance().format(number); 310 } 311 312 315 public static String getLocalizedNumber(double number, Locale locale) { 316 return NumberFormat.getInstance(locale).format(number); 317 } 318 } 319 | Popular Tags |