【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?...
1、背景
最近在負責公司基礎業務和移動基礎設施的開發工作,正在負責Lint代碼靜態檢查工作。因此編寫了自定義的Lint規則,在編寫自定義的Lint規則前,當然是需要去把Google的關于Lint檢測的源代碼看一遍學習學習如何編寫自定義規則。
google官方的lint源代碼鏈接為: 
https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks?spm=5176.100239.blogcont6918.10.MXIr5J
我的github上備份了該源代碼: 
https://github.com/ouyangpeng/android-lint-checks
在看源代碼的過程中,我發現一些奇怪的注釋//$NON-NLS-1$,這個注釋到底是什么意思呢?
/** Using a view inflater unconditionally in an AdapterView */public static final Issue ISSUE = Issue.create("ViewHolder", //$NON-NLS-1$"View Holder Candidates","When implementing a view Adapter, you should avoid unconditionally inflating a " +"new layout; if an available item is passed in for reuse, you should try to " +"use that one instead. This helps make for example ListView scrolling much " +"smoother.",Category.PERFORMANCE,5,Severity.WARNING,IMPLEMENTATION).addMoreInfo("http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder");private static final String GET_VIEW = "getView"; //$NON-NLS-1$static final String INFLATE = "inflate"; //$NON-NLS-1$除了上面的//$NON-NLS-1$注釋之外,還有類似于 //$NON-NLS-2$和//$NON-NLS-3$的注釋,如下所示:
/* (non-Javadoc)* @see javax.lang.model.util.Types#contains(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror)*/@Overridepublic boolean contains(TypeMirror t1, TypeMirror t2) {switch(t1.getKind()) {case EXECUTABLE :case PACKAGE :throw new IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$default:break;}switch(t2.getKind()) {case EXECUTABLE :case PACKAGE :throw new IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$default:break;}throw new UnsupportedOperationException("NYI: TypesImpl.contains(" + t1 + ", " + t2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$}反正看了下代碼,基本上所有的使用字符串的地方,后面都會接上這么一個注釋,//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$。那么 這個 //$NON-NLS-1$ 到底是什么意思呢?
2、//$NON-NLS-1$的意義
參考了網上的資料, 
 查看了stackoverflow上的一個提問,下面是鏈接:
https://stackoverflow.com/questions/654037/what-does-non-nls-1-mean
原文解釋如下:
They silence a warning that Eclipse emits when it encounters string literals (and has been configured to complain).
The idea is that UI messages should not be embedded as string literals, but rather sourced from a resource file (so that they can be translated, proofed, etc). Consequently, Eclipse can be configured to detect string literals, so that you don’t accidentally have leave unexternalized UI strings in the code; however, there are strings which should not be externalized (such as regexps) and so, //NON?NLS?1 gives you a way to communicate that fact to the compiler.
使用Google翻譯內容如下:
當它遇到字符串文字(并已被配置為抱怨)時,它們會使Eclipse發出警告。
這個想法是,UI消息不應該嵌入字符串文字,而是從資源文件中獲取(以便可以翻譯,校對等)。因此,Eclipse可以配置為檢測字符串文字,這樣您就不會意外地在代碼中留下無用的UI字符串;但是,有些字符串不應該被外部化(如regexps),所以// $ NON-NLS-1 $給你一個方法來將該事實傳遞給編譯器。
查看鏈接: 
http://www.eeworm.com/read/185704/8990379/3/
里面有一段注釋對// $ NON-NLS-1 $進行了描述,如下所示:
描述內容為:
The string $NON-NLS-1$ is a hint for both the compiler = 
 and the=20 
 Externalization wizard that the first character string on this line is a = 
 tag or=20 
 keyword of some sort and should not be localized. 
大概含義就是:
$NON-NLS-1$表明本行的第一個string型變量是一個標簽或者關鍵字,不需要被本地化。
參考鏈接: 
http://www.eclipse.org/articles/Article-Internationalization/how2I18n.html
里面舉了一個例子,如下所示: 
總結一下:使用了//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$的含義就是告訴Eclipse等IDE軟件,該字符串不需要被本地化操作。
3、參考鏈接
- https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks?spm=5176.100239.blogcont6918.10.MXIr5J 
- https://github.com/ouyangpeng/android-lint-checks 
- https://stackoverflow.com/questions/654037/what-does-non-nls-1-mean 
- http://www.eeworm.com/read/185704/8990379/3/ 
- http://blog.csdn.net/mr_yanfei/article/details/47271 
- https://ask.helplib.com/101812 
- http://www.cnblogs.com/DreamDrive/p/4234587.html 
- http://www.eclipse.org/articles/Article-Internationalization/how2I18n.html 
- https://gxnotes.com/article/21368.html 
作者:歐陽鵬 歡迎轉載,與人分享是進步的源泉! 
 轉載請保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77941890
如果覺得本文對您有所幫助,歡迎您掃碼下圖所示的支付寶和微信支付二維碼對本文進行隨意打賞。您的支持將鼓勵我繼續創作!
總結
以上是生活随笔為你收集整理的【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?...的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 简单的js文本框提示语
- 下一篇: vueRouter-命名视图
