Java Resource Message getMessage(Locale locale, String key, Object... args)

Here you can find the source of getMessage(Locale locale, String key, Object... args)

Description

Returns a message formated in the specified locale.

License

Open Source License

Parameter

Parameter Description
locale Locale
key Message key
args Arguments of the message

Return

Formated message

Declaration

public static String getMessage(Locale locale, String key, Object... args) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    /**//from   ww  w .j  av  a2 s.  c  o m
     * Returns a message formated in the specified locale.
     * Returns **key** if no message is set for this key.
     * 
     * @param locale Locale
     * @param key Message key
     * @param args Arguments of the message
     * @return Formated message
     */
    public static String getMessage(Locale locale, String key, Object... args) {
        ResourceBundle resources = ResourceBundle.getBundle("messages", locale);
        String message = null;
        try {
            message = resources.getString(key);
        } catch (MissingResourceException e) {
            message = "**" + key + "**";
        }
        return MessageFormat.format(message, args);
    }

    /**
     * Returns the resource bundle containing messages for the locale.
     * 
     * @param locale Locale to use
     * @return Resource bundle
     */
    public static ResourceBundle getMessage(Locale locale) {
        return ResourceBundle.getBundle("messages", locale);
    }
}

Related

  1. getMessage(final Locale locale, final String messageKey)
  2. getMessage(final String bundleName, final Locale locale, final String key, final Object... params)
  3. getMessage(Locale locale, String key, String... args)
  4. getMessage(Properties prop, String key, Object... args)
  5. getMessage(Properties props, String key, Object param)
  6. getMessage(ResourceBundle bundle, Object key, Object[] params)