While NLS class is nice to use, I sometimes want to localize messages corresponding to enums. Currently it looks like this in Java code:
package foo.bar;
public enum YesNo { YES, NO }
package foo.bar;
public class Msg extends NLS {
public String yes;
public String no;
... // other string fields
static { /* init messages */ }
public String getYesNoMsg(YesNo arg) {
switch(arg) {
case YES: return yes;
case NO: return no;
}
}
}
and in properties file:
So changing enum means I need to change 4 places:
- Enum itself
- Fields of Msg
- getYesNoMsg method
- properties file
Obviously, I can't get away from having to change 1 and 4, but ideally those should be the only places which have to change:
public enum YesNo { YES, NO }
package foo.bar;
public enum YesNo { YES, NO }
package foo.bar;
public class Msg extends NLS {
public EnumMap<YesNo, String> yesNoMap;
static { /* init messages */ }
public String getYesNoMsg(YesNo arg) {
return yesNoMap.get(arg)
}
}
and in properties file:
foo.bar.YesNo.YES = ...
foo.bar.YesNo.NO = ...
The best I can do currently is 3 places to change: keep yes and no fields (renamed to foo_bar_YesNo_YES/NO), and fill EnumMap by reflection after initializing messages. Is there a better way that I missed?