What does $NON-NLS-1$ mean?

JavaEclipseEclipse Rcp

Java Problem Overview


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 ?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 to
Project > Properties > Java Compiler > Errors/Warnings
and select the field
Non-externalized strings (missing/unused $NON-NLS$ tag)

Window of the Project Properties on Eclipse

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

It tells the compiler not to complain about a non externalized string, and that it doesn't require localization.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpaulgregView Question on Stackoverflow
Solution 1 - JavaAaron MaenpaaView Answer on Stackoverflow
Solution 2 - JavaMcDowellView Answer on Stackoverflow
Solution 3 - JavaoHoView Answer on Stackoverflow
Solution 4 - JavafishjdView Answer on Stackoverflow
Solution 5 - JavaKees de KooterView Answer on Stackoverflow
Solution 6 - JavaBjörnView Answer on Stackoverflow