如何在 GWT 共享包中使用本地化常量

发布于 2024-12-05 08:36:09 字数 1685 浏览 0 评论 0原文

我正在尝试重写几个月前编写的 GWT 应用程序。不过,应用程序视图逻辑已经过时了,我还有其他理由重写它。

然而,后端保持不变并且可以正常工作。

我的问题是项目的总体组织。我有客户端、服务器和共享包。客户端包含编译为 JavaScript 的客户端相关类,服务器包含发布到服务器上的 Java 类。 我在客户端使用常量接口,在服务器端使用属性文件。

我在共享包中使用了 DTO 和一些枚举,因为它们在两个客户端上都使用客户端和服务器端。虽然 DTO 不需要任何类型的翻译,因为它们只保存所填充的信息,但枚举确实需要它们。 我的枚举之一如下所示:

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;
    }
}

我想用常量替换“Armor”、“Jewelry”、“Recipes”等,这些常量将根据客户端应用程序所需的区域设置进行调整。但是:如果我将枚举与客户端常量接口联系得太紧密,那么在服务器端使用枚举时可能会遇到麻烦,对吧?

任何有关翻译共享内容的适当方式的想法都非常受欢迎。非常感谢您的阅读和帮助我!

PS:如果这个问题已经在某个地方被提及,请原谅。我搜索了开发人员指南(国际化)以及 stackoverflow。但我没有找到合适的解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-12-12 08:36:09

问题是您想在服务器和客户端上使用相同的属性文件,对吗?

我也有同样的问题。

我做了什么:

  • 我使用 com.google.gwt.i18n.client.ConstantsWithLookup 接口而不是 com.google.gwt.i18n.client.Constants 来动态查找枚举的翻译。
  • 在服务器端,您可以配置 ResourceBundle 以使用相同的属性文件。

在客户端,您使用 ConstantsWithLookup 实现,在服务器端,您使用消息包。因此,您的枚举不依赖于任何翻译代码。

客户端:

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

服务器端:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文