dynamic Messages/ImageBundle possible?

21 views
Skip to first unread message

denis56

unread,
Nov 23, 2009, 10:40:11 AM11/23/09
to Google Web Toolkit
Hi there,

I wonder if it is possible to use Internationalization and
ImageBundles in GWT in a more dynamic way. For instance, if there is a
client method

void createSportTitleAndImage(String sportId) {

}

is it somehow possible to have the "sportId" argument being
dynamically plugged into the relevant ImageBundle/Messages method to
return appropriate code without the if/else-jungle.

So instead of:

void createSportTitleAndImage(String sportId) {
if (sport.equals("basketball"))
ImageBundle.getBasketballImage();
Messages.getBasketballText();
else if (sport.equals("handball"))
ImageBundle.getHandballImage();
Messages.getHandballText();
else ...
}

to have something like :

void createSportTitleAndImage(String sportId) {
ImageBundle.getSportImageById(sportId);
Messages.getSportTextById(sportId);
}

where getSportByName(sportId) would fetch resource for the key "base +
sportId" ?

That would really simplify things:)
Thanks

Dean S. Jones

unread,
Nov 23, 2009, 10:44:09 AM11/23/09
to Google Web Toolkit
this might help a little

http://code.google.com/p/asmodaiosgwt/

denis56

unread,
Nov 24, 2009, 5:36:17 PM11/24/09
to Google Web Toolkit
Oh, thanks for the hint.

Are you aware of the similar class for Messages?

kgeri

unread,
Nov 25, 2009, 7:58:56 AM11/25/09
to Google Web Toolkit
Hi!

In our GWT projects we were faced with the dynamic message handling
problem for a long time, and just recently I had the free time to code
this module:

Dynamic Messages for GWT:
http://code.google.com/p/gwt-dmesg

It does exactly what you need :) Comments and ideas are of course
welcome.

kgeri

unread,
Nov 25, 2009, 4:12:39 PM11/25/09
to Google Web Toolkit
[Not sure if my previous post went through, so...
]


For dynamic (string-accessible) message support take a look at this:
http://code.google.com/p/gwt-dmesg



I wrote it because we've faced the same problem a million times now in
our GWT projects and it was annoying :)



Hope it helps, comments and ideas are welcome!

denis56

unread,
Nov 28, 2009, 7:33:56 AM11/28/09
to Google Web Toolkit
Thanks a lot,

that's indeed what i needed. It took me sometime to realize what the
requirement to put Messages.properties files in "classpath root"
really means. Is there a possibility to have Messages.properties
reside within the same package where the code using it (maybe though
an interface), since it makes it somewhat uncomfortable the way it
works now, more even so when you have several gwt modules in a single
project that should use difference bundles.

thanks

Sripathi Krishnan

unread,
Nov 29, 2009, 12:09:47 PM11/29/09
to google-we...@googlegroups.com
There is a reason why a lookup method doesn't exist - performance.

With methods like getBasketballImage() and getFootballImage(), the compiler knows which images are actually used. So, if you have 10 sports but only end up using two - the compiler can automatically discard the other eight. With getSportImage(sportId), the compiler has no way of knowing what ids you will end up passing at runtime - so it will have to retain all 10 images.

With strings and messages, the compiler inlines the strings. This means it is as good as hard coding it. Simply said,
new Label(messages.getBasketballText());
  becomes
new Label("I like basketball");

There are times when you absolutely need dynamic behaviour. For those times, use ConstantsWithLookup. With images, manually create a map, or use the generator that was mentioned in this thread. But that should be an exception rather than the norm.


--Sri


2009/11/28 denis56 <denis.er...@gmail.com>
--

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



kgeri

unread,
Nov 30, 2009, 9:05:47 PM11/30/09
to Google Web Toolkit
Sri: Yes, performance is an issue - but not a very big one, I think.

Think about it: how many messages do you have? Maybe a hundred or so?
How often do you need to load the messages? Usually _once_, when
initializing a widget.

I think neither the size (about 3-5 bytes per call + constant size),
nor the speed gain (0.03-0.2 msecs per call in _hosted mode_)
compensate the fact that I got rid of some unnecessary Java code :)

Yes, it's nice to be on the edge and optimize everything - but writing
webapps in GWT (Java) instead of JavaScript is not optimal in the
first place. I believe it's goal was to provide a developer-friendly
way of creating ajax apps, and I see this ideal seriously damaged by
the fact that the development cycle is based on how the GWT internals
work, instead of providing a framework first and optimizing
compilation after that.

Denis: I thought about that myself, and I'm planning the feature. As I
see now, one possible way of creating multiple bundles is like how the
original Messages works - you would have to create an (empty)
interface extending some base interface. This is because the GWT
Generator can only work with classes and methods (and so without a
type it has no idea where to ge the bundles from). It would be nice,
because one would see the bundle and the class side by side - but
would break the original idea of throwing out unnecessary classes :)

Another idea is to provide the bundle names in an extendable property
in the .gwt.xml - this is probably the cleaner solution and I think
I'll go with that.

The reason I didn't implement it in the first place was that we only
had monolithic GWT applications so far. What is a multi-module GWT app
good for? I mean how do you use the modules? Is there any
communication between them, or are they like portlets?

Geri

On nov. 29, 18:09, Sripathi Krishnan <sripathi.krish...@gmail.com>
wrote:
> There is a reason why a lookup method doesn't exist - performance.
>
> With methods like getBasketballImage() and getFootballImage(), the compiler
> knows which images are actually used. So, if you have 10 sports but only end
> up using two - the compiler can automatically discard the other eight. With
> getSportImage(sportId), the compiler has no way of knowing what ids you will
> end up passing at runtime - so it will have to retain all 10 images.
>
> With strings and messages, the compiler inlines the strings. This means it
> is as good as hard coding it. Simply said,
> new Label(messages.getBasketballText());
>   becomes
> new Label("I like basketball");
>
> There are times when you absolutely need dynamic behaviour. For those times,
> use ConstantsWithLookup. With images, manually create a map, or use the
> generator that was mentioned in this thread. But that should be an exception
> rather than the norm.
>
> --Sri
>
> 2009/11/28 denis56 <denis.ergashb...@gmail.com>
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages