【個人Android進階之旅】Android 源代碼中的Java代碼中//$NON-NLS-1$ 註釋是什麼意思?

一、背景

最近在負責公司基礎業務和移動基礎設施的開發工做,正在負責Lint代碼靜態檢查工做。所以編寫了自定義的Lint規則,在編寫自定義的Lint規則前,固然是須要去把Google的關於Lint檢測的源代碼看一遍學習學習如何編寫自定義規則。css

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.MXIr5Jhtml

個人github上備份了該源代碼:
https://github.com/ouyangpeng/android-lint-checksjava

在看源代碼的過程當中,我發現一些奇怪的註釋//$NON-NLS-1$,這個註釋究竟是什麼意思呢?android

/** 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$的註釋,以下所示:git

/* (non-Javadoc) * @see javax.lang.model.util.Types#contains(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) */
    @Override
    public 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$ 究竟是什麼意思呢?github

二、//$NON-NLS-1$的意義

參考了網上的資料,
查看了stackoverflow上的一個提問,下面是連接:微信

https://stackoverflow.com/questions/654037/what-does-non-nls-1-meanmarkdown

這裏寫圖片描述

原文解釋以下:eclipse

They silence a warning that Eclipse emits when it encounters string literals (and has been configured to complain).ide

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, // NONNLS1 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軟件,該字符串不須要被本地化操做。

三、參考連接


這裏寫圖片描述

做者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77941890

若是以爲本文對您有所幫助,歡迎您掃碼下圖所示的支付寶和微信支付二維碼對本文進行隨意打賞。您的支持將鼓勵我繼續創做!

這裏寫圖片描述

相關文章
相關標籤/搜索