Java $NON-NLS-1$ 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/654037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does $NON-NLS-1$ mean?
提问by paulgreg
In Eclipse source code, I've found some '$NON-NLS-1$' in comments used like that :
在 Eclipse 源代码中,我在这样使用的注释中发现了一些“$NON-NLS-1$”:
private String toolTip = ""; //$NON-NLS-1$
What does that mean ?
这意味着什么 ?
采纳答案by Aaron Maenpaa
They silence a warning that Eclipse emits when it encounters string literals (and has been configured to complain).
它们使 Eclipse 在遇到字符串文字时发出的警告静音(并且已配置为抱怨)。
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.
这个想法是 UI 消息不应该嵌入为字符串文字,而是来自资源文件(以便它们可以被翻译、校对等)。因此,Eclipse 可以配置为检测字符串文字,这样您就不会意外地在代码中留下未外部化的 UI 字符串;但是,有些字符串不应该被外部化(例如正则表达式),因此 //$NON-NLS-1$ 为您提供了一种将这一事实传达给编译器的方法。
回答by McDowell
The string is not translatable. It tells the Eclipse editor to not flag the string as unresourced. This is important for multilingual applications.
该字符串不可翻译。它告诉 Eclipse 编辑器不要将该字符串标记为无资源。这对于多语言应用程序很重要。
回答by Bj?rn
It tells the compiler not to complain about a non externalized string, and that it doesn't require localization.
它告诉编译器不要抱怨非外部化的字符串,并且它不需要本地化。
回答by Kees de Kooter
It's used by Eclipse to indicate that a string doesn't need to be translated, probably because it's not going to be seen by the application's users.
Eclipse 使用它来指示不需要翻译字符串,这可能是因为应用程序的用户不会看到它。
回答by fishjd
If you are an Android developer. All strings the user may see should be in the resource file /res/values/strings.xml to read strings.xml file in the code you use R.string.. By adding the tag //$NON-NLS-$ you are noting that the string will not be seen by users.
如果你是安卓开发者。用户可能看到的所有字符串都应该在资源文件 /res/values/strings.xml 中,以便在您使用 R.string 的代码中读取 strings.xml 文件。通过添加标签 //$NON-NLS-$ 您是请注意,用户将看不到该字符串。
The warning in Eclipse Helios may be turned on at Window -> preferences -> java -> Compiler -> code style -> "Non-externalized Strings (missing/unused &NON-NLS$ tag)
.
Eclipse Helios 中的警告可能会在Window -> preferences -> java -> Compiler -> code style -> "Non-externalized Strings (missing/unused &NON-NLS$ tag)
.
If you are planning on programming your activity to be multi-language, it would be recommended to turn this on. And then adding the &NON-NLS$ tag to strings that are internal to you activity. Eclipse will add &NON-NLS$ tag in the quick-fix if you right click on the warning or error.
如果您计划将您的活动编程为多语言,则建议启用此功能。然后将 &NON-NLS$ 标记添加到您活动内部的字符串中。如果右键单击警告或错误,Eclipse 将在快速修复中添加 &NON-NLS$ 标记。
回答by olibre
NON-NLS
means Non-National Language Support.
Wikipediaproposes also Non-Native Language Support(NLS) but this last one is not very used.
NON-NLS
装置非- ñ憩大号anguage小号upport。
维基百科还提出非- ñative大号anguage小号upport(NLS),但是这最后一个不是很习惯。
NLSis about internationalizing your application. Eclipse help to locate hard-coded strings in your code. To indicate a string is not part of the internationalization, append the comment //$NON-NLS-x$
where x is the position of the string. On the following example both "!"
are hard-coded strings not part of the internationalization:
NLS是关于国际化您的应用程序。Eclipse 有助于在您的代码中定位硬编码字符串。要指示字符串不是国际化的一部分,请附加注释//$NON-NLS-x$
,其中 x 是字符串的位置。在以下示例中,两者"!"
都是不属于国际化的硬编码字符串:
public String foo(String key) {
return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
}
Notes:
笔记:
- the leading
//
is necessary each time - no global
$NON-NLS$
for multiple strings within the same line
(e.g. if your line has six strings, you have to write six times//$NON-NLS-x$
)
//
每次都需要领导$NON-NLS$
同一行内的多个字符串没有全局性
(例如,如果您的行有六个字符串,则必须写六次//$NON-NLS-x$
)
The book EMF: Eclipse Modeling Framework at page 250says:
Non-NLS Markers—Eclipse's Java compiler has the ability to flag non-externalized strings as a warning or error, in order to facilitate enablement of National Language Support (NLS). EMF-generated code does not use hard coded strings for messages that the user will see; however, string literals do appear frequently, for example, as keys for lookup of externalized strings in a property file. This property controls whether to include comments that mark those literals as non-translatable, so that the compiler will not flag them.
非 NLS标记——Eclipse的 Java 编译器能够将非外部化字符串标记为警告或错误,以便于启用国家语言支持 (NLS)。EMF 生成的代码不会对用户将看到的消息使用硬编码字符串;然而,字符串文字确实经常出现,例如,作为在属性文件中查找外化字符串的键。此属性控制是否包含将这些文字标记为不可翻译的注释,以便编译器不会标记它们。
For more details see also the pages The Generator GUIand How to Internationalize your Eclipse Plug-In.
有关更多详细信息,另请参阅The Generator GUI和How to Internationalize your Eclipse Plug-In 页面。
You can enable/disable this feature. On Eclipse Neon go toProject > Properties > Java Compiler > Errors/Warnings
and select the fieldNon-externalized strings (missing/unused $NON-NLS$ tag)
您可以启用/禁用此功能。在 Eclipse Neon 上,转到Project > Properties > Java Compiler > Errors/Warnings
并选择该字段Non-externalized strings (missing/unused $NON-NLS$ tag)