0

In my JSF application with Primefaces 5.3 I have a SelectOneMenu with SelectItemGroup entries where the entries need to be translated.

My XHTML snippet:

<p:selectOneMenu id="status" value="#{bean.status}">
    <f:selectItems value="#{bean.statusItems}"
        var="status"
        itemLabel="#{bundle[status.label]}"
        itemValue="#{status}"/>
</p:selectOneMenu>

Here I use "#{bundle[status.label]}" to translate but always the labels as generated in the Bean class are shown and never the translated strings from my resource bundle!

My Bean class snippet:

public class Bean {
    @PostConstruct
    public void init() {
        SelectItemGroup g1 = new SelectItemGroup("status.offerPhase");
        g1.setSelectItems(
                new SelectItem[]{
                        new SelectItem("untreated", "status.offerPhase.untreated"),
                        new SelectItem("In Progress", "status.offerPhase.untreated.inProgress"),
                });

        SelectItemGroup g2 = new SelectItemGroup("status.ongoing");
        g2.setSelectItems(
                new SelectItem[]{
                        new SelectItem("Order received", "status.ongoing.orderReceived"),
                        new SelectItem("Ongoing construction site", "status.ongoing.ongoingConstructionSite"),
                });

        SelectItemGroup g3 = new SelectItemGroup("status.completed");
        g3.setSelectItems(
                new SelectItem[]{
                        new SelectItem("Finished", "status.completed.finished"),
                        new SelectItem("Archived", "status.completed.archived"),
                });
        statusItems = new ArrayList<>();
        statusItems.add(g1);
        statusItems.add(g2);
        statusItems.add(g3);
    }

    private List<SelectItem> statusItems;
    public List<SelectItem> getStatusItems() {
        return statusItems;
    }
}

It does also not work if I don't use SelectItemGroups like this:

    @PostConstruct
    public void init() {
        statusItems = new ArrayList<>();
        statusItems.add(new SelectItem("untreated", "status.offerPhase.untreated"));
        statusItems.add(new SelectItem("In Progress", "status.offerPhase.untreated.inProgress");
    }

Using enums like this works:

<p:selectOneMenu id="status2" value="#{bean.statusEnum}">
    <f:selectItems value="#{bean.statusEnum}"
        var="status"
        itemValue="#{status}"
        itemLabel="#{bundle[status.name()]}"/>
</p:selectOneMenu>

public enum Status {
    STATUS_UNTREATEFD, STATUS_IN_PROGRESS, STATUS_COMPLETED
}
public Status[] getStatusEnum() {
    return Status.values();
}

Bundle Properties:

STATUS_UNTREATED=Untreated
STATUS_IN_PROGRESS=In Progress
status.offerPhase.untreated=Untreated
status.offerPhase.inProgress=In Progress

What am I doing wrong here?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
raho
  • 129
  • 4
  • 18

1 Answers1

1

enter code hereWhen using javax.faces.model.SelectItem as model value, the var would represent its value, not the SelectItem itself. The item value is already represented as value of SelectItem instance itself, and the item label is already represented as label of SelectItem instance itself.

In other words, when providing a list or array of SelectItem, you do not need var, itemLabel and itemValue at all.

<f:selectItems value="#{bean.statusItems}" />

As to i18n'ing the labels, grab/inject the resource bundle #{bundle} in bean and do the job over there. E.g.

new SelectItemGroup(bundle.getString("status.offerPhase"));

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • First of all, thanks for your fast response! Using bundle.getString() solved my problem partially, because I get the translated strings for the inital locale. But I have a LocaleSwitcher in my JSF page, where I can switch the language and than the translated selectItems remain always the same! How can I handle this Locale switching? – raho Feb 17 '16 at 13:27
  • Pass locale while getting bundle, or inject bundle. See "See also" link for detail. – BalusC Feb 17 '16 at 13:28
  • I solved the translation now also with the LocaleSwitcher. Reason was that I created the SelectItems in a Producer class with ApplicationScope and therefore the SelectItems where created only once! – raho Feb 17 '16 at 13:46