Today, I have a style question: Given the following code, should I be overriding Enum#toString()
to present the application user a localized string or switch to a custom function (say, getLabel()
) or is there another method to achieve the same effect?
I'm mainly after staying DRY here, so hardcoding the resource bundle prefix is not an option.
I have a simple enum like this:
public enum DUE_DATE {
START,
PRESENTED;
public String toString() {
return DBResBundle.instance().getString("due_date_lbl_" + name());
}
}
where DBResBundle
is my ResourceBundle implementation that I can access as msg
in JSF like so:
<h:selectOneMenu value="#{m.due_date}" id="due">
<f:selectItems value="#{dueDates}" var="d"
itemLabel="#{msg['due_date_lbl_'.concat(d.name())]}" />
</h:selectOneMenu>
I'm also using the the enum in code (not just JSF) context, but I don't utilize the toString()
there.