There are 2 heavily used classes that help with obtaining localized strings at runtime. The strings themselves are maintained in one properties file per package. Usually what happens is that every class in the package will create a static string helper class instantiation. Since they are static they will last forever once the class is loaded. If the package has a lot of classes then there will be a lot of these instances. But the instances are all really the same thing. They all point at the same properties file in the package.



My proposal is to have an extremely light-weight helper class, in every package that uses localized strings. This one class has only 2 simple static methods that return strings from that package's properties file. The class is copied around to every package and, literally, only one line needs to be changed -- the package declaration.

  • Classes in the package need not import anything -- it is already in the package.
  • Everything in the class is package-private -- no public or protected.
  • The class is naturally thread-safe
  • Classes simply use it like so:
    • String msg = Strings.get("labelHere", "arg1", "arg2");
I have 2 goals with this proposal

  • Make it easier to localize strings in GlassFish code. I don't know how to make it any easier!
  • Avoid Code Duplication


Example Implementation. You literally copy, paste and change the package name. This implementation uses LocalStringsImpl as the workhorse. You could change things around bit to use LocalStringManagerImpl instead.


package com.sun.enterprise.util.cluster;

import com.sun.enterprise.universal.i18n.LocalStringsImpl ;

/**
* Strings -- Get your Strings here.
* One file with Strings
* So one class for messing with them!
* Nothing in here is public or protected. Only for use by this one java package.
* @author Byron Nevins
*/

final class Strings {
private Strings() {
// no instances allowed!
}

final static String get(String indexString) {
return strings.get(indexString);
}

final static String get(String indexString, Object... objects) {
return strings.get(indexString, objects);
}

final private static LocalStringsImpl strings = new LocalStringsImpl(Strings.class);
}








Read More about [Local Strings in GlassFish - A Proposal...