Example usage for java.text MessageFormat setLocale

List of usage examples for java.text MessageFormat setLocale

Introduction

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

Prototype

public void setLocale(Locale locale) 

Source Link

Document

Sets the locale to be used when creating or comparing subformats.

Usage

From source file:MessageFormatReuse.java

public static void main(String args[]) {
    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));
}

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);//from  w  w w. j a va2s. co m
    System.out.println(formatter.format(arguments));

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

From source file:ChoiceFormatDemo.java

static void displayMessages(Locale currentLocale) {

    System.out.println("currentLocale = " + currentLocale.toString());
    System.out.println();/*from   w ww.j a va2  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.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 {//w w  w  .  j  a va 2  s.  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   ww  w . jav a2  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//from w ww .j ava 2 s .  c om
 * @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 w  w  .j  a v a2 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:org.apache.manifoldcf.core.i18n.Messages.java

/** Obtain a string given a class, bundle, locale, message key, and arguments.
*//*from ww w  .ja  v a 2s . c om*/
public static String getString(Class clazz, String bundleName, Locale locale, String messageKey,
        Object[] args) {
    String message = getMessage(clazz, bundleName, locale, messageKey);
    if (message == null)
        return messageKey;

    // Format the message
    String formatMessage;
    if (args != null) {
        MessageFormat fm = new MessageFormat(message);
        fm.setLocale(locale);
        formatMessage = fm.format(args);
    } else {
        formatMessage = message;
    }
    return formatMessage;
}

From source file:org.apache.manifoldcf.core.i18n.Messages.java

/** Obtain a string given a resource bundle, message key, and arguments.
*//*w w  w . j ava  2 s  .c  om*/
public static String getString(ResourceBundle resourceBundle, String bundleName, Locale locale,
        String messageKey, Object[] args) {
    String message = getMessage(resourceBundle, bundleName, locale, messageKey);
    if (message == null)
        return messageKey;

    // Format the message
    String formatMessage;
    if (args != null) {
        MessageFormat fm = new MessageFormat(message);
        fm.setLocale(locale);
        formatMessage = fm.format(args);
    } else {
        formatMessage = message;
    }
    return formatMessage;

}

From source file:com.hypersocket.i18n.I18N.java

public static String getResourceNoOveride(Locale locale, String resourceBundle, String key,
        Object... arguments) {//from  w ww .j a  v  a 2s. com
    if (key == null) {
        throw new IllegalArgumentException("You must specify a key!");
    }
    if (resourceBundle == null) {
        throw new IllegalArgumentException("You must specify a resource bundle for key " + key);
    }

    if (!resourceBundle.startsWith("i18n/")) {
        resourceBundle = "i18n/" + resourceBundle;
    }

    try {
        ResourceBundle resource = ResourceBundle.getBundle(resourceBundle, locale, I18N.class.getClassLoader());
        String localizedString = resource.getString(key);
        if (arguments == null || arguments.length == 0) {
            return localizedString;
        }

        MessageFormat messageFormat = new MessageFormat(localizedString);
        messageFormat.setLocale(locale);
        return messageFormat.format(formatParameters(arguments));
    } catch (MissingResourceException mre) {
        return "[i18n/" + resourceBundle + "/" + key + "]";
    }
}