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

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

问题描述

我有一个接口,它扩展了 com.google.gwt.i18n.client.Messages 类,我用它来检索我的GWT应用程序中的国际化邮件。它看起来像这样:

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

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

// ...
}

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

 私人MyMessages消息= GWT.create(MyMessages.class); 

然而,这不适用于服务器端代码,只有客户端代码(它会引发错误,说GWT.create()只能在客户端代码中使用)。



答案类似的问题指向一个单独的库,您可以下载这个库,这将允许您访问服务器上的i18n消息,但我不想要下载任何额外的库(这看起来像一个简单的问题,必须有一个简单的解决方案)。



总结:如何访问我的i18n消息在服务器端,你可以使用标准的Java本地化工具,如 。 a href =http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html =noreferrer> ResourceBundle 。
查看这里了解如何使用它的教程。

  //从属性文件中创建一个ResourceBundle 
ResourceBundle标签=
ResourceBundle.getBundle LabelsBundle,currentLocale);

//获取本地化值
字符串值= labels.getString(key);

GWT从属性文件创建接口并通过延迟绑定提供实现的特定方式不能如果你无所畏惧并愿意花时间,你可以实现一个代码生成步骤来读取你的属性文件并生成实现类你的消息界面。这正是Google GWT编译器在现场所做的。


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

  //...
}

Normally, I create an instance of it using GWT.create() 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.

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

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