tags:

views:

753

answers:

4

I'm working on a java SE 1.5+ swing application, in conjunction with others. I'm wondering what the best way of managing string resources is. I understand the principles behind resource bundles etc. I'd like to avoid having one property file for every class that needs a string, as this seems a bit of overkill. Especially when you have a lot of classes that may only make a single reference to a string (say in an error handler). On the other hand it makes it easier when collaborating with others as you reduce the risk of merge conflicts.

It seems particularly cumbersome to have to load resource bundles, every time you need to display simple user feedback, likewise in error handlers, when many classes are involved.

What is the most effective way to manage strings in a fairly large application with hundreds of classes, many of which aren't GUI related, but need to pass informative messages back up to the GUI when exceptions occur.

I'm using NetBeans which generally creates a property file for each GUI class, for all text relating to buttons, labels etc.

+10  A: 

What makes you think you have to have a separate properties file for every class? Generally you only need a few (or just one!) properties file (per language, of course).

Just call ResourceBundle.getBundle() with appropriate parameters - you can use the same bundle from multiple classes.

EDIT: Having one set of property files per dialog etc makes it easier to see where any particular string is coming from, but it makes it harder to reuse the same messages etc. Quite where the right balance is will depend on the application.

Jon Skeet
A: 

This may be naive, but what about storing them in a database, either embedded or external? This might simplify management, and changing languages more configurable.

Argalatyr
+2  A: 

JSR 296 Swing Application Framework has support for resource management (and it looks like will be part of Java 7). SAF aims to pre-build parts of a Swing app that many people frequently need while encapsulating best practices. You probably don't want to tie to it directly but its worth taking a look at what they do to see whether it gives you some ideas. If I recall, they use cascading resource bundles with well-defined naming conventions. The latter means you know where to look and the former means that you can reuse properties across some portion of your package hierarchy.

Many JSR 296 resources collected here.

Alex Miller
A: 

I'm going to implement something similar to Launchpad's translation platform this year: https://launchpad.net/+tour/translation

In a nutshell:

  • Concurrent translation
  • Phrase suggestions based on previously-entered phrases
  • Policies, e.g. Partly restricted and structured: anyone can suggest translations, while trusted community members review and approve new work

UPDATE

Of course, this builds on top of ResourceBundle etc, but is a nice way to manage all them strings ;-)

opyate