Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading GWT Messages from a Database

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.

like image 773
Lars Tackmann Avatar asked Apr 04 '10 20:04

Lars Tackmann


2 Answers

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

like image 158
Sripathi Krishnan Avatar answered Oct 26 '22 18:10

Sripathi Krishnan


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.

like image 40
Tomislav Nakic-Alfirevic Avatar answered Oct 26 '22 17:10

Tomislav Nakic-Alfirevic