Home » Java » What does $NON-NLS-1$ mean?

What does $NON-NLS-1$ mean?

Posted by: admin November 2, 2017 Leave a comment

Questions:

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 ?

Answers:

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.

Questions:
Answers:

The string is not translatable. It tells the Eclipse editor to not flag the string as unresourced. This is important for multilingual applications.

Questions:
Answers:

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.

Questions:
Answers:

NON-NLS means NonNational Language Support.
Wikipedia proposes also NonNative 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

Questions:
Answers:

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.

Questions:
Answers:

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