Note that there are some explanatory texts on larger screens.

plurals
  1. POLocalizing enum values in resource bundle
    text
    copied!<p>I have a problem with i18n enums in my JSF application. When I started, I had enums with the text defined inside. But now, I have keys tied to message bundles in the enum.</p> <p>Example one of my enums:</p> <pre><code>public enum OrderStatus implements CustomEnum { PENDING("enum.orderstatus.pending"), CANCELED("enum.orderstatus.canceled"); /** * key in message bundle */ private String name; OrderStatus(String name) { this.name = name; } @Override public String getName() { return name; } } </code></pre> <p>In the view layer, I use something like:</p> <pre><code>&lt;!-- input --&gt; &lt;h:selectOneMenu value="#{order.status}"&gt; &lt;f:selectItems value="#{flowUtils.orderStatuses}"/&gt; &lt;/h:selectOneMenu&gt; &lt;!-- output --&gt; &lt;h:outputText value="#{order.status}"/&gt; </code></pre> <p>and in Java:</p> <pre><code>public class FlowUtils { public List&lt;SelectItem&gt; getOrderStatuses() { ArrayList&lt;SelectItem&gt; l = new ArrayList&lt;SelectItem&gt;(); for(OrderStatus c: OrderStatus.values()) { // before i18n // l.add(new SelectItem(c, c.getName())); // after i18n l.add(new SelectItem(c, FacesUtil.getMessageValue(c.getName()))); } return l; } } public class FacesUtil { public static String getMessageValue(String name) { FacesContext context = FacesContext.getCurrentInstance(); return context.getApplication().getResourceBundle(context, "m").getString(name); } } </code></pre> <p>It worked well, but when I needed to output <code>#{order.status}</code>, I needed to convert it. So I implemented a converter, but got in trouble with conversion of <code>String</code> to <code>Object</code> in the <code>getAsObject()</code> method.</p> <p>web.xml:</p> <pre><code>&lt;converter&gt; &lt;converter-for-class&gt;model.helpers.OrderStatus&lt;/converter-for-class&gt; &lt;converter-class&gt;model.helpers.EnumTypeConverter&lt;/converter-class&gt; &lt;/converter&gt; </code></pre> <p>Java:</p> <pre><code>public class EnumTypeConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException { // value = localized value :( Class enumType = comp.getValueBinding("value").getType(context); return Enum.valueOf(enumType, value); } @Override public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException { if (object == null) { return null; } CustomEnum type = (CustomEnum) object; ResourceBundle messages = context.getApplication().getResourceBundle(context, "m"); String text = messages.getString(type.getName()); return text; } } </code></pre> <p>I'm entangled now with that. Anybody know how to internationalize multiple Enums efficiently?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload