开发者

Loading GWT Messages from a Database

开发者 https://www.devze.com 2022-12-25 09:24 出处:网络
In GWT one typically loads i18n strings using a interface like this: public interface StatusMessage extends Messages {

In GWT one typically loads i18n strings using a interface like this:

public interface StatusMessage extends Messages {
   String error(String username);
   :
}

which then loads the actual strings from a StatusMessage.property file:

error=User: {0} does not have access to resource

This is a great solution, however my client is unbendable in his demand for putting the i18n strings in a database so they can be changed at runtime (though its not a requirement that they be changed realtime).

One solution is to create a async service which takes a message ID and user locale and returns a string. I have implemented this and find it terribly ugly (it introduces a huge amount of extra communication with the server, plus it makes property placeholder replacement rather complicated).

So my question is this, can I in some开发者_运维百科 nice way implement a custom message provider that loads the messages from the backend in one big swoop (for the current user session). If it can also hook into the default GWT message mechanism, then I would be completely happy (i.e. so I can create a interface like above and keep using the the nice {0}, {1}... property replacement format).

Other suggestions for clean database driven messages in GWT are also welcome.


GWT's in-built Dictionary class is the best way to move forward. Here's the official documentation on how to use it.


Let's say your application has 500 messages per locale at an average of 60 chars per message. I wouldn't think twice about loading all of these when the user logs in or selects his language: it's <50k of data and should not be an issue if you can assume broadband connectivity being available...your "one swoop" suggestion. I already do that in one GWT application, although it's not messages, but properties that are read from the database.


i think you might find this article useful: http://googlewebtoolkit.blogspot.com/2010/02/putting-test-data-in-its-place.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+blogspot/NWLT+(Google+Web+Toolkit+Blog)&utm_content=Google+Reader

What you could do is set up a TextResource and then, you could just change the text at runtime. I haven't tried this but I am very confident that this would work.


To optimize the performance, you can put your messages in a js resource, for example: http://host.com/app/js/messages.js?lang=en, then map this resource to a servlet which will take the messages dictionary from your cache (a singleton bean, for instance) and write it to the response.

To optimize even more, you can:
- put a parameter to the resource URL, for example: .../messages.js?lang=en&version={last updated date of messages}
- {last updated date of messages} is stored somewhere in DB
- whenever user updates the messages, {last updated date of messages} will change
- in the response to browser, set Cache-control as you want to tell browser to cache your messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号