What does $NON-NLS-1$ mean?
In Eclipse source code, I've found some '$NON-NLS-1$' in comments used like that :
private String toolTip = ""; //$NON-NLS-1$
What does that mean ?
Solution 1:
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.
Solution 2:
The string is not translatable. It tells the Eclipse editor to not flag the string as unresourced. This is important for multilingual applications.
Solution 3:
NON-NLS
means Non-National Language Support.
Wikipedia proposes also Non-Native Language Support (NLS) but this last one is not very used.
NLS is 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:
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$
)
The book EMF: Eclipse Modeling Framework at page 250 says:
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.
For more details see also the pages The Generator GUI and 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)
Solution 4:
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.
The warning in Eclipse Helios may be turned on at 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.