Example usage for java.text MessageFormat getFormatsByArgumentIndex

List of usage examples for java.text MessageFormat getFormatsByArgumentIndex

Introduction

In this page you can find the example usage for java.text MessageFormat getFormatsByArgumentIndex.

Prototype

public Format[] getFormatsByArgumentIndex() 

Source Link

Document

Gets the formats used for the values passed into format methods or returned from parse methods.

Usage

From source file:I18N.java

/**
 * Given a message and parameters, resolve all message's parameter
 *  placeholders with the parameter value. The firstParam can change which
 *  parameter relates to {0} placeholder in the message, and all increment
 *  from this index. If any of the parameters also have placeholders, this
 *  recursively calls itself to fill their placeholders, setting the
 *  firstParam to the index following all parameters that are used by the
 *  current message so params must be in the order p0..pN, p00..p0N..pMN,
 *  p000..p00N..p0MN..pLMN... where each additional index is for nested
 *  placeholders (ones in params) and assumes every message/param contains
 *  N M L placeholders; any that don't contain placeholders can have their
 *  pXXX.. taken out, so long as the order of remaining params don't change
 * @param message Message to format/*ww  w  . j  av a 2 s. c o m*/
 * @param firstParam Index of parameter that relates to {0} placeholder,
 *  all parameters following this one relate to incrementing placeholders
 * @param params The parameters used to fill the placeholders
 * @return Message with all placeholders filled with relative parameters
 */
private static String formatMessage(String message, int firstParam, Object[] params) {

    // Only need to do any formatting if there are parameters to do the
    //  formatting with. If there are none, the message input is returned
    //  unmodified
    if (params != null && firstParam < params.length) {
        MessageFormat parser;
        Locale locale = Locale.getDefault();
        Format[] formats;

        // Set up
        parser = new MessageFormat("");
        parser.setLocale(locale);
        parser.applyPattern(message);
        // Used only to count how many parameters are needed by this message
        formats = parser.getFormatsByArgumentIndex();

        // Recursively format the parameters used by this message
        for (int paramIndex = 0; paramIndex < formats.length; paramIndex++)
            if (params[firstParam + paramIndex] instanceof String)
                params[firstParam + paramIndex] = formatMessage(params[firstParam + paramIndex].toString(),
                        firstParam + formats.length, params);

        // Format the message using the formatted parameters
        message = parser.format(getParams(params, firstParam, firstParam + formats.length));

    }

    return message;

}