今天研究了1下Eclipse的NLS,觉得不错,与大家分享....
messages.properties
key_1=hello world,here is NLS
key_2=hello {0}, welcome to my blog
key_3=hello {0}, please reply to{1} if you like this article
其中的 {0} 代表MessageHelper.bind(MessageHelper.key_2, "gembin"); 里的第1个参数,一次类推
public class MessageHelper extends NLS {
//com.exmple.messages 中的点将会被转换成"/"
// 所以最终的路径为:com/exmple/messages.properties
private static final String BUNDLE_NAME = "com.exmple.messages"; //$NON-NLS-1$
public static String key_1;
public static String key_2;
static{
initializeMessages(BUNDLE_NAME, MessageHelper.class);
}
/**
* Bind the given message's substitution locations with the given string value.
*
* @param message
* the message to be manipulated
* @param binding
* the object to be inserted into the message
* @return the manipulated String
*/
public static String bind(String message, Object binding) {
return NLS.bind(message, binding);
}
/**
* Bind the given message's substitution locations with the given string values.
*
* @param message
* the message to be manipulated
* @param binding1
* An object to be inserted into the message
* @param binding2
* A second object to be inserted into the message
* @return
*/
public static String bind(String message, Object binding1, Object binding2) {
return NLS.bind(message, binding1, binding2);
}
/**
* Bind the given message's substitution locations with the given string values.
*
* @param message
* the message to be manipulated bindings
* @param bindings
* An array of objects to be inserted into the message
* @return the manipulated String
*/
public static String bind(String message, Object[] bindings) {
return NLS.bind(message, bindings);
}
/**
* Initialize the given class with the values from the specified message bundle.
*
* @param bundleName -
* fully qualified path of the class name
* @param clazz -
* the class where the constants will exist
*/
@SuppressWarnings("unchecked")
public static void initializeMessages(String bundleName, Class clazz) {
NLS.initializeMessages(BUNDLE_NAME, MessageHelper.class);
}
public static void main(String sp[]){
String s=MessageHelper.bind(MessageHelper.key_2, "hello");
System.out.println(s);
}
}