Example usage for java.text MessageFormat applyPattern

List of usage examples for java.text MessageFormat applyPattern

Introduction

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

Prototype

@SuppressWarnings("fallthrough") 
public void applyPattern(String pattern) 

Source Link

Document

Sets the pattern used by this message format.

Usage

From source file:MainClass.java

public static void main(String[] argv) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);

    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.FRANCE);
    System.out.println(formatter.format(arguments));

    pattern = "On {1}, {0}K was deleted.";
    formatter.applyPattern(pattern);
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
}

From source file:org.vosao.i18n.Messages.java

public static String get(String key, Object... objects) {
    VosaoContext ctx = VosaoContext.getInstance();
    String pattern = "not found";
    if (isLocaleSupported(ctx.getLocale())) {
        pattern = getBundle(ctx.getRequest()).getString(key);
    } else {//from  ww w .  j av  a 2s.c  o  m
        pattern = getDefaultBundle().getString(key);
    }
    if (objects != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(ctx.getLocale());
        formatter.applyPattern(pattern);
        pattern = formatter.format(objects);
    }
    return pattern;
}

From source file:org.kineticsystem.commons.util.Localizer.java

/** 
 * Retrieve and return a localized message from a resource bundle using the
 * given searching key.//from  w ww .  ja  v a 2  s . c o  m
 * @param resourceName The resource name.
 * @param key The resource key.
 * @param messageArguments Input parameters to be localized inside the
 *     string. 
 * @return The localized message.
 */
public static String localizeMessage(String resourceName, String key, Object[] messageArguments) {
    if (messageMode == DEBUG_MESSAGE_MODE) {
        return key;
    } else {
        String message = (String) doLocalize(resourceName, key);
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(Locale.getDefault());
        formatter.applyPattern(message);
        return formatter.format(messageArguments);
    }
}

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// w w  w  .  ja  va2  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;

}

From source file:net.mlw.vlh.web.util.JspUtils.java

public static String format(Object value, String format, Locale loc) {
    if (value == null) {
        return "";
    } else {/*from w ww  . ja  v a  2  s.co  m*/
        if (value instanceof Number) {
            if (format == null || format.length() == 0) {
                return value.toString();
            }
            MessageFormat mf = new MessageFormat("{0,number," + format + "}");
            if (loc != null) {
                mf.setLocale(loc);
                mf.applyPattern(mf.toPattern());
            }

            return mf.format(new Object[] { value });
        } else if (value instanceof java.util.Date) {
            if (format == null || format.length() == 0) {
                //TODO: get the default date format in here somehow. format =
                // SystemProperties.getProperty("default.dateFormat", "EEE, MMM
                // d, ''yy");
                format = "EEE, MMM d, ''yy";
            }
            MessageFormat mf = new MessageFormat("{0,date," + format + "}");
            if (loc != null) {
                mf.setLocale(loc);
                mf.applyPattern(mf.toPattern());
            }
            return mf.format(new Object[] { value });
        } else if (value instanceof Calendar) {
            Calendar calendar = (Calendar) value;
            if (format == null || format.length() == 0) {
                //TODO: get the default date format in here somehow. format =
                // SystemProperties.getProperty("default.dateFormat", "EEE, MMM
                // d, ''yy");
                format = "EEE, MMM d, ''yy";
            }

            MessageFormat mf = new MessageFormat("{0,date," + format + "}");
            if (loc != null) {
                mf.setLocale(loc);
                mf.applyPattern(mf.toPattern());
            }
            return mf.format(new Object[] { value });
        } else {
            return value.toString();
        }

    }
}

From source file:ChoiceFormatDemo.java

static void displayMessages(Locale currentLocale) {

    System.out.println("currentLocale = " + currentLocale.toString());
    System.out.println();//  w  w  w  .ja v  a 2 s  . c om

    ResourceBundle bundle = ResourceBundle.getBundle("ChoiceBundle", currentLocale);

    MessageFormat messageForm = new MessageFormat("");
    messageForm.setLocale(currentLocale);

    double[] fileLimits = { 0, 1, 2 };

    String[] fileStrings = { bundle.getString("noFiles"), bundle.getString("oneFile"),
            bundle.getString("multipleFiles") };

    ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings);

    String pattern = bundle.getString("pattern");
    Format[] formats = { choiceForm, null, NumberFormat.getInstance() };

    messageForm.applyPattern(pattern);
    messageForm.setFormats(formats);

    Object[] messageArguments = { null, "XDisk", null };

    for (int numFiles = 0; numFiles < 4; numFiles++) {
        messageArguments[0] = new Integer(numFiles);
        messageArguments[2] = new Integer(numFiles);
        String result = messageForm.format(messageArguments);
        System.out.println(result);
    }
}

From source file:org.jamwiki.utils.Utilities.java

/**
 * Given a message key, locale, and formatting parameters, return a
 * locale-specific message./*from  w w  w . ja v a  2  s.co  m*/
 *
 * @param key The message key that corresponds to the formatted message
 *  being retrieved.
 * @param locale The locale for the message that is to be retrieved.
 * @param params An array of formatting parameters to use in the message
 *  being returned.
 * @return A formatted message string that is specific to the locale.
 */
public static String formatMessage(String key, Locale locale, Object[] params) {
    MessageFormat formatter = new MessageFormat("");
    formatter.setLocale(locale);
    String message = Utilities.formatMessage(key, locale);
    formatter.applyPattern(message);
    return formatter.format(params);
}

From source file:com.opensymphony.xwork2.util.LocalizedTextUtil.java

private static MessageFormat buildMessageFormat(String pattern, Locale locale) {
    MessageFormatKey key = new MessageFormatKey(pattern, locale);
    MessageFormat format = messageFormats.get(key);
    if (format == null) {
        format = new MessageFormat(pattern);
        format.setLocale(locale);//from  w ww.  ja  v a2  s .c om
        format.applyPattern(pattern);
        messageFormats.put(key, format);
    }

    return format;
}

From source file:br.com.sicoob.cro.cop.util.BatchPropertiesUtil.java

/**
 * Passa os argumentos para a string da mensagem.
 *
 * @param key Chave./*from w w w  . j  a  v a2  s  .co  m*/
 * @param replace Argumentos.
 * @return String formatada.
 */
private String applyArguments(String key, String... replace) {
    MessageFormat format = new MessageFormat("");
    format.applyPattern((String) properties.get(key));
    return format.format(replace);
}

From source file:com.safetys.framework.jmesa.core.message.ResourceBundleMessages.java

public String getMessage(String code, Object[] args) {
    String result = findResource(customResourceBundle, code);

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }//w ww. ja va  2 s.  c  o  m

    if (result != null && args != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(locale);
        formatter.applyPattern(result);
        result = formatter.format(args);
    }

    return result;
}