gembin

OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

HBase, Hadoop, ZooKeeper, Cassandra

Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

About Me

 

学习Eclipse NLS

今天研究了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);
    }

}

posted on 2008-01-23 18:50 gembin 阅读(3646) 评论(4)  编辑  收藏

评论

# re: 学习Eclipse NLS 2008-01-24 20:35 guest

什么是nls  回复  更多评论   

# re: 学习Eclipse NLS 2008-01-26 15:31 gembin

Eclipse SRC里的: No need Localized String 1
可代替ResourceBundle,用来处理资源文件*.properties,效率更好,更方便,很好的扩展性。
当有Key没定义,会get a compile error.
缺点是:必须维护2个文件1个Properties和1个Java文件,他们必须同步  回复  更多评论   

# re: 学习Eclipse NLS 2008-03-04 21:52 jolly

请问绑定的类只能是当前类吗?而且不能用内部类作为绑定类?  回复  更多评论   

# re: 学习Eclipse NLS 2008-03-05 11:57 gembin

是可以的。请看如下例子:
message.properties
------------------
key_1=Say {0} hello to {1} !!
key_2=Say {0} hello to {1} !!
key3=Say {0} hello to {1} !!
key4=Say {0} hello to {1} !!

TobeBindedMsg.java
---------------------
public class TobeBindedMsg {
public static String key3;

static class InnerMsg{
public static String key4;
}
}

TestNLS.java
------------------
import org.eclipse.osgi.util.NLS;
public class TestNLS {
private static final String BUNDLE_NAME = "com.meccala.blog.util.message"; //$NON-NLS-1$
public static String key_2;
static {
NLS.initializeMessages(BUNDLE_NAME, TestNLS.class);
NLS.initializeMessages(BUNDLE_NAME, Inner.class);
NLS.initializeMessages(BUNDLE_NAME, TobeBindedMsg.class);
NLS.initializeMessages(BUNDLE_NAME, TobeBindedMsg.InnerMsg.class);
}

static class Inner {
public static String key_1;
}

public static void main(String ggg[]) {
String s = NLS.bind(Inner.key_1, "1", "gembin");
System.out.println(s);
String s1 = NLS.bind(TestNLS.key_2, "2", "gembin");
System.out.println(s1);
String s2 = NLS.bind(TobeBindedMsg.key3, "3", "gembin");
System.out.println(s2);
String s3 = NLS.bind(TobeBindedMsg.InnerMsg.key4, "4", "gembin");
System.out.println(s3);
}
}



最后的输出:
Say 1 hello to gembin !!
Say 2 hello to gembin !!
Say 3 hello to gembin !!
Say 4 hello to gembin !!
NOTE:因为Key要求是public static 的所以Inner Class必需是static   回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(6)

随笔分类(440)

随笔档案(378)

文章档案(6)

新闻档案(1)

相册

收藏夹(9)

Adobe

Android

AS3

Blog-Links

Build

Design Pattern

Eclipse

Favorite Links

Flickr

Game Dev

HBase

Identity Management

IT resources

JEE

Language

OpenID

OSGi

SOA

Version Control

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

free counters