339

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 ?

7 Answers 7

392

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.

10
  • 15
    So, what does NLS stand for?
    – Tyler
    Commented Jul 15, 2010 at 20:07
  • 44
    @MatrixFrog it seems to be "National Language Support", at least that's what it's referred to here: msdn.microsoft.com/en-us/library/ms906482.aspx Commented Aug 12, 2010 at 17:54
  • 25
    To be evaluative, I think such IDE-specific markers should not be used if code is altered in different environments. While the marker does no harm it clutters the code and makes it slightly more difficult to maintain.
    – migu
    Commented Nov 10, 2011 at 13:32
  • 4
    So, how do you configure Eclipse to detect string literals, so that missing //$NON-NLS-1$ would raise a warning or smt?
    – rpr
    Commented May 8, 2012 at 14:34
  • 3
    NetBeans Platform uses //NOI18N comment keyword for the same purpose Commented May 18, 2012 at 14:17
62

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

1
  • 9
    Good explanation to why the flag is called NON NLS (No National Language Support). Thanks.
    – Kaadzia
    Commented Sep 27, 2013 at 9:16
38

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

15

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.

10

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.

1
  • Some strings don't work properly when they're translated. If a string represents a charset, HTML/XML tag, regex, etc., they could change the behaviour of the program.
    – yingted
    Commented Mar 18, 2012 at 3:11
6

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

2
  • 4
    I don't think it's the compiler, I think it's Eclipse that complains. Commented Mar 17, 2009 at 12:40
  • 6
    Eclipse has it's own compiler as part of JDT which it uses for this kind of thing. So yes it is the compiler (Eclipse's compiler anyway). Commented Mar 17, 2009 at 12:45
0

It is an annotation that indicates that an annotated code element is a string which is not visible to users, which doesn’t require localization, and which doesn’t contain strings requiring localization. When you annotate an element with NonNls, localization tools will skip this element and strings inside it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.