Java Message Format formatString(ResourceBundle resourceBundle, String key, Object data)

Here you can find the source of formatString(ResourceBundle resourceBundle, String key, Object data)

Description

Returns the localized string associated with a resource key and formatted with a given string.

License

Open Source License

Parameter

Parameter Description
resourceBundle A resource bundle.
key A resource key.
data An object.

Return

A formatted localized string.

Declaration

public static String formatString(ResourceBundle resourceBundle, String key, Object data) 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import java.text.MessageFormat;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    /**/*from www.  j  av  a  2s  .co  m*/
     * Returns the localized string associated with a resource key and formatted
     * with a given string.
     * 
     * @param resourceBundle
     *            A resource bundle.
     * @param key
     *            A resource key.
     * @param data
     *            An object.
     * @return A formatted localized string.
     */
    public static String formatString(ResourceBundle resourceBundle, String key, Object data) {
        if (resourceBundle != null) {
            try {
                String localizedStr = resourceBundle.getString(key);
                return MessageFormat.format(localizedStr, new Object[] { data });
            } catch (MissingResourceException e) {
            }
        }
        return '[' + key + ']';
    }

    /**
     * Returns the localized string associated with a resource key and formatted
     * with two given string.
     * 
     * @param resourceBundle
     *            A resource bundle.
     * @param key
     *            A resource key.
     * @param data1
     *            An object.
     * @param data2
     *            An object.
     * @return A formatted localized string.
     */
    public static String formatString(ResourceBundle resourceBundle, String key, Object data1, Object data2) {
        if (resourceBundle != null) {
            try {
                String localizedStr = resourceBundle.getString(key);
                return MessageFormat.format(localizedStr, new Object[] { data1, data2 });
            } catch (MissingResourceException e) {
            }
        }
        return '[' + key + ']';
    }

    /**
     * Returns the localized string associated with a resource key and formatted
     * with a given string.
     * 
     * @param resourceBundle
     *            A resource bundle.
     * @param key
     *            A resource key.
     * @param data
     *            An array of objects.
     * @return A formatted localized string.
     */
    public static String formatString(ResourceBundle resourceBundle, String key, Object[] data) {
        if (resourceBundle != null) {
            try {
                String localizedStr = resourceBundle.getString(key);
                return MessageFormat.format(localizedStr, data);
            } catch (MissingResourceException e) {
            }
        }
        return '[' + key + ']';
    }

    /**
     * Returns the localized string associated with a resource key.
     * 
     * @param resourceBundle
     *            A resource bundle.
     * @param key
     *            A resource key.
     * @return A localized string.
     */
    public static String getString(ResourceBundle resourceBundle, String key) {
        if (resourceBundle != null) {
            try {
                return resourceBundle.getString(key);
            } catch (MissingResourceException e) {
            }
        }
        return '[' + key + ']';
    }
}

Related

  1. formatMessage(final String pattern, final String param1)
  2. formatMessage(String message, Object param1)
  3. formatMessage(String message, Object... args)
  4. formatResource(Object[] info, String require)
  5. formats(String pattern, Object... args)
  6. formatString(String format, String arg1)
  7. formatString(String message, Object... args)
  8. formatString(String str, String pattern)