tags:

views:

196

answers:

3

On my java project, I have a bunch of strings externalized on a messages.properties file. On its respective Messages.java file I had the same number of public static String-typed attributes, so I could access those externalized texts.

Then, I implemented a method called getString, which receives the name of the constant as its argument and returns the wanted text. This way, there is no need to declare all the public static Strings-typed attributes inside the Messages.java file.

But after doing this my log became filled with "NLS unused message" messages.

Do you know if there's a way to prevent those warning messages to be logged?

Thanks in advance.

+3  A: 

Messages sounds like a class you wrote, because I don't see it in my JDK 6 javadocs.

It sounds like you've tried to reinvent java.util.ResourceBundle. I'd recommend using that instead and ditching your class. It'll have the added advantage of handling I18N properly.

I don't see any value in hard-coding the public static message keys in the class. It's just another thing you'll have to maintain. If I understand what you're doing properly, I'd throw away your Messages and use ResourceBundle instead.

duffymo
As an addition to your post, If he now got MyClass.AddLog() in 5000 lines, maybe he should just wrap the ResourceBundle-call in the "AddLog" method inside the Messsages class.
Filip Ekberg
I don't even see the need for this, but then I'm not sure that I really understand the requirements here.
duffymo
+5  A: 

Your Messages class - it sounds like it extends org.eclipse.osgi.util.NLS.

If this is the case, it is designed to fill the requirements:

  • to provide compile time checking that a message exists.
  • to avoid the memory usage of a map containing both keys and values (this would be the case in a resource bundle approach).
  • good i18n support.

i.e. NLS populates the value of the Message.staticVariable with the value of the staticVariable found in messages.properties.

The warning logging provides information about a mismatch between the Messages.java and the messages.properties file.

Your getString() method sounds like it does not use any of the advantages of NLS, so as others have suggested, you may be better off using a ResourceBundle.

jamesh
Thank you, jamesh, I was ignorant of NLS. Your answer was better than mine. I'll have to look into it.
duffymo
This comment made my night. Thanks.
jamesh
A: 

duffymo, as jamesh said, Messages is a class I wrote, and it extends org.eclipse.osgi.util.NLS. It has a private static attribute, and its type is... ResourceBundle!

jamesh, thanks for detailing the way NLS works.

Based on your answers I removed my Messages class from my project and added a ResourceBundle-typed attribute on the classes that need to use the externalized strings. Plus, I did it in a way that the lines accessing the externalized strings did not need to be changed.

The number of files on our project has been reduced, the code was kept as clean as before and there are no more log warnings.

Thank you, guys. You rock.

Mario Marinato -br-