开发者

How to use localized constants in GWT shared package

I'm trying to rewrite a GWT application I've written months ago. That applications view logic is outdated though and I've other reasons to rewrite it.

The backend however stays the same and sort of works.

My problem is the general organization of the project. I've got the client, server and shared packages. The client开发者_开发技巧 contains client-related classes that are compiled into JavaScript, the server contains Java-classes that are published onto a server. I use the Constants-interface for the client-side and a properties-file on the server-side.

I've got DTOs and some enumerations in the shared-package as they're used on both the client- and server-side. While the DTOs don't need any kind of translation as they only hold information they're filled with, the enumerations do need them. One of my enumerations looks like this:

public enum ItemFamily {
    ARMORS(0, "A", "Armor"),
    JEWELRY(1, "J", "Jewelry"),
    MATERIALS(2, "M", "Materials"),
    RECIPES(3, "R", "Recipes"),
    WEAPONS(4, "W", "Weapons");

    private final int id;
    private final String label;
    private final String name;

    ItemFamily(final int id, final String label, final String name) {
        this.id = id;
        this.label = label;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String toString() {
        return label;
    }

    public String getName() {
        return name;
    }

    public static ItemFamily fromString(final String str) {
        for (ItemFamily e : ItemFamily.values()) {
            if (e.toString().equalsIgnoreCase(str)) {
                return e;
            }
        }
        return null;
    }

    public static ItemFamily fromId(final int id) {
        for (ItemFamily e : ItemFamily.values()) {
            if (e.id == id) {
                return e;
            }
        }
        return null;
    }
}

I'd like to replace "Armor", "Jewelry", "Recipes" etc. with constants, that would adjust depending on what locale is required by the client application. However: if I tie up the enumerations too tightly to a client-Constants-interface, I might run into trouble when using the enumerations on the server-side, right?

Any ideas regarding the appropriate way of translating shared-stuff is really welcome. Thank you very much for reading and helping me out!

P.s.: if this question has been covered somewhere, I beg my pardon. I've searched the Developer's Guide (Internationalization) as well as stackoverflow. I didn't find a suitable solution though.


The problem is you want to use the same properties file on the server and the client side right?

I had the same problem.

What I did:

  • I used the com.google.gwt.i18n.client.ConstantsWithLookup interface instead of the com.google.gwt.i18n.client.Constants in order to look up the translation for an enum dynamicaly.
  • On the server side you can configure your ResourceBundle to use the same properties files.

On the client side you use you ConstantsWithLookup implementation and on the sever side you use you message bundle. Hence, your enum does not depend on any translation code.

Client side:

    String translation = const.getString(yourEnum.name())

Server side:

    String translation = messageSource.getMessage(yourEnum.name(), null, clientLocale)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜