【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?

2017-09-12 10:34 出处:csdn 人气: 评论(0

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 */publicstaticfinal 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");

    privatestaticfinal String GET_VIEW = "getView";  //$NON-NLS-1$staticfinal 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)
     */@Overridepublicbooleancontains(TypeMirror t1, TypeMirror t2) {
        switch(t1.getKind()) {
            case EXECUTABLE :
            case PACKAGE :
                thrownew IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$default:
                break;
        }
        switch(t2.getKind()) {
            case EXECUTABLE :
            case PACKAGE :
                thrownew IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$default:
                break;
        }
        thrownew 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, //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软件,该字符串不需要被本地化操作。

3、参考链接


这里写图片描述

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77941890

如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作!

这里写图片描述

本文标签:

相关文章

网站内容来源于互联网,仅供用于技术学习,请遵循相关法律 规,如有侵权,请联系管理员删除

Copyright © 2002-2017 JISHUX. 技术栈 版权所有

京ICP备15061484号-3