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:org.sigmah.server.mail.InvitationMailer.java

@Trace
@LogException/*from  w  ww.j a v  a  2s  .  c o  m*/
public void send(Invitation model, Locale locale, boolean sendBySigmah)
        throws EmailException, TemplateException, IOException {

    final ResourceBundle mailMessages = getResourceBundle(locale);

    SimpleEmail mail = new SimpleEmail();
    mail.addTo(model.getNewUser().getEmail(), model.getNewUser().getName());

    MessageFormat formatter = new MessageFormat(mailMessages.getString("newUserInvitationMailSubject"), locale);
    mail.setSubject(formatter.format(null));

    // Create the string of message subject
    final Object[] messageArguments = { User.getUserCompleteName(model.getNewUser()),
            User.getUserCompleteName(model.getInvitingUser()), model.getInvitingUser().getEmail(),
            model.getHostUrl(), model.getNewUser().getEmail(), model.getNewUserPassword() };

    formatter = new MessageFormat(mailMessages.getString("newUserInvitationMailMessage"), locale);
    String messageSubject = formatter.format(messageArguments);

    mail.setMsg(messageSubject);

    sender.send(mail);
}

From source file:org.orekit.errors.OrekitInternalError.java

/**
 * Builds a message string by from a pattern and its arguments.
 * @param locale Locale in which the message should be translated
 * @return a message string/*from   w w  w . jav a2 s  .  c o  m*/
 */
private String buildMessage(final Locale locale) {
    return new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
}

From source file:org.obm.push.mail.MailErrorsMessages.java

private String getString(String key, Object... arguments) {
    String isoEncodedString = bundle.getString(key);
    String string = new String(isoEncodedString.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
    MessageFormat format = new MessageFormat(string, bundle.getLocale());
    return format.format(arguments);
}

From source file:org.hippoecm.frontend.plugins.standards.ClassResourceModel.java

@Override
protected String load() {
    Iterator<IStringResourceLoader> iter = Application.get().getResourceSettings().getStringResourceLoaders()
            .iterator();// w ww . ja v  a  2  s  .c o  m
    String value = null;
    while (iter.hasNext()) {
        IStringResourceLoader loader = iter.next();
        value = loader.loadStringResource(clazz, key, locale, style, null);
        if (value != null) {
            break;
        }
    }
    if (value != null) {
        if (parameters != null) {
            final MessageFormat format = new MessageFormat(value, locale);
            value = format.format(parameters);
        }
        return value;
    }

    if (RuntimeConfigurationType.DEVELOPMENT.equals(Application.get().getConfigurationType())) {
        throw new RuntimeException("No translation found for " + this);
    } else {
        return key;
    }
}

From source file:org.ppwcode.vernacular.l10n_III.I18nExceptionHelpers.java

public static String format(String template, Object context, Locale locale) throws I18nException {
    assert preArgumentNotNull(template, "template");
    assert preArgumentNotNull(context, "context");
    assert preArgumentNotNull(locale, "locale");

    List<Object> objects = new ArrayList<Object>();
    String pattern = processTemplate(template, context, locale, objects);
    LOG.debug("Pattern: " + pattern);
    MessageFormat form = new MessageFormat(pattern, locale);
    String result = form.format(objects.toArray());
    return result;
}

From source file:org.mitre.openid.connect.config.JsonMessageSource.java

@Override
protected MessageFormat resolveCode(String code, Locale locale) {

    List<JsonObject> langs = getLanguageMap(locale);

    String value = getValue(code, langs);

    if (value == null) {
        // if we haven't found anything, try the default locale
        langs = getLanguageMap(fallbackLocale);
        value = getValue(code, langs);// w  w w.j  a  v  a 2 s.com
    }

    if (value == null) {
        // if it's still null, return null
        return null;
    } else {
        // otherwise format the message
        return new MessageFormat(value, locale);
    }

}

From source file:org.orekit.errors.OrekitIllegalArgumentException.java

/**
 * Builds a message string by from a pattern and its arguments.
 * @param locale Locale in which the message should be translated
 * @return a message string//from   ww w  .j  a v a2  s . c o  m
 */
private String buildMessage(final Locale locale) {
    return (specifier == null) ? ""
            : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
}

From source file:org.dstadler.commons.logging.jdk.PatternFormatter.java

public PatternFormatter() {
    LogManager manager = LogManager.getLogManager();
    String cName = getClass().getName();

    timeFormat = manager.getProperty(cName + ".timeFormat");

    if (timeFormat == null) {
        timeFormat = "dd-MMM-yyy; HH:mm:ss";
    }//from   w  w  w  . j av  a2s .c  o m
    setTimeFormat(timeFormat);

    logPattern = manager.getProperty(cName + ".logPattern");
    if (logPattern == null) {
        logPattern = "[{0} - {1}] {2}: {3} \n";
    }
    setLogPattern(logPattern);

    exceptionPattern = manager.getProperty(cName + ".exceptionPattern");
    if (exceptionPattern == null) {
        exceptionPattern = "[{0} - {1}] {2} {3} \nException in {4}: {6} \n{7} ";
    }
    setExceptionPattern(exceptionPattern);

    logMessageFormat = new MessageFormat(logPattern, Locale.ROOT);
    exceptionMessageFormat = new MessageFormat(exceptionPattern, Locale.ROOT);

    dateFormat = FastDateFormat.getInstance(timeFormat);
}

From source file:org.archive.wayback.util.StringFormatter.java

public MessageFormat getFormat(String pattern) {
    MessageFormat format = formats.get(pattern);
    if (format == null) {
        format = new MessageFormat(pattern, locale);
        // lets try to make sure any internal DateFormats use UTC:
        Format[] subFormats = format.getFormats();
        if (subFormats != null) {
            for (Format subFormat : subFormats) {
                if (subFormat instanceof DateFormat) {
                    DateFormat subDateFormat = (DateFormat) subFormat;
                    subDateFormat.setTimeZone(TZ_UTC);
                }// www .j ava  2 s. c  o  m
            }
        }
        formats.put(pattern, format);
    }
    return format;
}

From source file:dk.statsbiblioteket.util.i18n.Translator.java

/**
 * Create a Translator for a named {@link ResourceBundle} and a given
 * {@link Locale}./*  www  .ja  v  a  2  s.c o  m*/
 *
 * If no resource bundle matching the request can be found this
 * class will try to fall back to the {@link #DEFAULT_LOCALE}. If this fails
 * too it will fall back to an empty resource bundle. This means that all
 * requests to {@link #translate} will return a String where '.' is
 * replaced by a white space. See {@link #translate} documentation.
 *
 * @param bundleName the name of the ResourceBundle to look up
 * @param locale     the locale to translate to
 */
public Translator(String bundleName, Locale locale) {
    this.locale = locale;
    this.formatter = new MessageFormat("", locale);
    this.unknownMessages = new HashMap<String, String>();
    this.log = LogFactory.getLog(Translator.class);

    try {
        this.bundle = BundleCache.getBundle(bundleName, locale);
        usingFallBack = false;
    } catch (MissingResourceException e) {
        usingFallBack = true;
        log.warn("Unable to find resource bundle '" + bundleName + "' for locale '" + locale
                + "'. Trying default: '" + DEFAULT_LOCALE + "'.");
        try {
            this.bundle = BundleCache.getBundle(bundleName, DEFAULT_LOCALE);
        } catch (MissingResourceException ee) {
            log.warn("Unable to load bundle '" + bundleName + "' for default locale '" + DEFAULT_LOCALE
                    + "'. Falling back to raw translations.");
            this.bundle = new EmptyResourceBundle();
        }
    }
}