i18nCreator 生成参数类型不是字符串的消息接口

发布于 2024-11-30 09:36:10 字数 1707 浏览 1 评论 0原文

我正在尝试学习 GWT。我在教程中完成了 StockWatcher 示例应用。

然后我进入下一步:国际化 (I18N )。

I18N 教程要求您手动创建 Message 和 Constant 接口。其中一个消息文件如下所示:

public interface StockWatcherMessages extends Messages {
    @DefaultMessage("''{0}'' is not a valid symbol.")
    String invalidSymbol(String symbol);

    @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
    String lastUpdate(Date timestamp);
}

请注意其中一个方法中的 Date 类型参数。这允许我在调用此方法时传入 Date 实例,并且它会以本地化的方式显示。这效果很好。

GWT 还有一个名为 i18nCreator 的工具,可用于根据相应的 .properties 文件生成这些接口。

我正在使用 gwt-maven-plugin 来构建我的应用程序和 它还支持使用 i18nCreator 作为构建的自动部分

所以我的问题是,当我显式地或通过 maven 插件使用 i18nCreator 时,我最终会得到这样的消息接口:

public interface StockWatcherMessages extends com.google.gwt.i18n.client.Messages {
    @DefaultMessage("''{0}'' is not a valid symbol.")
    @Key("invalidSymbol")
    String invalidSymbol(String arg0);

    @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
    @Key("lastUpdate")
    String lastUpdate(String arg0);
}

lastUpdate 的参数现在是一个字符串。这会导致我的其余代码(使用 Date 实例调用此方法)编译失败。

如何让 i18nCreator 在适当的时候使用除字符串之外的 Java 类型创建接口?我在 Date 方面遇到了这个问题,但我想它可能会出现其他类型,例如 MoneyDouble 或其他类型。

I was trying to learn GWT. I completed the StockWatcher sample app in the tutorial.

I then moved onto the next step: Internationalization (I18N).

The I18N tutorial has you create the Message and Constant interfaces manually. One of the Message files looks like this:

public interface StockWatcherMessages extends Messages {
    @DefaultMessage("''{0}'' is not a valid symbol.")
    String invalidSymbol(String symbol);

    @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
    String lastUpdate(Date timestamp);
}

Notice the Date type parameter in one of the methods. This allows me to pass in a Date instance when I call this method and it gets displayed ina localized fashion. This worked just fine.

GWT also has a tool called i18nCreator which can be used to Generate these interfaces for you, based on the corresponding .properties files.

I am using the gwt-maven-plugin to build my app and it also supports using the i18nCreator as an automatic part of your build.

So my problem is that when I use the i18nCreator, either explicitly or through the maven plugin, I end up with a Message interface like this:

public interface StockWatcherMessages extends com.google.gwt.i18n.client.Messages {
    @DefaultMessage("''{0}'' is not a valid symbol.")
    @Key("invalidSymbol")
    String invalidSymbol(String arg0);

    @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
    @Key("lastUpdate")
    String lastUpdate(String arg0);
}

The argument to lastUpdate is now a String. This causes the rest of my code, which calls this method with a Date instance, to fail compilation.

How can I make the i18nCreator create interfaces using Java types other than Strings when appropriate? I am having this problem with Date but I imagine it could come up with other types like Money or Double or whatever.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文