Message Bundles

Description

Standard Java ResourceBundles have quite inefficient space characteristics. Since a running Eclipse tends to have many externalized messages, we have implemented a new message bundle system to be used in Eclipse. The mechanism is quite simple and completely generic - it can be used anywhere.

Summary of the new approach:

When creating a new message:

Example Files:

Client Code

Old Code:

public class MyClass {
  public void myMethod() {
    String message;
    ...
    // no args
    message = Messages.getString("key.one"); //$NON-NLS-1$
    ...
    // bind one arg
    message = MessageFormat.format(Messages.getString("key.two"), new Object[] {"example usage"}); //$NON-NLS-1$ //$NON-NLS-2$
    ...
  }
}

New Code:

public class MyClass {
  public void myMethod() {
    String message;
    ...
    // no args
    message = Messages.key_one;
    ...
    // bind one arg
    message = NLS.bind(Messages.key_two, "example usage"); //$NON-NLS-1$
    ...
  }
}

Messages.java

Old Code:

public class Messages {
  private static final String BUNDLE_NAME = "org.eclipse.core.utils.messages"; //$NON-NLS-1$
  private static final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);

  public static String getString(String key) {
    try {
      return bundle.getString(key);
    } catch (MissingResourceException e) {
      return key;
    }
  }
}

New Code:

import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
  private static final String BUNDLE_NAME = "org.eclipse.core.utils.messages"; //$NON-NLS-1$

  public static String key_one;
  public static String key_two;
  ...
  static {
    NLS.initializeMessages(BUNDLE_NAME, Messages.class);
  }
}

messages.properties

Old Code:

key.one = Hello world.
key.two = This is an {0} of binding with one argument.

New Code:

key_one = Hello world.
key_two = This is an {0} of binding with one argument.