Example usage for java.text MessageFormat MessageFormat

List of usage examples for java.text MessageFormat MessageFormat

Introduction

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

Prototype

public MessageFormat(String pattern, Locale locale) 

Source Link

Document

Constructs a MessageFormat for the specified locale and pattern.

Usage

From source file:com.albert.util.StringUtilCommon.java

/**
 * Get message from resource bundle//ww w .  j  a v a  2  s .com
 * 
 * @param key
 * @param locale
 * @param bundleName
 * @param params
 * @return
 */
public static String getMessage(String key, Locale locale, String bundleName, Object params[]) {

    String text = null;

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

    try {
        text = bundle.getString(key);
    } catch (Exception e) {
        text = key;
    }

    if (params != null) {
        MessageFormat mf = new MessageFormat(text, locale);
        text = mf.format(params, new StringBuffer(), null).toString();
    }

    return text;
}

From source file:ninja.i18n.MessagesImpl.java

MessageFormat getMessageFormatForLocale(String value, Optional<String> language) {
    Locale locale = lang.getLocaleFromStringOrDefault(language);
    MessageFormat messageFormat = new MessageFormat(value, locale);
    return messageFormat;
}

From source file:it.govpay.web.utils.Utils.java

public Map<String, String> getMessagesFromResourceBundle(String bundleName, String keyPrefix, Object params[],
        Locale locale) {//  w  w  w.  j  ava2 s .c  o  m

    Map<String, String> toRet = new HashMap<String, String>();

    String text = null;

    ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, this.getCurrentClassLoader(params));

    Enumeration<String> keys = bundle.getKeys();

    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        if (key.startsWith(keyPrefix)) {
            try {
                text = bundle.getString(key);
            } catch (MissingResourceException e) {
                text = MISSING_RESOURCE_START_PLACEHOLDER + key + MISSING_RESOURCE_END_PLACEHOLDER;
            }
            if (params != null) {
                MessageFormat mf = new MessageFormat(text, locale);
                text = mf.format(params, new StringBuffer(), null).toString();
            }

            toRet.put(key.substring(keyPrefix.length()), text);
        }
    }

    return toRet;
}

From source file:gwtupload.server.UploadServlet.java

/**
 * Returns the localized text of a key./*from ww w.  j av  a 2 s.c  o  m*/
 */
public static String getMessage(String key, Object... pars) {
    Locale loc = getThreadLocalRequest() == null || getThreadLocalRequest().getLocale() == null
            ? new Locale("en")
            : getThreadLocalRequest().getLocale();

    ResourceBundle res = ResourceBundle.getBundle(UploadServlet.class.getName(), loc);

    String msg = res.getString(key);
    if ("zh".equals(loc.getLanguage()) && "CN".equals(loc.getCountry())) {
        msg = convertEncodingFormat(msg, "ISO-8859-1", "UTF-8");
    }
    return new MessageFormat(msg, loc).format(pars);
}

From source file:com.github.jknack.handlebars.helper.I18nHelper.java

@Override
public String message(final String key, final Locale locale, final Object... args) {
    isTrue(bundle.containsKey(key), "no message found: '%s' for locale '%s'.", key, locale);
    String message = bundle.getString(key);
    if (args.length == 0) {
        return message;
    }// w  w  w  .java2s.c o m
    MessageFormat format = new MessageFormat(message, locale);
    return format.format(args);
}

From source file:org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource.java

private MessageFormat findMessageFormatInBinaryPlugins(String code, Locale locale) {
    final GrailsPlugin[] allPlugins = pluginManager.getAllPlugins();
    for (GrailsPlugin plugin : allPlugins) {
        if (!(plugin instanceof BinaryGrailsPlugin)) {
            continue;
        }/*  w  w  w.  jav a  2 s  .  co  m*/

        BinaryGrailsPlugin binaryPlugin = (BinaryGrailsPlugin) plugin;
        final Properties binaryPluginProperties = binaryPlugin.getProperties(locale);
        if (binaryPluginProperties != null) {
            String foundCode = binaryPluginProperties.getProperty(code);
            if (foundCode != null) {
                MessageFormat result = new MessageFormat(foundCode, locale);
                if (result != null)
                    return result;
            }
        }
    }
    return null;
}

From source file:com.vaadin.integration.maven.wscdn.CvalChecker.java

static String getErrorMessage(String key, Object... pars) {
    Locale loc = Locale.getDefault();
    ResourceBundle res = ResourceBundle.getBundle(CvalChecker.class.getName(), loc);
    String msg = res.getString(key);
    return new MessageFormat(msg, loc).format(pars);
}

From source file:org.panbox.desktop.common.gui.PairNewDeviceDialog.java

private void startNewRefreshTimer() {
    logger.debug("PairNewDeviceDialog : startNewRefreshTimer : Starting new refresh timeout timer.");
    timeout = PAKCorePairingHandler.PAIRING_TIMEOUT / 1000;
    refreshTimeout = new TimerTask() {

        @Override/*from   ww  w .jav  a  2 s .c o  m*/
        public void run() {
            logger.debug("Timeout timer started... Left: " + timeout + " seconds.");
            MessageFormat formatter = new MessageFormat("", Settings.getInstance().getLocale());
            formatter.applyPattern(bundle.getString("client.deviceList.devicepairing.timeoutstatusbar"));
            statusInfoLabel.setText(formatter.format(new Object[] { timeout }));
            statusInfoLabel.invalidate();
            timeout--;
        }
    };
    timer = new Timer();
    timer.schedule(refreshTimeout, new Date(), 1000);
}

From source file:cc.redpen.validator.Validator.java

/**
 * returns localized error message for the given key formatted with argument
 *
 * @param key  message key//from ww w.  j  a v  a2 s .c om
 * @param args objects to format
 * @return localized error message
 */
protected String getLocalizedErrorMessage(String key, Object... args) {
    if (errorMessages != null) {
        String suffix = key != null ? "." + key : "";
        MessageFormat fmt = new MessageFormat(errorMessages.getString(this.getClass().getSimpleName() + suffix),
                locale);
        return fmt.format(args);
    } else {
        throw new AssertionError("message resource not found.");
    }
}

From source file:org.apache.openmeetings.web.app.Application.java

public static String getString(String key, final Locale loc, boolean noReplace) {
    if (!exists()) {
        ThreadContext.setApplication(Application.get(appName));
    }/*from www. ja  va2  s  .  c o m*/
    Localizer l = get().getResourceSettings().getLocalizer();
    String value = l.getStringIgnoreSettings(key, null, null, loc, null, "[Missing]");
    if (!noReplace && STRINGS_WITH_APP.contains(key)) {
        final MessageFormat format = new MessageFormat(value, loc);
        value = format.format(new Object[] { getBean(ConfigurationDao.class).getAppName() });
    }
    if (!noReplace && RuntimeConfigurationType.DEVELOPMENT == get().getConfigurationType()) {
        value += String.format(" [%s]", key);
    }
    return value;
}