GWT easy i18n with GIN
Posted by David Chandler on October 2, 2009
Short version, from the GIN tutorial:
Longer version
Suppose we have some GWT Constants we want to make available application-wide. We have our usual Constants interface
import com.google.gwt.i18n.client.Constants; public interface MyConstants extends Constants { String copyright(); String logoPath(); }
and the corresponding MyConstants.properties in the same package:
copyright=Copyright 2009 David M Chandler All Rights Reserved. logoPath=/images/logo.jpg
Now suppose we don’t want to call GWT.create() on it in every class in order to save resources. Thanks to the GIN capability mentioned above, any class created with GIN (which is all views and presenters if you’re using gwt-presenter) can simply do this:
@Inject private MyConstants constants;
There’s no need to create a binding for MyConstants in our GIN module for the above to work, but to get singleton behavior, which is the whole point of this exercise, we must bind it as such in our GIN Module:
bind(MyConstants.class).in(Singleton.class);
Now we’ve theoretically saved some memory and/or CPU by using a singleton, although I wouldn’t be surprised if GWT.create() already does this optimization for Constants and Images…
Jeff said
Couldn’t you add @Singleton to your interface eliminating the need to add your binding? I know that would work for a class but I’m not sure with interfaces.
David Chandler said
Great idea, but unfortunately does not work. I get an error from Guice that “scope annotations are not supported for abstract types.” Thanks, though.
Base presenter and view classes for gwt-presenter « TurboManage said
[…] In similar fashion, I define a corresponding BaseView that eliminates the need for the startProcessing() and stopProcessing() methods required by gwt-presenter’s WidgetDisplay interface. Besides eliminating boilerplate, I use the base view constructor to inject references to my applications Constants and Images singletons (see this previous post): […]