GWT:在服务器代码中访问 i18n 消息

发布于 2024-12-09 18:28:53 字数 788 浏览 0 评论 0原文

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

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() 创建它的实例,如下所示:

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

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

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

总之:如何在服务器端代码中访问我的 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();

  //...
}

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.

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

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

发布评论

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

评论(5

情话难免假 2024-12-16 18:28:53

在服务器端,您可以使用标准 Java 本地化工具,例如 资源包
请参阅此处了解如何使用它的教程。

// Create a ResourceBundle out of your property files
ResourceBundle labels =
  ResourceBundle.getBundle("LabelsBundle", currentLocale);

// Get localized value
String value = labels.getString(key);

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

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

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.

空名 2024-12-16 18:28:53

我同意迈克尔的观点..我遇到了尝试“本地化”服务器上生成的消息的问题....但我决定在服务器上抛出异常(因为这是一条错误消息,只应在异常情况下发生) )其中包含消息代码,客户端代码可以查找该消息代码并向用户显示正确的本地化消息。

I agree with Michael.. I was having this problem of trying to "localize" messages generated on the server.... but I decided to instead just throw an Exception on the server (because it is an error message which should only happen exceptionally) which contains the message code, which the client code can then look up and show the correct localized message to the user.

寄居人 2024-12-16 18:28:53

有一个很棒的 GWT 国际化库 gwt-dmesg。它允许您在客户端和服务器之间“共享”.properties 文件。但是,该项目似乎已被作者放弃,您必须手动重新编译它才能与 GWT 版本 >= 2.1.0 一起使用。

There's a great library for GWT internationalization gwt-dmesg. It allows you to 'share' .properties files between clent and server. However, project looks to be abandoned by author and you must recompile it manually for use with GWT versio >= 2.1.0.

飘过的浮云 2024-12-16 18:28:53

GWT.create() 只能在客户端代码中使用。

要做的好事是您提供自己的 I18NProvider 类/接口,然后您可以从中扩展到服务器端 I18N 工厂和客户端 I18N 工厂读取相同的资源包。

之后,您可以简单地在整个系统中使用它,统一您的代码。
希望有帮助。

GWT.create() can only be used in client-side code.

The good thing to do is that you provide your own I18NProvider class/interface, from which then you can extend to server side I18N factory and client side I18N factory read the same resource bundle.

After that you can simply use it all over your system, unify your code.
Hope that helps.

來不及說愛妳 2024-12-16 18:28:53

根据vanje的回答,并考虑用于属性文件的编码(这可能很麻烦,因为ResourceBundle默认使用“ISO-8859-1”,这是我想出的解决方案:

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyResourceBundle {

    // feature variables
    private ResourceBundle bundle;
    private String fileEncoding;

    public MyResourceBundle(Locale locale, String fileEncoding){
        this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
        this.fileEncoding = fileEncoding;
    }

    public MyResourceBundle(Locale locale){
        this(locale, "UTF-8");
    }

    public String getString(String key){
        String value = bundle.getString(key); 
        try {
            return new String(value.getBytes("ISO-8859-1"), fileEncoding);
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }
}

使用它的方式与常规 ResourceBundle 用法:

private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)

或者您可以使用默认使用 UTF-8 的备用构造函数:

private MyResourceBundle labels = new MyResourceBundle("es");

Following vanje's answer, and considering the encoding used for the properties files (which can be troublesome as ResourceBundle uses by default "ISO-8859-1", here is the solution I came up with:

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyResourceBundle {

    // feature variables
    private ResourceBundle bundle;
    private String fileEncoding;

    public MyResourceBundle(Locale locale, String fileEncoding){
        this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
        this.fileEncoding = fileEncoding;
    }

    public MyResourceBundle(Locale locale){
        this(locale, "UTF-8");
    }

    public String getString(String key){
        String value = bundle.getString(key); 
        try {
            return new String(value.getBytes("ISO-8859-1"), fileEncoding);
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }
}

The way to use this would be very similar than the regular ResourceBundle usage:

private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)

Or you can use the alternate constructor which uses UTF-8 by default:

private MyResourceBundle labels = new MyResourceBundle("es");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文