Michael Michael - 5 months ago 26x
Java Question

GWT: Accessing i18n messages in server code

I have an interface that extends the

class, which I use for retrieving i18n messages in my GWT application. It looks like this:

public interface MyMessages extends com.google.gwt.i18n.client.Messages {
@DefaultMessage("Hello world")
String message1();

@DefaultMessage("Hello again")
String message2();


Normally, I create an instance of it using
like so:

private MyMessages messages = GWT.create(MyMessages.class);

However, this does not work with server-side code, only client-side code (it throws an error saying that GWT.create() is only usable in client-side code).

The answer to a similar question points to a separate library that you can download which will let you access the i18n messages on the server, but I don't want to download any extra libraries (this seems like a simple problem, there must be a simple solution).

In summary: How can I access my i18n messages in server-side code? Thanks.


On the server side you can use the standard Java localization tools like ResourceBundle. Look here for a tutorial how to use it.

// Create a ResourceBundle out of your property files
ResourceBundle labels =
  ResourceBundle.getBundle("LabelsBundle", currentLocale);

// Get localized value
String value = labels.getString(key);

The GWT specific way of creating an interface out of your property files and providing implementations via deferred binding can not be used on sever side Java.

If you are fearless and willing to spend the time, you can implement a code generation step to read your property files and generate implementation classes for your message interface. That's exactly what the Google GWT compiler does behind the scene.