The postings on this site are the contributor's and don’t necessarily represent IBM’s positions, strategies or opinions.
June 7th, 2008 Alex Moffat Posted in EffectiveGWT, GWT | 1 Comment »
The GWT documentation has a great explanation of how to use the GWT Constants interface for internationalization of static strings. This post looks at the JavaScript that gets generated, using GWT 1.5, and offers a few suggestions for how to make effective use of Constants in larger applications.
I’m going to start by looking at a trivial case. First, a simple interface that extends the Constants interface.
public interface ExampleConstants extends Constants {
String one();
String two();
}
Next a properties file (ExampleConstants.properties) to provide the default values.
one = One two = Two
And now a very simple class that makes use of ExampleConstants.
public class Example implements EntryPoint {
public void onModuleLoad() {
ExampleConstants constants = GWT.create(ExampleConstants.class);
String value = constants.one();
Window.alert(value);
}
}
Compiling this to produce DETAILED output gives
$wnd.alert($intern_2);
as the compilation of the onModuleLoad method where $intern_2 is a global defined at the top of the script with the statement var $intern_2 = 'One'. This code will execute faster than the code you’d probably write by hand, especially when executed in a loop, because using a variable like this is faster than using a literal like $wnd.alert('One');
So, with the Java written in this way ExampleConstants is completely removed and only the values are put into the JavaScript. Does it make a difference if you write the code in a different way? Well, no. If you change constants from a variable into a private static final field then the generated code is the same. There’s also no difference between using GWT.create(...) in one object and passing the Constants implementation to another object and having each object call GWT.create(...) itself. Bearing this in mind what’s the best way to use Constants in GWT code? I recommend the following
Firstly, reduce the number of Constants interfaces, and hence .properties files, you have by using a single Constants interface per module. This should be placed in the same package as the module entry point. If you have code that is shared between modules and makes use of Constants you’ll need a shared module for the shared code. In this case the interface and properties file should be placed in one of the top level common packages. A smaller number of .properties files makes it easier to manage the translation process and keeps you aware of possible duplicate values.
Secondly, remember that because of the special way these interfaces are handled by the GWT compilation process using deferred binding and the agressive inlining of literals that takes place during compilation you don’t need to worry about using GWT.create(...) in multiple different places. No extra object instances are created. Each .java file should use a public static final field for any Constants implementation it is going to use, this should generally be one. All classes defined in the file (top level and possible inner classes) can make use of these fields.
Thirdly, when you’re choosing a name for a method in the Constants interface use something that makes it clear what the default value for the property is. This gives you an idea, when you’re reading the code, of what value is going to be used so that you can more easily spot places where the wrong method is called. A method name like pleaseEnterYourPassword should be preferred to pwdPromptOne or valueOne when the default value is going to be Please enter your password. I think it’s best not to use the @Key annotation in your Constants interface to separate the method name from the property key, it’s another level of indirection that you have to handle mentally.
December 22nd, 2008 at 5:26 am
thanks dude! I have been searching for this for the longest time. keep it up!