LocaleUtils.java 24.1 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004-2005 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10 11 12 13
 */

package org.jivesoftware.util;

14 15 16
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
17

18
import java.text.*;
19
import java.util.*;
20
import java.util.concurrent.ConcurrentHashMap;
Matt Tucker's avatar
Matt Tucker committed
21 22 23 24 25 26 27 28

/**
 * A set of methods for retrieving and converting locale specific strings and numbers.
 *
 * @author Jive Software
 */
public class LocaleUtils {

29
    private static final Map<Locale, String[][]> timeZoneLists =
30
            new ConcurrentHashMap<Locale, String[][]>();
Matt Tucker's avatar
Matt Tucker committed
31 32 33

    // The basename to use for looking up the appropriate resource bundles
    // TODO - extract this out into a test that grabs the resource name from JiveGlobals
34 35
    // TODO and defaults to openfire_i18n if nothing set.
    private static final String resourceBaseName = "openfire_i18n";
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    private LocaleUtils() {
    }

    /**
     * Converts a locale string like "en", "en_US" or "en_US_win" to a Java
     * locale object. If the conversion fails, null is returned.
     *
     * @param localeCode the locale code for a Java locale. See the {@link java.util.Locale}
     *                   class for more details.
     */
    public static Locale localeCodeToLocale(String localeCode) {
        Locale locale = null;
        if (localeCode != null) {
            String language = null;
            String country = null;
            String variant = null;
53 54 55
            StringTokenizer tokenizer = new StringTokenizer(localeCode, "_");
            if (tokenizer.hasMoreTokens()) {
                language = tokenizer.nextToken();
Matt Tucker's avatar
Matt Tucker committed
56
                if (tokenizer.hasMoreTokens()) {
57
                    country = tokenizer.nextToken();
Matt Tucker's avatar
Matt Tucker committed
58
                    if (tokenizer.hasMoreTokens()) {
59
                        variant = tokenizer.nextToken();
Matt Tucker's avatar
Matt Tucker committed
60 61 62 63
                    }
                }
            }
            locale = new Locale(language,
64 65
                    ((country != null) ? country : ""),
                    ((variant != null) ? variant : ""));
Matt Tucker's avatar
Matt Tucker committed
66 67 68 69
        }
        return locale;
    }

70 71 72
    // The list of supported timezone ids. The list tries to include all of the relevant
    // time zones for the world without any extraneous zones.
    private static String[] timeZoneIds = new String[]{"GMT",
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            "Pacific/Apia",
            "HST",
            "AST",
            "America/Los_Angeles",
            "America/Phoenix",
            "America/Mazatlan",
            "America/Denver",
            "America/Belize",
            "America/Chicago",
            "America/Mexico_City",
            "America/Regina",
            "America/Bogota",
            "America/New_York",
            "America/Indianapolis",
            "America/Halifax",
            "America/Caracas",
            "America/Santiago",
            "America/St_Johns",
            "America/Sao_Paulo",
            "America/Buenos_Aires",
            "America/Godthab",
            "Atlantic/South_Georgia",
            "Atlantic/Azores",
            "Atlantic/Cape_Verde",
            "Africa/Casablanca",
            "Europe/Dublin",
            "Europe/Berlin",
            "Europe/Belgrade",
            "Europe/Paris",
            "Europe/Warsaw",
            "ECT",
            "Europe/Athens",
            "Europe/Bucharest",
            "Africa/Cairo",
            "Africa/Harare",
            "Europe/Helsinki",
            "Asia/Jerusalem",
            "Asia/Baghdad",
            "Asia/Kuwait",
            "Europe/Moscow",
            "Africa/Nairobi",
            "Asia/Tehran",
            "Asia/Muscat",
            "Asia/Baku",
            "Asia/Kabul",
            "Asia/Yekaterinburg",
            "Asia/Karachi",
            "Asia/Calcutta",
            "Asia/Katmandu",
            "Asia/Almaty",
            "Asia/Dhaka",
            "Asia/Colombo",
            "Asia/Rangoon",
            "Asia/Bangkok",
            "Asia/Krasnoyarsk",
            "Asia/Hong_Kong",
            "Asia/Irkutsk",
            "Asia/Kuala_Lumpur",
            "Australia/Perth",
            "Asia/Taipei",
            "Asia/Tokyo",
            "Asia/Seoul",
            "Asia/Yakutsk",
            "Australia/Adelaide",
            "Australia/Darwin",
            "Australia/Brisbane",
            "Australia/Sydney",
            "Pacific/Guam",
            "Australia/Hobart",
            "Asia/Vladivostok",
            "Pacific/Noumea",
            "Pacific/Auckland",
            "Pacific/Fiji",
            "Pacific/Tongatapu"
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    };

    // A mapping from the supported timezone ids to friendly english names.
    private static final Map<String, String> nameMap = new HashMap<String, String>();

    static {
        nameMap.put(timeZoneIds[0], "International Date Line West");
        nameMap.put(timeZoneIds[1], "Midway Island, Samoa");
        nameMap.put(timeZoneIds[2], "Hawaii");
        nameMap.put(timeZoneIds[3], "Alaska");
        nameMap.put(timeZoneIds[4], "Pacific Time (US & Canada); Tijuana");
        nameMap.put(timeZoneIds[5], "Arizona");
        nameMap.put(timeZoneIds[6], "Chihuahua, La Pax, Mazatlan");
        nameMap.put(timeZoneIds[7], "Mountain Time (US & Canada)");
        nameMap.put(timeZoneIds[8], "Central America");
        nameMap.put(timeZoneIds[9], "Central Time (US & Canada)");
        nameMap.put(timeZoneIds[10], "Guadalajara, Mexico City, Monterrey");
        nameMap.put(timeZoneIds[11], "Saskatchewan");
        nameMap.put(timeZoneIds[12], "Bogota, Lima, Quito");
        nameMap.put(timeZoneIds[13], "Eastern Time (US & Canada)");
        nameMap.put(timeZoneIds[14], "Indiana (East)");
        nameMap.put(timeZoneIds[15], "Atlantic Time (Canada)");
        nameMap.put(timeZoneIds[16], "Caracas, La Paz");
        nameMap.put(timeZoneIds[17], "Santiago");
        nameMap.put(timeZoneIds[18], "Newfoundland");
        nameMap.put(timeZoneIds[19], "Brasilia");
        nameMap.put(timeZoneIds[20], "Buenos Aires, Georgetown");
        nameMap.put(timeZoneIds[21], "Greenland");
        nameMap.put(timeZoneIds[22], "Mid-Atlantic");
        nameMap.put(timeZoneIds[23], "Azores");
        nameMap.put(timeZoneIds[24], "Cape Verde Is.");
        nameMap.put(timeZoneIds[25], "Casablanca, Monrovia");
        nameMap.put(timeZoneIds[26], "Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London");
        nameMap.put(timeZoneIds[27], "Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna");
        nameMap.put(timeZoneIds[28], "Belgrade, Bratislava, Budapest, Ljubljana, Prague");
        nameMap.put(timeZoneIds[29], "Brussels, Copenhagen, Madrid, Paris");
        nameMap.put(timeZoneIds[30], "Sarajevo, Skopje, Warsaw, Zagreb");
        nameMap.put(timeZoneIds[31], "West Central Africa");
        nameMap.put(timeZoneIds[32], "Athens, Istanbul, Minsk");
        nameMap.put(timeZoneIds[33], "Bucharest");
        nameMap.put(timeZoneIds[34], "Cairo");
        nameMap.put(timeZoneIds[35], "Harare, Pretoria");
        nameMap.put(timeZoneIds[36], "Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius");
        nameMap.put(timeZoneIds[37], "Jerusalem");
        nameMap.put(timeZoneIds[38], "Baghdad");
        nameMap.put(timeZoneIds[39], "Kuwait, Riyadh");
        nameMap.put(timeZoneIds[40], "Moscow, St. Petersburg, Volgograd");
        nameMap.put(timeZoneIds[41], "Nairobi");
        nameMap.put(timeZoneIds[42], "Tehran");
        nameMap.put(timeZoneIds[43], "Abu Dhabi, Muscat");
        nameMap.put(timeZoneIds[44], "Baku, Tbilisi, Muscat");
        nameMap.put(timeZoneIds[45], "Kabul");
        nameMap.put(timeZoneIds[46], "Ekaterinburg");
        nameMap.put(timeZoneIds[47], "Islamabad, Karachi, Tashkent");
        nameMap.put(timeZoneIds[48], "Chennai, Kolkata, Mumbai, New Dehli");
        nameMap.put(timeZoneIds[49], "Kathmandu");
        nameMap.put(timeZoneIds[50], "Almaty, Novosibirsk");
        nameMap.put(timeZoneIds[51], "Astana, Dhaka");
        nameMap.put(timeZoneIds[52], "Sri Jayawardenepura");
        nameMap.put(timeZoneIds[53], "Rangoon");
        nameMap.put(timeZoneIds[54], "Bangkok, Hanoi, Jakarta");
        nameMap.put(timeZoneIds[55], "Krasnoyarsk");
        nameMap.put(timeZoneIds[56], "Beijing, Chongqing, Hong Kong, Urumqi");
        nameMap.put(timeZoneIds[57], "Irkutsk, Ulaan Bataar");
Gaston Dombiak's avatar
Gaston Dombiak committed
211
        nameMap.put(timeZoneIds[58], "Kuala Lumpur, Singapore");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        nameMap.put(timeZoneIds[59], "Perth");
        nameMap.put(timeZoneIds[60], "Taipei");
        nameMap.put(timeZoneIds[61], "Osaka, Sapporo, Tokyo");
        nameMap.put(timeZoneIds[62], "Seoul");
        nameMap.put(timeZoneIds[63], "Yakutsk");
        nameMap.put(timeZoneIds[64], "Adelaide");
        nameMap.put(timeZoneIds[65], "Darwin");
        nameMap.put(timeZoneIds[66], "Brisbane");
        nameMap.put(timeZoneIds[67], "Canberra, Melbourne, Sydney");
        nameMap.put(timeZoneIds[68], "Guam, Port Moresby");
        nameMap.put(timeZoneIds[69], "Hobart");
        nameMap.put(timeZoneIds[70], "Vladivostok");
        nameMap.put(timeZoneIds[71], "Magadan, Solomon Is., New Caledonia");
        nameMap.put(timeZoneIds[72], "Auckland, Wellington");
        nameMap.put(timeZoneIds[73], "Fiji, Kamchatka, Marshall Is.");
        nameMap.put(timeZoneIds[74], "Nuku'alofa");
    }

Matt Tucker's avatar
Matt Tucker committed
230 231 232 233 234
    /**
     * Returns a list of all available time zone's as a String [][]. The first
     * entry in each list item is the timeZoneID, and the second is the
     * display name.<p>
     * <p/>
235 236 237 238 239
     * The list of time zones attempts to be inclusive of all of the worlds
     * zones while being as concise as possible. For "en" language locales
     * the name is a friendly english name. For non-"en" language locales
     * the standard JDK name is used for the given Locale. The GMT+/- time
     * is also included for readability.
Matt Tucker's avatar
Matt Tucker committed
240 241 242 243 244
     *
     * @return a list of time zones, as a tuple of the zime zone ID, and its
     *         display name.
     */
    public static String[][] getTimeZoneList() {
245 246 247
        Locale jiveLocale = JiveGlobals.getLocale();

        String[][] timeZoneList = timeZoneLists.get(jiveLocale);
Matt Tucker's avatar
Matt Tucker committed
248
        if (timeZoneList == null) {
249 250 251 252 253 254 255
            String[] timeZoneIDs = timeZoneIds;
            // Now, create String[][] using the unique zones.
            timeZoneList = new String[timeZoneIDs.length][2];
            for (int i = 0; i < timeZoneList.length; i++) {
                String zoneID = timeZoneIDs[i];
                timeZoneList[i][0] = zoneID;
                timeZoneList[i][1] = getTimeZoneName(zoneID, jiveLocale);
Matt Tucker's avatar
Matt Tucker committed
256
            }
257 258 259

            // Add the new list to the map of locales to lists
            timeZoneLists.put(jiveLocale, timeZoneList);
Matt Tucker's avatar
Matt Tucker committed
260
        }
261

Matt Tucker's avatar
Matt Tucker committed
262 263 264 265 266
        return timeZoneList;
    }

    /**
     * Returns the display name for a time zone. The display name is the name
267 268
     * specified by the Java TimeZone class for non-"en" locales or a friendly english
     * name for "en", with the addition of the GMT offset
Matt Tucker's avatar
Matt Tucker committed
269 270 271 272 273 274
     * for human readability.
     *
     * @param zoneID the time zone to get the name for.
     * @param locale the locale to use.
     * @return the display name for the time zone.
     */
275
    public static String getTimeZoneName(String zoneID, Locale locale) {
Matt Tucker's avatar
Matt Tucker committed
276
        TimeZone zone = TimeZone.getTimeZone(zoneID);
277
        StringBuffer buf = new StringBuffer();
Matt Tucker's avatar
Matt Tucker committed
278 279
        // Add in the GMT part to the name. First, figure out the offset.
        int offset = zone.getRawOffset();
280
        if (zone.inDaylightTime(new Date()) && zone.useDaylightTime()) {
281
            offset += (int)JiveConstants.HOUR;
Matt Tucker's avatar
Matt Tucker committed
282 283
        }

284
        buf.append("(");
Matt Tucker's avatar
Matt Tucker committed
285 286 287 288 289 290 291
        if (offset < 0) {
            buf.append("GMT-");
        }
        else {
            buf.append("GMT+");
        }
        offset = Math.abs(offset);
292 293
        int hours = offset / (int)JiveConstants.HOUR;
        int minutes = (offset % (int)JiveConstants.HOUR) / (int)JiveConstants.MINUTE;
Matt Tucker's avatar
Matt Tucker committed
294 295 296 297 298 299 300
        buf.append(hours).append(":");
        if (minutes < 10) {
            buf.append("0").append(minutes);
        }
        else {
            buf.append(minutes);
        }
301 302 303 304 305 306 307 308 309 310 311 312 313
        buf.append(") ");

        // Use a friendly english timezone name if the locale is en, otherwise use the timezone id
        if ("en".equals(locale.getLanguage())) {
            String name = nameMap.get(zoneID);
            if (name == null) {
                name = zoneID;
            }

            buf.append(name);
        }
        else {
            buf.append(
314 315
                    zone.getDisplayName(true, TimeZone.LONG, locale).replace('_', ' ').replace('/',
                            ' '));
316 317
        }

Matt Tucker's avatar
Matt Tucker committed
318 319 320 321 322 323 324 325 326 327
        return buf.toString();
    }

    /**
     * Returns the specified resource bundle, which is a properties file
     * that aids in localization of skins. This method is handy since it
     * uses the class loader that other Jive classes are loaded from (hence,
     * it can load bundles that are stored in jive.jar).
     *
     * @param baseName the name of the resource bundle to load.
Matt Tucker's avatar
Matt Tucker committed
328
     * @param locale the desired Locale.
Matt Tucker's avatar
Matt Tucker committed
329 330 331 332 333 334 335 336 337 338 339 340
     * @return the specified resource bundle, if it exists.
     */
    public static ResourceBundle getResourceBundle(String baseName,
                                                   Locale locale) {
        return ResourceBundle.getBundle(baseName, locale);
    }

    /**
     * Returns an internationalized string loaded from a resource bundle.
     * The locale used will be the locale specified by JiveGlobals.getLocale().
     *
     * @param key the key to use for retrieving the string from the
Matt Tucker's avatar
Matt Tucker committed
341
     *      appropriate resource bundle.
Matt Tucker's avatar
Matt Tucker committed
342 343 344
     * @return the localized string.
     */
    public static String getLocalizedString(String key) {
345 346 347 348 349
        Locale locale = JiveGlobals.getLocale();

        ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale);

        return getLocalizedString(key, locale, null, bundle);
Matt Tucker's avatar
Matt Tucker committed
350 351 352 353 354 355
    }

    /**
     * Returns an internationalized string loaded from a resource bundle using
     * the passed in Locale.
     *
Matt Tucker's avatar
Matt Tucker committed
356 357
     * @param key the key to use for retrieving the string from the
     *      appropriate resource bundle.
Matt Tucker's avatar
Matt Tucker committed
358
     * @param locale the locale to use for retrieving the appropriate
Matt Tucker's avatar
Matt Tucker committed
359
     *      locale-specific string.
Matt Tucker's avatar
Matt Tucker committed
360 361 362
     * @return the localized string.
     */
    public static String getLocalizedString(String key, Locale locale) {
363 364 365
        ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale);

        return getLocalizedString(key, locale, null, bundle);
Matt Tucker's avatar
Matt Tucker committed
366 367 368 369 370 371 372 373
    }

    /**
     * Returns an internationalized string loaded from a resource bundle using
     * the locale specified by JiveGlobals.getLocale() substituting the passed
     * in arguments. Substitution is handled using the
     * {@link java.text.MessageFormat} class.
     *
Matt Tucker's avatar
Matt Tucker committed
374 375
     * @param key the key to use for retrieving the string from the
     *      appropriate resource bundle.
Matt Tucker's avatar
Matt Tucker committed
376
     * @param arguments a list of objects to use which are formatted, then
Matt Tucker's avatar
Matt Tucker committed
377
     *      inserted into the pattern at the appropriate places.
Matt Tucker's avatar
Matt Tucker committed
378 379 380
     * @return the localized string.
     */
    public static String getLocalizedString(String key, List arguments) {
381 382 383 384 385 386 387 388
        Locale locale = JiveGlobals.getLocale();

        ResourceBundle bundle = ResourceBundle.getBundle(resourceBaseName, locale);
        return getLocalizedString(key, locale, arguments, bundle);
    }

    /**
     * Returns an internationalized string loaded from a resource bundle from the passed
Matt Tucker's avatar
Matt Tucker committed
389 390
     * in plugin. If the plugin name is <tt>null</tt>, the key will be looked up using
     * the standard resource bundle.
391
     *
Matt Tucker's avatar
Matt Tucker committed
392 393
     * @param key the key to use for retrieving the string from the
     *      appropriate resource bundle.
394 395 396 397 398 399 400 401 402
     * @param pluginName the name of the plugin to load the require resource bundle from.
     * @return the localized string.
     */
    public static String getLocalizedString(String key, String pluginName) {
        return getLocalizedString(key, pluginName, null);
    }

    /**
     * Returns an internationalized string loaded from a resource bundle from the passed
Matt Tucker's avatar
Matt Tucker committed
403 404
     * in plugin. If the plugin name is <tt>null</tt>, the key will be looked up using
     * the standard resource bundle.
405
     *
Matt Tucker's avatar
Matt Tucker committed
406 407
     * @param key the key to use for retrieving the string from the
     *      appropriate resource bundle.
408
     * @param pluginName the name of the plugin to load the require resource bundle from.
Matt Tucker's avatar
Matt Tucker committed
409 410
     * @param arguments a list of objects to use which are formatted, then
     *      inserted into the pattern at the appropriate places.
411 412 413
     * @return the localized string.
     */
    public static String getLocalizedString(String key, String pluginName, List arguments) {
Matt Tucker's avatar
Matt Tucker committed
414 415 416
        if (pluginName == null) {
            return getLocalizedString(key, arguments);
        }
417

Matt Tucker's avatar
Matt Tucker committed
418
        Locale locale = JiveGlobals.getLocale();
419 420 421 422 423 424
        String i18nFile = pluginName + "_i18n";

        // Retrieve classloader from pluginName.
        final XMPPServer xmppServer = XMPPServer.getInstance();
        PluginManager pluginManager = xmppServer.getPluginManager();
        Plugin plugin = pluginManager.getPlugin(pluginName);
425
        if (plugin == null) {
Matt Tucker's avatar
Matt Tucker committed
426
            throw new NullPointerException("Plugin could not be located: " + pluginName);
427 428
        }

429
        ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin);
430 431 432 433 434 435 436 437
        try {
            ResourceBundle bundle = ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader);
            return getLocalizedString(key, locale, arguments, bundle);
        }
        catch (MissingResourceException mre) {
            Log.error(mre);
            return key;
        }
Matt Tucker's avatar
Matt Tucker committed
438 439
    }

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    /**
     * Retrieve the <code>ResourceBundle</code> that is used with this plugin.
     *
     * @param pluginName the name of the plugin.
     * @return the ResourceBundle used with this plugin.
     * @throws Exception thrown if an exception occurs.
     */
    public static ResourceBundle getPluginResourceBundle(String pluginName) throws Exception {
        final Locale locale = JiveGlobals.getLocale();

        String i18nFile = pluginName + "_i18n";

        // Retrieve classloader from pluginName.
        final XMPPServer xmppServer = XMPPServer.getInstance();
        PluginManager pluginManager = xmppServer.getPluginManager();
        Plugin plugin = pluginManager.getPlugin(pluginName);
        if (plugin == null) {
            throw new NullPointerException("Plugin could not be located.");
        }

460
        ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin);
461 462 463
        return ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader);
    }

Matt Tucker's avatar
Matt Tucker committed
464 465 466
    /**
     * Returns an internationalized string loaded from a resource bundle using
     * the passed in Locale substituting the passed in arguments. Substitution
467
     * is handled using the {@link MessageFormat} class.
Matt Tucker's avatar
Matt Tucker committed
468
     *
Matt Tucker's avatar
Matt Tucker committed
469 470 471 472
     * @param key the key to use for retrieving the string from the
     *      appropriate resource bundle.
     * @param locale the locale to use for retrieving the appropriate
     *      locale-specific string.
Matt Tucker's avatar
Matt Tucker committed
473
     * @param arguments a list of objects to use which are formatted, then
Matt Tucker's avatar
Matt Tucker committed
474
     *      inserted into the pattern at the appropriate places.
Matt Tucker's avatar
Matt Tucker committed
475 476
     * @return the localized string.
     */
Matt Tucker's avatar
Matt Tucker committed
477 478 479
    public static String getLocalizedString(String key, Locale locale, List arguments,
            ResourceBundle bundle)
    {
Matt Tucker's avatar
Matt Tucker committed
480 481 482 483 484 485 486
        if (key == null) {
            throw new NullPointerException("Key cannot be null");
        }
        if (locale == null) {
            locale = JiveGlobals.getLocale();
        }

487
        String value;
Matt Tucker's avatar
Matt Tucker committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

        // See if the bundle has a value
        try {
            // The jdk caches resource bundles on it's own, so we won't bother.
            value = bundle.getString(key);
            // perform argument substitutions
            if (arguments != null) {
                MessageFormat messageFormat = new MessageFormat("");
                messageFormat.setLocale(bundle.getLocale());
                messageFormat.applyPattern(value);
                try {
                    // This isn't fool-proof, but it's better than nothing
                    // The idea is to try and convert strings into the
                    // types of objects that the formatters expects
                    // i.e. Numbers and Dates
                    Format[] formats = messageFormat.getFormats();
                    for (int i = 0; i < formats.length; i++) {
                        Format format = formats[i];
                        if (format != null) {
                            if (format instanceof DateFormat) {
                                if (arguments.size() > i) {
                                    Object val = arguments.get(i);
                                    if (val instanceof String) {
                                        DateFormat dateFmt = (DateFormat)format;
                                        try {
                                            val = dateFmt.parse((String)val);
                                            arguments.set(i, val);
                                        }
                                        catch (ParseException e) {
                                            Log.error(e);
                                        }
                                    }
                                }
                            }
                            else if (format instanceof NumberFormat) {
                                if (arguments.size() > i) {
                                    Object val = arguments.get(i);
                                    if (val instanceof String) {
                                        NumberFormat nbrFmt = (NumberFormat)format;
                                        try {
                                            val = nbrFmt.parse((String)val);
                                            arguments.set(i, val);
                                        }
                                        catch (ParseException e) {
                                            Log.error(e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    value = messageFormat.format(arguments.toArray());
                }
                catch (IllegalArgumentException e) {
                    Log.error("Unable to format resource string for key: "
543
                            + key + ", argument type not supported");
Matt Tucker's avatar
Matt Tucker committed
544 545 546 547 548
                    value = "";
                }
            }
        }
        catch (java.util.MissingResourceException mre) {
549
            Log.warn("Missing resource for key: " + key
550
                    + " in locale " + locale.toString());
Matt Tucker's avatar
Matt Tucker committed
551 552 553 554 555 556 557
            value = "";
        }

        return value;
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
558 559
     * Returns an internationalized String representation of the number using
     * the default locale.
Matt Tucker's avatar
Matt Tucker committed
560
     *
Matt Tucker's avatar
Matt Tucker committed
561 562
     * @param number the number to format.
     * @return an internationalized String representation of the number.
Matt Tucker's avatar
Matt Tucker committed
563 564 565 566 567 568
     */
    public static String getLocalizedNumber(long number) {
        return NumberFormat.getInstance().format(number);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
569 570
     * Returns an internationalized String representation of the number using
     * the specified locale.
Matt Tucker's avatar
Matt Tucker committed
571
     *
Matt Tucker's avatar
Matt Tucker committed
572 573 574
     * @param number the number to format.
     * @param locale the locale to use for formatting.
     * @return an internationalized String representation of the number.
Matt Tucker's avatar
Matt Tucker committed
575 576 577 578 579 580
     */
    public static String getLocalizedNumber(long number, Locale locale) {
        return NumberFormat.getInstance(locale).format(number);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
581 582
     * Returns an internationalized String representation of the number using
     * the default locale.
Matt Tucker's avatar
Matt Tucker committed
583
     *
Matt Tucker's avatar
Matt Tucker committed
584 585
     * @param number the number to format.
     * @return an internationalized String representation of the number.
Matt Tucker's avatar
Matt Tucker committed
586 587 588 589 590 591
     */
    public static String getLocalizedNumber(double number) {
        return NumberFormat.getInstance().format(number);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
592 593
     * Returns an internationalized String representation of the number using
     * the specified locale.
Matt Tucker's avatar
Matt Tucker committed
594
     *
Matt Tucker's avatar
Matt Tucker committed
595 596 597
     * @param number the number to format.
     * @param locale the locale to use for formatting.
     * @return an internationalized String representation of the number.
Matt Tucker's avatar
Matt Tucker committed
598 599 600 601 602
     */
    public static String getLocalizedNumber(double number, Locale locale) {
        return NumberFormat.getInstance(locale).format(number);
    }
}