$NON-NLS-1$
eclipse時,經常在官方的例子中看到一些奇怪的注釋,例如: shell.setText(Messages.getString("TestRef.hello")); //$NON-NLS-1$這$NON-NLS-1$到底代表什麼呢?當時在一陣淺嘗輒止之後,也就忽略了這個問題,如下圖:
現在大家也許對注釋$NON-NLS-1$的含義就能夠猜到個大概了,我個人猜測他也許就是non need localize string 1的縮寫。rcp的文檔里是這樣表述的The string $NON-NLS-1$ is a hint for both the compiler and the Externalization wizard that the first character string on this line is a tag or keyword of some sort and should not be localized. 也就是說$NON-NLS-1$表明本行的第一個string型變數是一個標籤或者關鍵字,不需要被本地化.
代碼示例
//TestRef.javapublic class TestRef {
public static void main(String[] args) {
Shell shell =new Shell();
shell.setText(Messages.getString("TestRef.hello")); //$NON-NLS-1$
}
//Messages.java
public class Messages {
private static final String BUNDLE_NAME = "test";//$NON-NLS-1$
private static final ResourceBundle
RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
//test.properties
TestRef.hello=Hello