如何本地化GWT客户端代码中的枚举值? [英] How to localize enum values in GWT client code?

查看:98
本文介绍了如何本地化GWT客户端代码中的枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的GWT客户端代码中使用枚举类来定义一组类型。

  public enum MyType {

FIRST_TYPE(first),SECOND_TYPE(second),THIRD_TYPE(third);

私有字符串标题;

private MyType(String title){
this.title = title;
}

public String getTitle(){
return this.title;
}

}

如何定位枚举值将它们翻译成不同的语言? 标题字段并不重要,如果这有助于解决问题,可以将其删除。



我知道来自Java的ResourceBundle 方法,但是在GWT的客户端代码中不起作用。

我设法通过使用GWT的 ConstantsWithLookup 界面来解决问题。
这是解决方案:

MyType.java

  public enum MyType {

FIRST_TYPE,SECOND_TYPE,THIRD_TYPE;

private final MyConstantsWithLookup constants = GWT.create(MyConstantsWithLookup.class)

public String getTitle(){
return this.constants.getString(this.name( ));
}
}

MyConstantsWithLookup.java

  public interface MyConstantsWithLookup extends ConstantsWithLookup {

字符串FIRST_TYPE();

String SECOND_TYPE();

String THIRD_TYPE();
}

MyConstantsWithLookup.properties

  FIRST_TYPE = first 
SECOND_TYPE = second
THIRD_TYPE = third


I am using an enumeration class in my GWT client's code to define a set of types.

public enum MyType {

    FIRST_TYPE("first"), SECOND_TYPE("second"), THIRD_TYPE("third");

    private String title;

    private MyType(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

}

How is it possible to localize the enum values to translate them into different languages? The title field is not that important and could be dropped if this helps to solve the problem.

I know the ResourceBundle approach from Java, but that is not working in GWT's client code.

解决方案

I managed to solve the problem by using GWT's ConstantsWithLookup interface. Here is the solution:

MyType.java

public enum MyType {

    FIRST_TYPE, SECOND_TYPE, THIRD_TYPE;

    private final MyConstantsWithLookup constants = GWT.create(MyConstantsWithLookup.class)

    public String getTitle() {
        return this.constants.getString(this.name());
    }
}

MyConstantsWithLookup.java

public interface MyConstantsWithLookup extends ConstantsWithLookup {

    String FIRST_TYPE();

    String SECOND_TYPE();

    String THIRD_TYPE();
}

MyConstantsWithLookup.properties

FIRST_TYPE = first
SECOND_TYPE = second
THIRD_TYPE = third

这篇关于如何本地化GWT客户端代码中的枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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