Example usage for java.text MessageFormat format

List of usage examples for java.text MessageFormat format

Introduction

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

Prototype

public final String format(Object obj) 

Source Link

Document

Formats an object to produce a string.

Usage

From source file:com.ultrapower.eoms.common.plugin.ecside.resource.TableResourceBundle.java

 /**
 * Get the resource property./*from   ww  w  .  j ava2s.c  o  m*/
 */
 public String getMessage(String code, Object[] args) {
   String result = null;
   ResourceBundle customResourceBundle;
   for (int i = 0; i < customResourceBundleList.size(); i++) {
      customResourceBundle = (ResourceBundle) customResourceBundleList
            .get(i);
      result = findResource(customResourceBundle, code);
      if (result != null) {
         break;
      }
   }

   if (result == null) {
      result = findResource(defaultResourceBundle, code);
   }

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

   return result;
}

From source file:com.googlecode.jtiger.modules.ecside.resource.TableResourceBundle.java

/**
* Get the resource property.//from   w  w  w . j a v  a2 s .c  o  m
*/
public String getMessage(String code, Object[] args) {
    String result = null;
    ResourceBundle customResourceBundle;
    for (int i = 0; i < customResourceBundleList.size(); i++) {
        customResourceBundle = (ResourceBundle) customResourceBundleList.get(i);
        result = findResource(customResourceBundle, code);
        if (result != null) {
            break;
        }
    }

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }

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

    return result;
}

From source file:enumsupport.reverselookupmapfactory.DeduplicatdeNumberSetFactory.java

/**
 *
 * @param numberRange ???//  w  w w.j  a v  a2s.  c  o m
 * @param number  null??
 * @param numbers ??????null??????
 * @return ????(??)
 * @throws NullPointerException ?????null?????
 * @throws IllegalArgumentException ??numberrange???????
 */
public Set<T> makeSet(Range<T> numberRange, T number, T... numbers)
        throws NullPointerException, IllegalArgumentException {

    List<T> t = new ArrayList<>();
    t.add(number);

    if (numbers != null) {
        t.addAll(Arrays.asList(numbers));
    }
    for (T num : t) {
        if (num == null) {
            throw new IllegalArgumentException("?null???????");
        }
        if (!numberRange.contains(num)) {
            MessageFormat msg = new MessageFormat(
                    "????????????={0}");
            Object[] parameters = { num };
            throw new IllegalArgumentException(msg.format(parameters));
        }
    }
    Set<T> numberSet_t = Collections.synchronizedSet(new HashSet<>());
    numberSet_t.addAll(t);
    return Collections.unmodifiableSet(numberSet_t);
}

From source file:name.gumartinm.weather.information.service.ServiceForecastParser.java

public String createURIAPIForecast(final String urlAPI, final String APIVersion, final double latitude,
        final double longitude, final String resultsNumber) {

    final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US);
    final Object[] values = new Object[4];
    values[0] = APIVersion;//  w w w.j a va  2  s .com
    values[1] = latitude;
    values[2] = longitude;
    values[3] = resultsNumber;

    return formatURIAPI.format(values);
}

From source file:libepg.ts.packet.PROGRAM_ID.java

@Override
public synchronized String toString() {
    StringBuilder s = new StringBuilder();
    for (int i : this.pids) {
        s.append("[");
        s.append(Integer.toHexString(i));
        s.append("]");
    }//  w w  w  .  jav  a  2  s  .c  o m
    String set = s.toString();
    MessageFormat msg = new MessageFormat("{0}(pidName={1},PIDs={2})");
    Object[] parameters = { super.toString(), this.getPidName(), set };
    return msg.format(parameters);
}

From source file:it.cnr.icar.eric.client.ui.swing.metal.MetalThemeMenu.java

/**
 * Updates the UI strings based on the current locale.
 *//*  ww  w . java 2s .  co  m*/
protected void updateText() {
    for (int i = 0; i < getItemCount(); i++) {
        JMenuItem item = getItem(i);

        // item is null if there is a JSeparator at that position.
        if (item != null) {
            String itemName = item.getName();
            String themeName;

            try {
                themeName = themeNames.getString(itemName);
            } catch (MissingResourceException mre) {
                themeName = itemName;

                Object[] noResourceArgs = { itemName, getLocale() };
                MessageFormat form = new MessageFormat(themeNames.getString("message.error.noResource"));
                log.error(form.format(noResourceArgs));
            }

            item.setText(themeName);
        }
    }
}

From source file:it.cnr.icar.eric.client.ui.swing.metal.MetalThemeMenu.java

/**
 * Constucts a JMenu named 'name' with a a RadioButton for each
 * object in 'themeArray'.//from w  w w  . j  a  va  2 s.c om
 *
 * @param name the visible name for the Menu.
 * @param themeArray the array of themes to put in the menu.
 */
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
    super(name);
    themeNames = ResourceBundle.getBundle(BASE_NAME, Locale.getDefault());
    themes = themeArray;

    ButtonGroup group = new ButtonGroup();
    JRadioButtonMenuItem defaultItem = null;

    for (int i = 0; i < themes.length; i++) {
        String themeName;

        try {
            themeName = themeNames.getString(themes[i].getName().replaceAll("\\s", ""));
        } catch (MissingResourceException mre) {
            themeName = themes[i].getName();

            Object[] noResourceArgs = { themes[i].getName(), getLocale() };
            MessageFormat form = new MessageFormat(themeNames.getString("message.error.noResource"));
            log.error(form.format(noResourceArgs));
        }

        JRadioButtonMenuItem item = new JRadioButtonMenuItem(themeName);
        group.add(item);
        add(item);
        item.setActionCommand(i + "");
        item.addActionListener(this);

        // Theme name without spaces is the key for looking up localized item text.
        item.setName(themes[i].getName().replaceAll(" ", ""));

        if (i == 0) {
            item.setSelected(true);
            defaultItem = item;
        }
    }

    //add listener for 'locale' bound property
    RegistryBrowser.getInstance().addPropertyChangeListener(RegistryBrowser.PROPERTY_LOCALE, this);

    defaultItem.doClick();
}

From source file:com.redhat.rhn.common.localization.XmlMessages.java

/**
 * Method to format a string from the resource bundle.  This
 * method allows the user of this class to directly specify the
 * bundle package name to be used.  Allows us to have
 * resource bundles in packages without classes.  Eliminates
 * the need for "Dummy" classes.//from   ww w .ja v a  2  s.  c  o m
 *
 * @param clazz the class to which the string belongs
 * @param locale the locale used to find the resource bundle
 * @param key the key for the string to be obtained from the resource bundle
 * @param args the arguments that should be applied to the string obtained
 * from the resource bundle. Can be null, to represent no arguments
 * @return the formatted message for the given key and arguments
 */
public String format(final Class clazz, final Locale locale, final String key, final Object... args) {

    // Fetch the bundle
    ResourceBundle bundle = getBundle(getBundleName(clazz), locale);
    String pattern = StringEscapeUtils.unescapeHtml(bundle.getString(key));

    pattern = pattern.replaceAll(PRODUCT_NAME_MACRO, Config.get().getString("web.product_name"));

    if (args == null || args.length == 0) {
        return pattern;
    }

    //MessageFormat uses single quotes to escape text. Therefore, we have to
    //escape the single quote so that MessageFormat keeps the single quote and
    //does replace all arguments after it.
    String escapedPattern = pattern.replaceAll("'", "''");
    MessageFormat mf = new MessageFormat(escapedPattern, locale);
    return mf.format(args);
}

From source file:com.salesmanager.core.util.LabelUtil.java

public String getText(Locale locale, String key, String parameter) {
    Iterator bundleListIterator = bundleList.iterator();
    ResourceBundle myResources = null;
    String label = "";
    while (bundleListIterator.hasNext()) {
        String bundle = (String) bundleListIterator.next();

        try {// w  w w .j av  a2s  . c o  m

            myResources = ResourceBundle.getBundle(bundle, locale);
            if (myResources != null) {
                String l = myResources.getString(key);
                if (l != null) {
                    MessageFormat mFormat = new MessageFormat(l);
                    l = mFormat.format(parameter);
                    label = l;
                    break;
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        }

    }
    return label;
}

From source file:libepg.epg.util.datetime.BCD.java

public BCD(byte bcd) {

    high = bcd >>> 4;//from w  ww  .j a  va  2 s.  c o  m
    low = bcd & 0x0f;

    Object[] parameters = null;
    CHECK: {
        if (!BCD_RANGE.contains(high)) {
            parameters = new Object[] { Integer.toHexString(bcd), "", Integer.toHexString(bcd) };
            break CHECK;
        }
        if (!BCD_RANGE.contains(low)) {
            parameters = new Object[] { Integer.toHexString(bcd), "", Integer.toHexString(bcd) };
            break CHECK;
        }

    }
    if (parameters != null) {
        MessageFormat msg = new MessageFormat(
                "??????????={0} ?={1} ?????={2}");
        throw new IllegalArgumentException(msg.format(parameters));
    }

}