i18n for selectOneMenu witrh SelectItemGroup

时间:2016-02-17 12:36:58

标签: jsf internationalization selectonemenu

在我使用Primefaces 5.3的JSF应用程序中,我有一个带有SelectItemGroup条目的SelectOneMenu,其中需要翻译条目。

我的XHTML片段:

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

这里我使用"#{bundle[status.label]}"进行翻译,但是总是显示Bean类中生成的标签,而不是我资源包中的翻译字符串!

My Bean类代码段:

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;
    }
}

如果我不使用像这样的SelectItemGroups它也不起作用:

    @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");
    }

使用这样的枚举:

<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();
}

捆绑属性:

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

enter code here使用javax.faces.model.SelectItem作为模型值时,var代表其value,而不是SelectItem本身。商品值已经表示为value实例本身的SelectItem,商品标签已经表示为label实例本身的SelectItem

换句话说,在提供SelectItem的列表或数组时,您根本不需要varitemLabelitemValue

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

至于i18n的标签,在bean中抓取/注入资源包#{bundle}并在那里完成工作。 E.g。

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

另见: