GWT:在服务器代码中访问 i18n 消息 [英] GWT: Accessing i18n messages in server code

查看:15
本文介绍了GWT:在服务器代码中访问 i18n 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 com.google.gwt.i18n.client.Messages 类的接口,我用它来检索 GWT 应用程序中的 i18n 消息.它看起来像这样:

I have an interface that extends the com.google.gwt.i18n.client.Messages 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")
  @Key("message1")
  String message1();

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

  //...
}

通常,我使用 GWT.create() 创建它的一个实例,如下所示:

Normally, I create an instance of it using GWT.create() like so:

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

但是,这不适用于服务器端代码,仅适用于客户端代码(它会抛出一个错误,指出 GWT.create() 只能在客户端代码中使用).

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).

类似问题的答案指向一个单独的库,您可以下载它可以让你访问服务器上的 i18n 消息,但我不想下载任何额外的库(这似乎是一个简单的问题,必须有一个简单的解决方案).

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).

总结:如何在服务器端代码中访问我的 i18n 消息?谢谢.

推荐答案

在服务器端,您可以使用标准的 Java 本地化工具,例如 ResourceBundle.查看此处了解如何使用它的教程.

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

从属性文件创建接口并通过延迟绑定提供实现的 GWT 特定方式不能在服务器端 Java 上使用.

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.

如果你无所畏惧,愿意花时间,你可以实现一个代码生成步骤来读取你的属性文件并为你的消息接口生成实现类.这正是 Google GWT 编译器在幕后所做的.

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.

这篇关于GWT:在服务器代码中访问 i18n 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆