How to localize enum values in GWT client code?

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.

How to reference third-party library source code for client use in GWT

The GWT documentation states that all the source code for compilation to JavaScript on the client-side must be in a sub-package of the gwt.xml file. How does this work for when one references third-

GWT client global configuration values

I need to store a few configuration parameters on the client side of my GWT app–I’m only using the client side of the framework. I just want to store things like an API access URL base. Ideally, it

How to group Enum values?

How can I group Enum values? Assume I have an enum like public enum Colors { LightBlue, LightGreen, DarkGreen, Black, White, LightGry, Yellow } Now I want to define some groups of colors, e.g. the li

How to debug GWT client side code on jboss

I’m starting with GWT and smartgwt, and I realized that when your code works on jetty will no necessarily work on Jboss. Is there a way that I can debug exceptions or any kind of error when something

using Gson library in GWT client code

I’m currently writing a web application in java using GWT 2.0 in eclipse. I wanted to know if there is a way to use Gson library in a GWT application’s client code. and if there is a way – please tell

How to i18N localize the header of a tab in GWT TabLayoutPanel

I am unable to use the usual i18N localization of GWT to localize a tabs name (‘header’) in a TabLayoutPanel. This works: <g:TabLayoutPanel addStyleNames=’LocationsModule’ barUnit=PX barHeight=

How can I share a class between GWT client code and server code?

Is it possible to share a Java class between GWT client code and server code? I have a simple class that just holds a few Strings and a few List<String>’s. I’d like to be able to use this clas

How values method work in enum

In Enum how values() method work ? what logic behind values() method ? In my project we cache all enum data in Map as follow : public enum Actions { CREATE(create), UPDATE(update), DELETE(delet

Have you tried using MVC for GWT client code?

Have you tried using MVC or any other UI pattern for GWT client code. What are the pitfalls/advantages you’ve faced in different approaches?

GWT: How to set text-align on com.google.gwt.dom.client.Style

How to set text-align on com.google.gwt.dom.client.Style object?

Answers

Maybe this will help you, since it seems to be the gwt way Internationalization

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

I would like to add to @thommyslaw answer that in some cases you may need to pass Enums thru the wire. I mean make them serializable. In such cases putting GWT.create() inside the Enum will not work. Here is where some kind of Singleton Glossary class will be handy, like:

public class LEGlossary {

private static LEGlossary instance=null;
private static final LocalizationEnum localConstants=GWT.create(LocalizationEnum.class);

private LEGlossary(){

}

public static LEGlossary instance(){
    if(instance==null){
        instance=new LEGlossary();
    }
    return instance;
}

public String localizedValue(Enum<?> value){
    return localConstants.getString(value.name());
}


}

Where LocalizationEnum in my case extends ConstantsWithLookup interface. In this way you isolate the localization code on the client and leave the Enum free to pass thru the wire.